(select-safe-coding-system): Try to
[bpt/emacs.git] / lisp / international / mule.el
CommitLineData
4ed46869
KH
1;;; mule.el --- basic commands for mulitilingual environment
2
2fd125a3
KH
3;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4;; Free Software Foundation, Inc.
5;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6;; National Institute of Advanced Industrial Science and Technology (AIST)
7;; Registration Number H14PRO021
4ed46869
KH
8
9;; Keywords: mule, multilingual, character set, coding system
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
369314dc 24;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
4ed46869 27
60370d40
PJ
28;;; Commentary:
29
4ed46869
KH
30;;; Code:
31
d2542b42 32(defconst mule-version "5.0 (SAKAKI)" "\
4ed46869
KH
33Version number and name of this version of MULE (multilingual environment).")
34
35554641 35(defconst mule-version-date "1999.12.7" "\
4ed46869
KH
36Distribution date of this version of MULE (multilingual environment).")
37
38(defun load-with-code-conversion (fullname file &optional noerror nomessage)
0f69cb38
KH
39 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
40The file contents are decoded before evaluation if necessary.
4ed46869
KH
41If optional second arg NOERROR is non-nil,
42 report no error if FILE doesn't exist.
43Print messages at start and end of loading unless
44 optional third arg NOMESSAGE is non-nil.
45Return t if file exists."
46 (if (null (file-readable-p fullname))
47 (and (null noerror)
48 (signal 'file-error (list "Cannot open load file" file)))
49 ;; Read file with code conversion, and then eval.
50 (let* ((buffer
51 ;; To avoid any autoloading, set default-major-mode to
52 ;; fundamental-mode.
88162676
RS
53 ;; So that we don't get completely screwed if the
54 ;; file is encoded in some complicated character set,
55 ;; read it with real decoding, as a multibyte buffer,
56 ;; even if this is a --unibyte Emacs session.
57 (let ((default-major-mode 'fundamental-mode)
58 (default-enable-multibyte-characters t))
4ed46869
KH
59 ;; We can't use `generate-new-buffer' because files.el
60 ;; is not yet loaded.
61 (get-buffer-create (generate-new-buffer-name " *load*"))))
db5cae4b
SM
62 (load-in-progress t)
63 (source (save-match-data (string-match "\\.el\\'" fullname))))
64 (unless nomessage
65 (if source
66 (message "Loading %s (source)..." file)
67 (message "Loading %s..." file)))
68 (when purify-flag
4c86cca0 69 (push file preloaded-file-list))
4ed46869 70 (unwind-protect
a6acd8a2 71 (let ((load-file-name fullname)
1c4cc63a 72 (set-auto-coding-for-load t)
a6acd8a2 73 (inhibit-file-name-operation nil))
4ed46869
KH
74 (save-excursion
75 (set-buffer buffer)
76 (insert-file-contents fullname)
7d276780
EZ
77 ;; If the loaded file was inserted with no-conversion or
78 ;; raw-text coding system, make the buffer unibyte.
79 ;; Otherwise, eval-buffer might try to interpret random
80 ;; binary junk as multibyte characters.
81 (if (and enable-multibyte-characters
82 (or (eq (coding-system-type last-coding-system-used) 5)
83 (eq last-coding-system-used 'no-conversion)))
84 (set-buffer-multibyte nil))
4ed46869
KH
85 ;; Make `kill-buffer' quiet.
86 (set-buffer-modified-p nil))
0f69cb38 87 ;; Have the original buffer current while we eval.
88162676
RS
88 (eval-buffer buffer nil file
89 ;; If this Emacs is running with --unibyte,
90 ;; convert multibyte strings to unibyte
91 ;; after reading them.
ba74e833 92;; (not default-enable-multibyte-characters)
8dd08b5b 93 nil t
ba74e833 94 ))
cfc70cdf
RS
95 (let (kill-buffer-hook kill-buffer-query-functions)
96 (kill-buffer buffer)))
4ed46869 97 (let ((hook (assoc file after-load-alist)))
db5cae4b
SM
98 (when hook
99 (mapcar (function eval) (cdr hook))))
100 (unless (or nomessage noninteractive)
101 (if source
102 (message "Loading %s (source)...done" file)
103 (message "Loading %s...done" file)))
4ed46869
KH
104 t)))
105
106;; API (Application Program Interface) for charsets.
107
2e02a76f 108(defsubst charset-quoted-standard-p (obj)
521d4010 109 "Return t if OBJ is a quoted symbol, and is the name of a standard charset."
2e02a76f
RS
110 (and (listp obj) (eq (car obj) 'quote)
111 (symbolp (car-safe (cdr obj)))
112 (let ((vector (get (car-safe (cdr obj)) 'charset)))
113 (and (vectorp vector)
114 (< (aref vector 0) 160)))))
4ed46869
KH
115
116(defsubst charsetp (object)
e4a7fdfc 117 "T if OBJECT is a charset."
4ed46869
KH
118 (and (symbolp object) (vectorp (get object 'charset))))
119
120(defsubst charset-info (charset)
121 "Return a vector of information of CHARSET.
122The elements of the vector are:
123 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
124 LEADING-CODE-BASE, LEADING-CODE-EXT,
125 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
126 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
127 PLIST,
128where
129CHARSET-ID (integer) is the identification number of the charset.
aa8bb645
KH
130BYTES (integer) is the length of multi-byte form of a character in
131 the charset: one of 1, 2, 3, and 4.
4ed46869
KH
132DIMENSION (integer) is the number of bytes to represent a character of
133the charset: 1 or 2.
134CHARS (integer) is the number of characters in a dimension: 94 or 96.
4ed46869
KH
135WIDTH (integer) is the number of columns a character in the charset
136 occupies on the screen: one of 0, 1, and 2.
137DIRECTION (integer) is the rendering direction of characters in the
08b4ace5
KH
138 charset when rendering. If 0, render from left to right, else
139 render from right to left.
4ed46869
KH
140LEADING-CODE-BASE (integer) is the base leading-code for the
141 charset.
142LEADING-CODE-EXT (integer) is the extended leading-code for the
143 charset. All charsets of less than 0xA0 has the value 0.
144ISO-FINAL-CHAR (character) is the final character of the
7dd4c92d
KH
145 corresponding ISO 2022 charset. If the charset is not assigned
146 any final character, the value is -1.
4ed46869
KH
147ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
148 while encoding to variants of ISO 2022 coding system, one of the
149 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
7dd4c92d 150 If the charset is not assigned any final character, the value is -1.
4ed46869
KH
151REVERSE-CHARSET (integer) is the charset which differs only in
152 LEFT-TO-RIGHT value from the charset. If there's no such a
153 charset, the value is -1.
154SHORT-NAME (string) is the short name to refer to the charset.
155LONG-NAME (string) is the long name to refer to the charset
156DESCRIPTION (string) is the description string of the charset.
157PLIST (property list) may contain any type of information a user
158 want to put and get by functions `put-charset-property' and
159 `get-charset-property' respectively."
160 (get charset 'charset))
161
40c81f74
PE
162;; It is better not to use backquote in this file,
163;; because that makes a bootstrapping problem
164;; if you need to recompile all the Lisp files using interpreted code.
165
4ed46869
KH
166(defmacro charset-id (charset)
167 "Return charset identification number of CHARSET."
2e02a76f 168 (if (charset-quoted-standard-p charset)
4ed46869 169 (aref (charset-info (nth 1 charset)) 0)
40c81f74 170 (list 'aref (list 'charset-info charset) 0)))
4ed46869
KH
171
172(defmacro charset-bytes (charset)
900dc6e3
KH
173 "Return bytes of CHARSET.
174See the function `charset-info' for more detail."
2e02a76f 175 (if (charset-quoted-standard-p charset)
4ed46869 176 (aref (charset-info (nth 1 charset)) 1)
40c81f74 177 (list 'aref (list 'charset-info charset) 1)))
4ed46869
KH
178
179(defmacro charset-dimension (charset)
900dc6e3
KH
180 "Return dimension of CHARSET.
181See the function `charset-info' for more detail."
2e02a76f 182 (if (charset-quoted-standard-p charset)
4ed46869 183 (aref (charset-info (nth 1 charset)) 2)
40c81f74 184 (list 'aref (list 'charset-info charset) 2)))
4ed46869
KH
185
186(defmacro charset-chars (charset)
900dc6e3
KH
187 "Return character numbers contained in a dimension of CHARSET.
188See the function `charset-info' for more detail."
2e02a76f 189 (if (charset-quoted-standard-p charset)
4ed46869 190 (aref (charset-info (nth 1 charset)) 3)
40c81f74 191 (list 'aref (list 'charset-info charset) 3)))
4ed46869
KH
192
193(defmacro charset-width (charset)
900dc6e3
KH
194 "Return width (how many column occupied on a screen) of CHARSET.
195See the function `charset-info' for more detail."
2e02a76f 196 (if (charset-quoted-standard-p charset)
4ed46869 197 (aref (charset-info (nth 1 charset)) 4)
40c81f74 198 (list 'aref (list 'charset-info charset) 4)))
4ed46869
KH
199
200(defmacro charset-direction (charset)
900dc6e3
KH
201 "Return direction of CHARSET.
202See the function `charset-info' for more detail."
2e02a76f 203 (if (charset-quoted-standard-p charset)
4ed46869 204 (aref (charset-info (nth 1 charset)) 5)
40c81f74 205 (list 'aref (list 'charset-info charset) 5)))
4ed46869
KH
206
207(defmacro charset-iso-final-char (charset)
900dc6e3
KH
208 "Return final char of CHARSET.
209See the function `charset-info' for more detail."
2e02a76f 210 (if (charset-quoted-standard-p charset)
4ed46869 211 (aref (charset-info (nth 1 charset)) 8)
40c81f74 212 (list 'aref (list 'charset-info charset) 8)))
4ed46869
KH
213
214(defmacro charset-iso-graphic-plane (charset)
900dc6e3
KH
215 "Return graphic plane of CHARSET.
216See the function `charset-info' for more detail."
2e02a76f 217 (if (charset-quoted-standard-p charset)
4ed46869 218 (aref (charset-info (nth 1 charset)) 9)
40c81f74 219 (list 'aref (list 'charset-info charset) 9)))
4ed46869
KH
220
221(defmacro charset-reverse-charset (charset)
900dc6e3
KH
222 "Return reverse charset of CHARSET.
223See the function `charset-info' for more detail."
2e02a76f 224 (if (charset-quoted-standard-p charset)
4ed46869 225 (aref (charset-info (nth 1 charset)) 10)
40c81f74 226 (list 'aref (list 'charset-info charset) 10)))
4ed46869
KH
227
228(defmacro charset-short-name (charset)
900dc6e3
KH
229 "Return short name of CHARSET.
230See the function `charset-info' for more detail."
2e02a76f 231 (if (charset-quoted-standard-p charset)
4ed46869 232 (aref (charset-info (nth 1 charset)) 11)
40c81f74 233 (list 'aref (list 'charset-info charset) 11)))
4ed46869
KH
234
235(defmacro charset-long-name (charset)
900dc6e3
KH
236 "Return long name of CHARSET.
237See the function `charset-info' for more detail."
2e02a76f 238 (if (charset-quoted-standard-p charset)
4ed46869 239 (aref (charset-info (nth 1 charset)) 12)
40c81f74 240 (list 'aref (list 'charset-info charset) 12)))
4ed46869
KH
241
242(defmacro charset-description (charset)
0611934b 243 "Return description of CHARSET.
900dc6e3 244See the function `charset-info' for more detail."
2e02a76f 245 (if (charset-quoted-standard-p charset)
4ed46869 246 (aref (charset-info (nth 1 charset)) 13)
40c81f74 247 (list 'aref (list 'charset-info charset) 13)))
4ed46869
KH
248
249(defmacro charset-plist (charset)
900dc6e3
KH
250 "Return list charset property of CHARSET.
251See the function `charset-info' for more detail."
40c81f74
PE
252 (list 'aref
253 (if (charset-quoted-standard-p charset)
254 (charset-info (nth 1 charset))
255 (list 'charset-info charset))
256 14))
4ed46869
KH
257
258(defun set-charset-plist (charset plist)
e8dd0160 259 "Set CHARSET's property list to PLIST, and return PLIST."
4ed46869
KH
260 (aset (charset-info charset) 14 plist))
261
d3e7e7cf
EZ
262(defun make-char (charset &optional code1 code2)
263 "Return a character of CHARSET whose position codes are CODE1 and CODE2.
f98e2797 264CODE1 and CODE2 are optional, but if you don't supply
a9fb0b58
KH
265sufficient position codes, return a generic character which stands for
266all characters or group of characters in the character set.
d3e7e7cf 267A generic character can be used to index a char table (e.g. syntax-table).
9ff05eae
KH
268
269Such character sets as ascii, eight-bit-control, and eight-bit-graphic
270don't have corresponding generic characters. If CHARSET is one of
271them and you don't supply CODE1, return the character of the smallest
272code in CHARSET.
273
6fb7766c
DL
274If CODE1 or CODE2 are invalid (out of range), this function signals an
275error. However, the eighth bit of both CODE1 and CODE2 is zeroed
276before they are used to index CHARSET. Thus you may use, say, the
277actual ISO 8859 character code rather than subtracting 128, as you
278would need to index the corresponding Emacs charset."
636799f2 279 (make-char-internal (charset-id charset) code1 code2))
a6acd8a2
KH
280
281(put 'make-char 'byte-compile
521d4010 282 (function
a6acd8a2
KH
283 (lambda (form)
284 (let ((charset (nth 1 form)))
285 (if (charset-quoted-standard-p charset)
286 (byte-compile-normal-call
287 (cons 'make-char-internal
288 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
289 (byte-compile-normal-call
290 (cons 'make-char-internal
291 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
4ed46869 292
d3675a42 293(defun charset-list ()
900dc6e3
KH
294 "Return list of charsets ever defined.
295
d3675a42 296This function is provided for backward compatibility.
900dc6e3 297Now we have the variable `charset-list'."
d3675a42
KH
298 charset-list)
299
13d5617d
KH
300(defsubst generic-char-p (char)
301 "Return t if and only if CHAR is a generic character.
521d4010 302See also the documentation of `make-char'."
1d935669
KH
303 (and (>= char 0400)
304 (let ((l (split-char char)))
305 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
306 (not (eq (car l) 'composition))))))
69eba008 307
cc57cc54 308(defun decode-char (ccs code-point &optional restriction)
7f341749 309 "Return character specified by coded character set CCS and CODE-POINT in it.
cc57cc54 310Return nil if such a character is not supported.
e76938e7 311Currently the only supported coded character set is `ucs' (ISO/IEC
9fb1b1a8 31210646: Universal Multi-Octet Coded Character Set), and the result is
0c76a98d
KH
313translated through the translation-table named
314`utf-translation-table-for-decode' or the translation-hash-table named
315`utf-subst-table-for-decode'.
cc57cc54
KH
316
317Optional argument RESTRICTION specifies a way to map the pair of CCS
e7fea150 318and CODE-POINT to a character. Currently not supported and just ignored."
9fb1b1a8
DL
319 (cond
320 ((eq ccs 'ucs)
8458c8b9 321 (or (utf-lookup-subst-table-for-decode code-point)
0c76a98d
KH
322 (let ((c (cond
323 ((< code-point 160)
324 code-point)
325 ((< code-point 256)
326 (make-char 'latin-iso8859-1 code-point))
327 ((< code-point #x2500)
328 (setq code-point (- code-point #x0100))
329 (make-char 'mule-unicode-0100-24ff
330 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
331 ((< code-point #x3400)
332 (setq code-point (- code-point #x2500))
333 (make-char 'mule-unicode-2500-33ff
334 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
335 ((and (>= code-point #xe000) (< code-point #x10000))
336 (setq code-point (- code-point #xe000))
337 (make-char 'mule-unicode-e000-ffff
338 (+ (/ code-point 96) 32)
339 (+ (% code-point 96) 32))))))
340 (when c
341 (or (aref (get 'utf-translation-table-for-decode
342 'translation-table) c)
343 c)))))))
cc57cc54
KH
344
345(defun encode-char (char ccs &optional restriction)
7f341749 346 "Return code-point in coded character set CCS that corresponds to CHAR.
cc57cc54 347Return nil if CHAR is not included in CCS.
e76938e7 348Currently the only supported coded character set is `ucs' (ISO/IEC
9fb1b1a8 34910646: Universal Multi-Octet Coded Character Set), and CHAR is first
0c76a98d
KH
350translated through the translation-table named
351`utf-translation-table-for-encode' or the translation-hash-table named
352`utf-subst-table-for-encode'.
7f341749
KH
353
354CHAR should be in one of these charsets:
cc57cc54 355 ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
6ba9b20b 356 mule-unicode-e000-ffff, eight-bit-control
cc57cc54
KH
357Otherwise, return nil.
358
359Optional argument RESTRICTION specifies a way to map CHAR to a
360code-point in CCS. Currently not supported and just ignored."
361 (let* ((split (split-char char))
9fb1b1a8
DL
362 (charset (car split))
363 trans)
cc57cc54 364 (cond ((eq ccs 'ucs)
8458c8b9 365 (or (utf-lookup-subst-table-for-encode char)
0c76a98d
KH
366 (let ((table (get 'utf-translation-table-for-encode
367 'translation-table)))
368 (setq trans (aref table char))
369 (if trans
370 (setq split (split-char trans)
371 charset (car split)))
372 (cond ((eq charset 'ascii)
bb55c6c9 373 (or trans char))
0c76a98d
KH
374 ((eq charset 'latin-iso8859-1)
375 (+ (nth 1 split) 128))
376 ((eq charset 'mule-unicode-0100-24ff)
377 (+ #x0100 (+ (* (- (nth 1 split) 32) 96)
378 (- (nth 2 split) 32))))
379 ((eq charset 'mule-unicode-2500-33ff)
380 (+ #x2500 (+ (* (- (nth 1 split) 32) 96)
381 (- (nth 2 split) 32))))
382 ((eq charset 'mule-unicode-e000-ffff)
383 (+ #xe000 (+ (* (- (nth 1 split) 32) 96)
384 (- (nth 2 split) 32))))
385 ((eq charset 'eight-bit-control)
386 char))))))))
cc57cc54 387
0269ddfb 388\f
e76938e7 389;; Coding system stuff
4ed46869 390
8057896b 391;; Coding system is a symbol that has the property `coding-system'.
4ed46869 392;;
8057896b
KH
393;; The value of the property `coding-system' is a vector of the
394;; following format:
d3675a42 395;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
8057896b 396;; We call this vector as coding-spec. See comments in src/coding.c
521d4010 397;; for more detail.
8057896b
KH
398
399(defconst coding-spec-type-idx 0)
400(defconst coding-spec-mnemonic-idx 1)
401(defconst coding-spec-doc-string-idx 2)
0269ddfb 402(defconst coding-spec-plist-idx 3)
8057896b
KH
403(defconst coding-spec-flags-idx 4)
404
a6acd8a2
KH
405;; PLIST is a property list of a coding system. To share PLIST among
406;; alias coding systems, a coding system has PLIST in coding-spec
407;; instead of having it in normal property list of Lisp symbol.
408;; Here's a list of coding system properties currently being used.
0269ddfb
KH
409;;
410;; o coding-category
411;;
412;; The value is a coding category the coding system belongs to. The
521d4010 413;; function `make-coding-system' sets this value automatically
1b46a680 414;; unless its argument PROPERTIES specifies this property.
4ed46869 415;;
0269ddfb 416;; o alias-coding-systems
4ed46869 417;;
0269ddfb
KH
418;; The value is a list of coding systems of the same alias group. The
419;; first element is the coding system made at first, which we call as
1b46a680
KH
420;; `base coding system'. The function `make-coding-system' sets this
421;; value automatically and `define-coding-system-alias' updates it.
0269ddfb 422;;
3c948dde
KH
423;; See the documentation of make-coding-system for the meanings of the
424;; following properties.
4ed46869 425;;
3c948dde 426;; o post-read-conversion
0269ddfb 427;; o pre-write-conversion
f967223b 428;; o translation-table-for-decode
f967223b 429;; o translation-table-for-encode
c11a8f77 430;; o safe-chars
a6acd8a2 431;; o safe-charsets
b25eef20 432;; o mime-charset
c76b5c99 433;; o valid-codes (meaningful only for a coding system based on CCL)
c76b5c99 434
0269ddfb 435
0269ddfb 436(defsubst coding-system-spec (coding-system)
521d4010 437 "Return coding-spec of CODING-SYSTEM."
0269ddfb 438 (get (check-coding-system coding-system) 'coding-system))
4ed46869 439
4ed46869 440(defun coding-system-type (coding-system)
0269ddfb
KH
441 "Return the coding type of CODING-SYSTEM.
442A coding type is an integer value indicating the encoding method
443of CODING-SYSTEM. See the function `make-coding-system' for more detail."
2653e2a7 444 (aref (coding-system-spec coding-system) coding-spec-type-idx))
4ed46869 445
4ed46869 446(defun coding-system-mnemonic (coding-system)
0269ddfb 447 "Return the mnemonic character of CODING-SYSTEM.
6e2c8840
KH
448The mnemonic character of a coding system is used in mode line
449to indicate the coding system. If the arg is nil, return ?-."
450 (let ((spec (coding-system-spec coding-system)))
451 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
4ed46869 452
8057896b 453(defun coding-system-doc-string (coding-system)
0269ddfb 454 "Return the documentation string for CODING-SYSTEM."
2653e2a7 455 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
4ed46869 456
d3675a42 457(defun coding-system-plist (coding-system)
0269ddfb 458 "Return the property list of CODING-SYSTEM."
2653e2a7 459 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
d3675a42 460
4ed46869 461(defun coding-system-flags (coding-system)
0269ddfb
KH
462 "Return `flags' of CODING-SYSTEM.
463A `flags' of a coding system is a vector of length 32 indicating detailed
464information of a coding system. See the function `make-coding-system'
465for more detail."
2653e2a7 466 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
0269ddfb
KH
467
468(defun coding-system-get (coding-system prop)
469 "Extract a value from CODING-SYSTEM's property list for property PROP."
470 (plist-get (coding-system-plist coding-system) prop))
471
472(defun coding-system-put (coding-system prop val)
473 "Change value in CODING-SYSTEM's property list PROP to VAL."
474 (let ((plist (coding-system-plist coding-system)))
475 (if plist
476 (plist-put plist prop val)
2653e2a7
KH
477 (aset (coding-system-spec coding-system) coding-spec-plist-idx
478 (list prop val)))))
0269ddfb
KH
479
480(defun coding-system-category (coding-system)
72f73748
DL
481 "Return the coding category of CODING-SYSTEM.
482See also `coding-category-list'."
0269ddfb
KH
483 (coding-system-get coding-system 'coding-category))
484
485(defun coding-system-base (coding-system)
486 "Return the base coding system of CODING-SYSTEM.
d9e3229d
KH
487A base coding system is what made by `make-coding-system'.
488Any alias nor subsidiary coding systems are not base coding system."
0269ddfb
KH
489 (car (coding-system-get coding-system 'alias-coding-systems)))
490
0269ddfb
KH
491;; Coding system also has a property `eol-type'.
492;;
493;; This property indicates how the coding system handles end-of-line
494;; format. The value is integer 0, 1, 2, or a vector of three coding
495;; systems. Each integer value 0, 1, and 2 indicates the format of
496;; end-of-line LF, CRLF, and CR respectively. A vector value
497;; indicates that the format of end-of-line should be detected
498;; automatically. Nth element of the vector is the subsidiary coding
499;; system whose `eol-type' property is N.
4ed46869 500
8057896b 501(defun coding-system-eol-type (coding-system)
0269ddfb
KH
502 "Return eol-type of CODING-SYSTEM.
503An eol-type is integer 0, 1, 2, or a vector of coding systems.
4ed46869 504
0269ddfb
KH
505Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
506CRLF, and CR respectively.
507
508A vector value indicates that a format of end-of-line should be
509detected automatically. Nth element of the vector is the subsidiary
510coding system whose eol-type is N."
511 (get coding-system 'eol-type))
bd882697 512
2e729bfa
JB
513(defun coding-system-eol-type-mnemonic (coding-system)
514 "Return the string indicating end-of-line format of CODING-SYSTEM."
515 (let* ((eol-type (coding-system-eol-type coding-system))
f4f00827 516 (val (cond ((eq eol-type 0) eol-mnemonic-unix)
2e729bfa
JB
517 ((eq eol-type 1) eol-mnemonic-dos)
518 ((eq eol-type 2) eol-mnemonic-mac)
f4f00827 519 (t eol-mnemonic-undecided))))
2e729bfa
JB
520 (if (stringp val)
521 val
522 (char-to-string val))))
523
857ea15c
AS
524(defun coding-system-lessp (x y)
525 (cond ((eq x 'no-conversion) t)
526 ((eq y 'no-conversion) nil)
527 ((eq x 'emacs-mule) t)
528 ((eq y 'emacs-mule) nil)
529 ((eq x 'undecided) t)
530 ((eq y 'undecided) nil)
531 (t (let ((c1 (coding-system-mnemonic x))
532 (c2 (coding-system-mnemonic y)))
533 (or (< (downcase c1) (downcase c2))
534 (and (not (> (downcase c1) (downcase c2)))
535 (< c1 c2)))))))
536
5e2e859a
KH
537(defun coding-system-equal (coding-system-1 coding-system-2)
538 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
539Two coding systems are identical if two symbols are equal
540or one is an alias of the other."
541 (or (eq coding-system-1 coding-system-2)
542 (and (equal (coding-system-spec coding-system-1)
543 (coding-system-spec coding-system-2))
544 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
545 (eol-type-2 (coding-system-eol-type coding-system-2)))
546 (or (eq eol-type-1 eol-type-2)
547 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
548
857ea15c 549(defun add-to-coding-system-list (coding-system)
521d4010 550 "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
857ea15c
AS
551 (if (or (null coding-system-list)
552 (coding-system-lessp coding-system (car coding-system-list)))
553 (setq coding-system-list (cons coding-system coding-system-list))
554 (let ((len (length coding-system-list))
555 mid (tem coding-system-list))
556 (while (> len 1)
557 (setq mid (nthcdr (/ len 2) tem))
558 (if (coding-system-lessp (car mid) coding-system)
559 (setq tem mid
560 len (- len (/ len 2)))
561 (setq len (/ len 2))))
562 (setcdr tem (cons coding-system (cdr tem))))))
563
80a7463d 564(defun coding-system-list (&optional base-only)
c11a8f77
KH
565 "Return a list of all existing non-subsidiary coding systems.
566If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
567The value doesn't include subsidiary coding systems which are what
568made from bases and aliases automatically for various end-of-line
569formats (e.g. iso-latin-1-unix, koi8-r-dos)."
80a7463d
KH
570 (let* ((codings (copy-sequence coding-system-list))
571 (tail (cons nil codings)))
572 ;; Remove subsidiary coding systems (eol variants) and alias
573 ;; coding systems (if necessary).
574 (while (cdr tail)
575 (let* ((coding (car (cdr tail)))
576 (aliases (coding-system-get coding 'alias-coding-systems)))
577 (if (or
578 ;; CODING is an eol variant if not in ALIASES.
579 (not (memq coding aliases))
580 ;; CODING is an alias if it is not car of ALIASES.
581 (and base-only (not (eq coding (car aliases)))))
582 (setcdr tail (cdr (cdr tail)))
583 (setq tail (cdr tail)))))
584 codings))
585
6fb7766c
DL
586(defun map-charset-chars (func charset)
587 "Use FUNC to map over all characters in CHARSET for side effects.
588FUNC is a function of two args, the start and end (inclusive) of a
589character code range. Thus FUNC should iterate over [START, END]."
590 (let* ((dim (charset-dimension charset))
591 (chars (charset-chars charset))
592 (start (if (= chars 94)
593 33
594 32)))
595 (if (= dim 1)
596 (funcall func
597 (make-char charset start)
598 (make-char charset (+ start chars -1)))
599 (dotimes (i chars)
600 (funcall func
601 (make-char charset (+ i start) start)
602 (make-char charset (+ i start) (+ start chars -1)))))))
603
9857367f 604(defalias 'register-char-codings 'ignore "")
620956ca 605(make-obsolete 'register-char-codings
9857367f 606 "it exists just for backward compatibility, and does nothing."
620956ca
KH
607 "21.3")
608
609(defconst char-coding-system-table nil
610 "This is an obsolete variable.
611It exists just for backward compatibility, and the value is always nil.")
c11a8f77 612
6e9722b0 613(defun make-subsidiary-coding-system (coding-system)
521d4010 614 "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
0269ddfb
KH
615 (let ((coding-spec (coding-system-spec coding-system))
616 (subsidiaries (vector (intern (format "%s-unix" coding-system))
8057896b
KH
617 (intern (format "%s-dos" coding-system))
618 (intern (format "%s-mac" coding-system))))
0269ddfb
KH
619 (i 0)
620 temp)
8057896b 621 (while (< i 3)
0269ddfb 622 (put (aref subsidiaries i) 'coding-system coding-spec)
8057896b 623 (put (aref subsidiaries i) 'eol-type i)
857ea15c 624 (add-to-coding-system-list (aref subsidiaries i))
0269ddfb
KH
625 (setq coding-system-alist
626 (cons (list (symbol-name (aref subsidiaries i)))
627 coding-system-alist))
8057896b
KH
628 (setq i (1+ i)))
629 subsidiaries))
4ed46869 630
50c29104
KH
631(defun transform-make-coding-system-args (name type &optional doc-string props)
632 "For internal use only.
633Transform XEmacs style args for `make-coding-system' to Emacs style.
634Value is a list of transformed arguments."
635 (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
636 (eol-type (plist-get props 'eol-type))
637 properties tmp)
638 (cond
639 ((eq eol-type 'lf) (setq eol-type 'unix))
640 ((eq eol-type 'crlf) (setq eol-type 'dos))
641 ((eq eol-type 'cr) (setq eol-type 'mac)))
642 (if (setq tmp (plist-get props 'post-read-conversion))
643 (setq properties (plist-put properties 'post-read-conversion tmp)))
644 (if (setq tmp (plist-get props 'pre-write-conversion))
645 (setq properties (plist-put properties 'pre-write-conversion tmp)))
646 (cond
f4a012a6
KH
647 ((eq type 'shift-jis)
648 `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
649 ((eq type 'iso2022) ; This is not perfect.
650 (if (plist-get props 'escape-quoted)
651 (error "escape-quoted is not supported: %S"
652 `(,name ,type ,doc-string ,props)))
653 (let ((g0 (plist-get props 'charset-g0))
654 (g1 (plist-get props 'charset-g1))
655 (g2 (plist-get props 'charset-g2))
656 (g3 (plist-get props 'charset-g3))
657 (use-roman
658 (and
659 (eq (cadr (assoc 'latin-jisx0201
660 (plist-get props 'input-charset-conversion)))
661 'ascii)
662 (eq (cadr (assoc 'ascii
663 (plist-get props 'output-charset-conversion)))
664 'latin-jisx0201)))
665 (use-oldjis
666 (and
667 (eq (cadr (assoc 'japanese-jisx0208-1978
668 (plist-get props 'input-charset-conversion)))
669 'japanese-jisx0208)
670 (eq (cadr (assoc 'japanese-jisx0208
671 (plist-get props 'output-charset-conversion)))
672 'japanese-jisx0208-1978))))
673 (if (charsetp g0)
674 (if (plist-get props 'force-g0-on-output)
675 (setq g0 `(nil ,g0))
676 (setq g0 `(,g0 t))))
677 (if (charsetp g1)
678 (if (plist-get props 'force-g1-on-output)
679 (setq g1 `(nil ,g1))
680 (setq g1 `(,g1 t))))
681 (if (charsetp g2)
682 (if (plist-get props 'force-g2-on-output)
683 (setq g2 `(nil ,g2))
684 (setq g2 `(,g2 t))))
685 (if (charsetp g3)
686 (if (plist-get props 'force-g3-on-output)
687 (setq g3 `(nil ,g3))
688 (setq g3 `(,g3 t))))
689 `(,name 2 ,mnemonic ,doc-string
690 (,g0 ,g1 ,g2 ,g3
691 ,(plist-get props 'short)
692 ,(not (plist-get props 'no-ascii-eol))
693 ,(not (plist-get props 'no-ascii-cntl))
694 ,(plist-get props 'seven)
695 t
696 ,(not (plist-get props 'lock-shift))
697 ,use-roman
698 ,use-oldjis
699 ,(plist-get props 'no-iso6429)
700 nil nil nil nil)
701 ,properties ,eol-type)))
702 ((eq type 'big5)
703 `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
50c29104 704 ((eq type 'ccl)
f4a012a6 705 `(,name 4 ,mnemonic ,doc-string
50c29104 706 (,(plist-get props 'decode) . ,(plist-get props 'encode))
f4a012a6 707 ,properties ,eol-type))
50c29104 708 (t
f4a012a6 709 (error "unsupported XEmacs style make-coding-style arguments: %S"
50c29104
KH
710 `(,name ,type ,doc-string ,props))))))
711
8057896b 712(defun make-coding-system (coding-system type mnemonic doc-string
1b46a680
KH
713 &optional
714 flags
715 properties
716 eol-type)
3bb1accb 717 "Define a new coding system CODING-SYSTEM (symbol).
521d4010 718Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
d9e3229d 719and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
a6acd8a2 720in the following format:
0269ddfb 721 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
3bb1accb
KH
722
723TYPE is an integer value indicating the type of the coding system as follows:
4ed46869 724 0: Emacs internal format,
d9f6dfe6 725 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PCs,
4ed46869 726 2: ISO-2022 including many variants,
d9f6dfe6 727 3: Big5 used mainly on Chinese PCs,
cd9c3177 728 4: private, CCL programs provide encoding/decoding algorithm,
521d4010 729 5: Raw-text, which means that text contains random 8-bit codes.
0269ddfb 730
3bb1accb 731MNEMONIC is a character to be displayed on mode line for the coding system.
0269ddfb 732
3bb1accb 733DOC-STRING is a documentation string for the coding system.
0269ddfb 734
3bb1accb 735FLAGS specifies more detailed information of the coding system as follows:
d6d6d592 736
3bb1accb 737 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
4ed46869
KH
738 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
739 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 740 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 741 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
742 CHARSETn are character sets initially designated to Gn graphic registers.
743 If CHARSETn is nil, Gn is never used.
744 If CHARSETn is t, Gn can be used but nothing designated initially.
745 If CHARSETn is a list of character sets, those character sets are
746 designated to Gn on output, but nothing designated to Gn initially.
421e3b4e 747 But, character set `ascii' can be designated only to G0.
4ed46869
KH
748 SHORT-FORM non-nil means use short designation sequence on output.
749 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
750 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
751 SPACE on output.
752 SEVEN non-nil means use 7-bit code only on output.
753 LOCKING-SHIFT non-nil means use locking-shift.
754 SINGLE-SHIFT non-nil means use single-shift.
755 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
756 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
757 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
758 INIT-BOL non-nil means any designation state is assumed to be reset
759 to initial at each beginning of line on output.
760 DESIGNATION-BOL non-nil means designation sequences should be placed
761 at beginning of line on output.
a6acd8a2 762 SAFE non-nil means convert unsafe characters to `?' on output.
c11a8f77
KH
763 Characters not specified in the property `safe-charsets' nor
764 `safe-chars' are unsafe.
850101ed
RS
765 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
766 a code specified in `latin-extra-code-table' (which see) as a valid
767 code of the coding system.
d6d6d592 768
3bb1accb
KH
769 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
770 decoding and encoding. CCL programs should be specified by their
771 symbols.
d9e3229d 772
a2852e45
KH
773PROPERTIES is an alist of properties vs the corresponding values. The
774following properties are recognized:
775
776 o post-read-conversion
0bca779a 777
a2852e45
KH
778 The value is a function to call after some text is inserted and
779 decoded by the coding system itself and before any functions in
521d4010
DL
780 `after-insert-functions' are called. The argument of this
781 function is the same as for a function in
782 `after-insert-file-functions', i.e. LENGTH of the text inserted,
783 with point at the head of the text to be decoded.
0bca779a 784
a2852e45 785 o pre-write-conversion
0bca779a 786
a2852e45
KH
787 The value is a function to call after all functions in
788 `write-region-annotate-functions' and `buffer-file-format' are
789 called, and before the text is encoded by the coding system itself.
a284eea3
DL
790 The arguments to this function are the same as those of a function
791 in `write-region-annotate-functions', i.e. FROM and TO, specifying
521d4010 792 a region of text.
0bca779a 793
a2852e45 794 o translation-table-for-decode
0bca779a 795
a2852e45
KH
796 The value is a translation table to be applied on decoding. See
797 the function `make-translation-table' for the format of translation
521d4010 798 table. This is not applicable to type 4 (CCL-based) coding systems.
0bca779a 799
a2852e45 800 o translation-table-for-encode
0bca779a 801
521d4010
DL
802 The value is a translation table to be applied on encoding. This is
803 not applicable to type 4 (CCL-based) coding systems.
0bca779a 804
a2852e45 805 o safe-chars
0bca779a 806
a2852e45
KH
807 The value is a char table. If a character has non-nil value in it,
808 the character is safely supported by the coding system. This
809 overrides the specification of safe-charsets.
810
811 o safe-charsets
521d4010 812
a2852e45
KH
813 The value is a list of charsets safely supported by the coding
814 system. The value t means that all charsets Emacs handles are
815 supported. Even if some charset is not in this list, it doesn't
521d4010
DL
816 mean that the charset can't be encoded in the coding system;
817 it just means that some other receiver of text encoded
a2852e45 818 in the coding system won't be able to handle that charset.
0bca779a 819
a2852e45 820 o mime-charset
0bca779a 821
d9f6dfe6 822 The value is a symbol whose name is the `MIME-charset' parameter of
a2852e45 823 the coding system.
0bca779a 824
3b6cc699
DL
825 o mime-text-unsuitable
826
827 A non-nil value means the `mime-charset' property names a charset
828 which is unsuitable for the top-level media type \"text\".
829
a2852e45 830 o valid-codes (meaningful only for a coding system based on CCL)
0bca779a 831
a2852e45
KH
832 The value is a list to indicate valid byte ranges of the encoded
833 file. Each element of the list is an integer or a cons of integer.
834 In the former case, the integer value is a valid byte code. In the
6fb7766c 835 latter case, the integers specify the range of valid byte codes.
a2852e45 836
1e7f6327
KH
837 o composition (meaningful only when TYPE is 0 or 2)
838
0ea0b6d2
DL
839 If the value is non-nil, the coding system preserves composition
840 information.
1e7f6327 841
43b557d5
KH
842 o ascii-incompatible
843
844 If the value is non-nil, the coding system is not compatible
845 with ASCII, which means it encodes or decodes ASCII character
846 string to the different byte sequence.
847
d9e3229d
KH
848These properties are set in PLIST, a property list. This function
849also sets properties `coding-category' and `alias-coding-systems'
850automatically.
4ed46869 851
1b46a680
KH
852EOL-TYPE specifies the EOL type of the coding-system in one of the
853following formats:
854
855 o symbol (unix, dos, or mac)
856
857 The symbol `unix' means Unix-like EOL (LF), `dos' means
858 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
859
860 o number (0, 1, or 2)
861
862 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
863 respectively.
864
865 o vector of coding-systems of length 3
866
867 The EOL type is detected automatically for the coding system.
868 And, according to the detected EOL type, one of the coding
869 systems in the vector is selected. Elements of the vector
4d3a9228 870 corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
1b46a680
KH
871 in this order.
872
3bb1accb
KH
873Kludgy features for backward compatibility:
874
8751. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
876treated as a compiled CCL code.
877
8782. If PROPERTIES is just a list of character sets, the list is set as
879a value of `safe-charsets' in PLIST."
50c29104
KH
880
881 ;; For compatiblity with XEmacs, we check the type of TYPE. If it
c3d0ee51
EZ
882 ;; is a symbol, perhaps, this function is called with XEmacs-style
883 ;; arguments. Here, try to transform that kind of arguments to
50c29104
KH
884 ;; Emacs style.
885 (if (symbolp type)
886 (let ((args (transform-make-coding-system-args coding-system type
887 mnemonic doc-string)))
888 (setq coding-system (car args)
1053cc93 889 type (nth 1 args)
50c29104
KH
890 mnemonic (nth 2 args)
891 doc-string (nth 3 args)
892 flags (nth 4 args)
893 properties (nth 5 args)
894 eol-type (nth 6 args))))
895
0269ddfb 896 ;; Set a value of `coding-system' property.
6e9722b0 897 (let ((coding-spec (make-vector 5 nil))
d9e3229d
KH
898 (no-initial-designation t)
899 (no-alternative-designation t)
e6cddb1a 900 (accept-latin-extra-code nil)
6e9722b0 901 coding-category)
cd9c3177 902 (if (or (not (integerp type)) (< type 0) (> type 5))
0269ddfb 903 (error "TYPE argument must be 0..5"))
8057896b 904 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
521d4010 905 (error "MNEMONIC argument must be an ASCII printable character"))
0269ddfb
KH
906 (aset coding-spec coding-spec-type-idx type)
907 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
908 (aset coding-spec coding-spec-doc-string-idx
e127a722 909 (purecopy (if (stringp doc-string) doc-string "")))
6e9722b0
KH
910 (cond ((= type 0)
911 (setq coding-category 'coding-category-emacs-mule))
912 ((= type 1)
913 (setq coding-category 'coding-category-sjis))
914 ((= type 2) ; ISO2022
4ed46869 915 (let ((i 0)
6e9722b0 916 (vec (make-vector 32 nil))
05134257
KH
917 (g1-designation nil)
918 (fl flags))
4ed46869 919 (while (< i 4)
05134257 920 (let ((charset (car fl)))
6e9722b0
KH
921 (if (and no-initial-designation
922 (> i 0)
923 (or (charsetp charset)
924 (and (consp charset)
925 (charsetp (car charset)))))
926 (setq no-initial-designation nil))
927 (if (charsetp charset)
928 (if (= i 1) (setq g1-designation charset))
929 (if (consp charset)
930 (let ((tail charset)
931 elt)
932 (while tail
933 (setq elt (car tail))
d9e3229d
KH
934 (if (eq elt t)
935 (setq no-alternative-designation nil)
936 (if (and elt (not (charsetp elt)))
937 (error "Invalid charset: %s" elt)))
6e9722b0
KH
938 (setq tail (cdr tail)))
939 (setq g1-designation (car charset)))
d9e3229d
KH
940 (if charset
941 (if (eq charset t)
942 (setq no-alternative-designation nil)
943 (error "Invalid charset: %s" charset)))))
4ed46869 944 (aset vec i charset))
05134257
KH
945 (setq fl (cdr fl) i (1+ i)))
946 (while (and (< i 32) fl)
947 (aset vec i (car fl))
e6cddb1a
KH
948 (if (and (= i 16) ; ACCEPT-LATIN-EXTRA-CODE
949 (car fl))
950 (setq accept-latin-extra-code t))
05134257 951 (setq fl (cdr fl) i (1+ i)))
6e9722b0 952 (aset coding-spec 4 vec)
6e9722b0
KH
953 (setq coding-category
954 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
955 (or (and (aref vec 7) 'coding-category-iso-7-else)
956 'coding-category-iso-8-else)
6e9722b0
KH
957 (if (aref vec 7) ; 7-bit only.
958 (if (aref vec 9) ; Use single-shift.
dc64cd19 959 'coding-category-iso-7-else
d9e3229d
KH
960 (if no-alternative-designation
961 'coding-category-iso-7-tight
962 'coding-category-iso-7))
3ad911d8
KH
963 (if (or no-initial-designation
964 (not no-alternative-designation))
dc64cd19 965 'coding-category-iso-8-else
6e9722b0
KH
966 (if (and (charsetp g1-designation)
967 (= (charset-dimension g1-designation) 2))
968 'coding-category-iso-8-2
969 'coding-category-iso-8-1)))))))
970 ((= type 3)
971 (setq coding-category 'coding-category-big5))
972 ((= type 4) ; private
c76b5c99 973 (setq coding-category 'coding-category-ccl)
3bb1accb
KH
974 (if (not (consp flags))
975 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
976 (let ((decoder (check-ccl-program
977 (car flags)
978 (intern (format "%s-decoder" coding-system))))
979 (encoder (check-ccl-program
980 (cdr flags)
981 (intern (format "%s-encoder" coding-system)))))
982 (if (and decoder encoder)
983 (aset coding-spec 4 (cons decoder encoder))
984 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
cd9c3177
KH
985 (t ; i.e. (= type 5)
986 (setq coding-category 'coding-category-raw-text)))
0269ddfb
KH
987
988 (let ((plist (list 'coding-category coding-category
d9e3229d 989 'alias-coding-systems (list coding-system))))
0269ddfb 990 (if no-initial-designation
d9e3229d
KH
991 (plist-put plist 'no-initial-designation t))
992 (if (and properties
993 (or (eq properties t)
994 (not (consp (car properties)))))
995 ;; In the old version, the arg PROPERTIES is a list to be
996 ;; set in PLIST as a value of property `safe-charsets'.
c11a8f77
KH
997 (setq properties (list (cons 'safe-charsets properties))))
998 ;; In the current version PROPERTIES is a property list.
999 ;; Reflect it into PLIST one by one while handling safe-chars
1000 ;; specially.
1001 (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
1002 (safe-chars (cdr (assq 'safe-chars properties)))
1003 (l properties)
1004 prop val)
1005 ;; If only safe-charsets is specified, make a char-table from
1006 ;; it, and store that char-table as the value of `safe-chars'.
1007 (if (and (not safe-chars) safe-charsets)
1008 (let (charset)
1009 (if (eq safe-charsets t)
1010 (setq safe-chars t)
1011 (setq safe-chars (make-char-table 'safe-chars))
1012 (while safe-charsets
1013 (setq charset (car safe-charsets)
1014 safe-charsets (cdr safe-charsets))
1015 (cond ((eq charset 'ascii)) ; just ignore
1016 ((eq charset 'eight-bit-control)
1017 (let ((i 128))
1018 (while (< i 160)
1019 (aset safe-chars i t)
1020 (setq i (1+ i)))))
1021 ((eq charset 'eight-bit-graphic)
1022 (let ((i 160))
1023 (while (< i 256)
1024 (aset safe-chars i t)
1025 (setq i (1+ i)))))
1026 (t
e6cddb1a
KH
1027 (aset safe-chars (make-char charset) t))))
1028 (if accept-latin-extra-code
1029 (let ((i 128))
1030 (while (< i 160)
1031 (if (aref latin-extra-code-table i)
1032 (aset safe-chars i t))
1033 (setq i (1+ i))))))
c11a8f77
KH
1034 (setq l (cons (cons 'safe-chars safe-chars) l))))
1035 (while l
1036 (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
1037 (if (eq prop 'safe-chars)
1038 (progn
cc57cc54
KH
1039 (if (and (symbolp val)
1040 (get val 'translation-table))
1041 (setq safe-chars (get val 'translation-table)))
cc57cc54 1042 (setq val safe-chars)))
c11a8f77 1043 (plist-put plist prop val)))
1b46a680
KH
1044 ;; The property `coding-category' may have been set differently
1045 ;; through PROPERTIES.
1046 (setq coding-category (plist-get plist 'coding-category))
0269ddfb 1047 (aset coding-spec coding-spec-plist-idx plist))
6e9722b0 1048 (put coding-system 'coding-system coding-spec)
c95cf5ac 1049 (put coding-system 'coding-system-define-form nil)
6e9722b0
KH
1050 (put coding-category 'coding-systems
1051 (cons coding-system (get coding-category 'coding-systems))))
4ed46869 1052
1b46a680 1053 ;; Next, set a value of `eol-type' property.
af873cd1 1054 (if (not eol-type)
1b46a680
KH
1055 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
1056 ;; systems, each corresponds to a coding system for the detected
1057 ;; EOL format.
1058 (setq eol-type (make-subsidiary-coding-system coding-system)))
1059 (setq eol-type
1060 (cond ((or (eq eol-type 'unix) (null eol-type))
1061 0)
1062 ((eq eol-type 'dos)
1063 1)
1064 ((eq eol-type 'mac)
1065 2)
1066 ((or (and (vectorp eol-type)
1067 (= (length eol-type) 3))
1068 (and (numberp eol-type)
1069 (and (>= eol-type 0)
1070 (<= eol-type 2))))
1071 eol-type)
1072 (t
1073 (error "Invalid EOL-TYPE spec:%S" eol-type))))
1074 (put coding-system 'eol-type eol-type)
0269ddfb 1075
620956ca
KH
1076 (define-coding-system-internal coding-system)
1077
0269ddfb
KH
1078 ;; At last, register CODING-SYSTEM in `coding-system-list' and
1079 ;; `coding-system-alist'.
857ea15c 1080 (add-to-coding-system-list coding-system)
0269ddfb 1081 (setq coding-system-alist (cons (list (symbol-name coding-system))
d9e3229d 1082 coding-system-alist))
05134257
KH
1083
1084 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
1085 ;; XXX-with-esc variants.
1086 (let ((coding-category (coding-system-category coding-system)))
1087 (if (or (eq coding-category 'coding-category-iso-8-1)
1088 (eq coding-category 'coding-category-iso-8-2))
1089 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
c11a8f77
KH
1090 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
1091 (safe-charsets (assq 'safe-charsets properties))
1092 (mime-charset (assq 'mime-charset properties)))
1093 (if safe-charsets
1094 (setcdr safe-charsets t)
1095 (setq properties (cons (cons 'safe-charsets t) properties)))
1096 (if mime-charset
1097 (setcdr mime-charset nil))
05134257
KH
1098 (make-coding-system esc type mnemonic doc
1099 (if (listp (car flags))
1100 (cons (append (car flags) '(t)) (cdr flags))
1101 (cons (list (car flags) t) (cdr flags)))
c11a8f77 1102 properties))))
05134257 1103
d9e3229d 1104 coding-system)
8057896b 1105
5470d5b6
KH
1106(put 'safe-chars 'char-table-extra-slots 0)
1107
a42763dc 1108(defun define-coding-system-alias (alias coding-system)
358d28fb 1109 "Define ALIAS as an alias for coding system CODING-SYSTEM."
0269ddfb 1110 (put alias 'coding-system (coding-system-spec coding-system))
c95cf5ac 1111 (put alias 'coding-system-define-form nil)
857ea15c 1112 (add-to-coding-system-list alias)
0269ddfb
KH
1113 (setq coding-system-alist (cons (list (symbol-name alias))
1114 coding-system-alist))
1115 (let ((eol-type (coding-system-eol-type coding-system)))
1116 (if (vectorp eol-type)
708fc465
KH
1117 (progn
1118 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
1119 (put alias 'eol-type (make-subsidiary-coding-system alias)))
0269ddfb 1120 (put alias 'eol-type eol-type))))
4ed46869 1121
bbdea948
RS
1122(defun merge-coding-systems (first second)
1123 "Fill in any unspecified aspects of coding system FIRST from SECOND.
1124Return the resulting coding system."
1125 (let ((base (coding-system-base second))
1126 (eol (coding-system-eol-type second)))
1127 ;; If FIRST doesn't specify text conversion, merge with that of SECOND.
1128 (if (eq (coding-system-base first) 'undecided)
1129 (setq first (coding-system-change-text-conversion first base)))
1130 ;; If FIRST doesn't specify eol conversion, merge with that of SECOND.
1131 (if (and (vectorp (coding-system-eol-type first))
1132 (numberp eol) (>= eol 0) (<= eol 2))
1133 (setq first (coding-system-change-eol-conversion
1134 first eol)))
1135 first))
1136
2b96ab33
KH
1137(defun autoload-coding-system (symbol form)
1138 "Define SYMBOL as a coding-system that is defined on demand.
1139
1140FROM is a form to evaluate to define the coding-system."
28849ffc
KH
1141 (put symbol 'coding-system-define-form form)
1142 (setq coding-system-alist (cons (list (symbol-name symbol))
1143 coding-system-alist)))
2b96ab33 1144
14b3fa07 1145(defun set-buffer-file-coding-system (coding-system &optional force nomodify)
358d28fb
RS
1146 "Set the file coding-system of the current buffer to CODING-SYSTEM.
1147This means that when you save the buffer, it will be converted
1148according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
1149use \\[list-coding-systems].
1150
bbdea948
RS
1151If CODING-SYSTEM leaves the text conversion unspecified, or if it
1152leaves the end-of-line conversion unspecified, FORCE controls what to
1153do. If FORCE is nil, get the unspecified aspect (or aspects) from the
1154buffer's previous `buffer-file-coding-system' value (if it is
3b6cc699 1155specified there). Otherwise, leave it unspecified.
aeef8f07
KH
1156
1157This marks the buffer modified so that the succeeding \\[save-buffer]
1158surely saves the buffer with CODING-SYSTEM. From a program, if you
14b3fa07
RS
1159don't want to mark the buffer modified, specify t for NOMODIFY.
1160If you know exactly what coding system you want to use,
1161just set the variable `buffer-file-coding-system' directly."
bbdea948 1162 (interactive "zCoding system for saving file (default, nil): \nP")
4ed46869 1163 (check-coding-system coding-system)
36d455c4 1164 (if (and coding-system buffer-file-coding-system (null force))
bbdea948
RS
1165 (setq coding-system
1166 (merge-coding-systems coding-system buffer-file-coding-system)))
4ed46869 1167 (setq buffer-file-coding-system coding-system)
38a1356d
RS
1168 ;; This is in case of an explicit call. Normally, `normal-mode' and
1169 ;; `set-buffer-major-mode-hook' take care of setting the table.
1170 (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
1171 (ucs-set-table-for-input))
de5ffead
RS
1172 (unless nomodify
1173 (set-buffer-modified-p t))
4ed46869
KH
1174 (force-mode-line-update))
1175
bbdea948
RS
1176(defun revert-buffer-with-coding-system (coding-system &optional force)
1177 "Visit the current buffer's file again using coding system CODING-SYSTEM.
1178For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1179
1180If CODING-SYSTEM leaves the text conversion unspecified, or if it
1181leaves the end-of-line conversion unspecified, FORCE controls what to
1182do. If FORCE is nil, get the unspecified aspect (or aspects) from the
1183buffer's previous `buffer-file-coding-system' value (if it is
1184specified there). Otherwise, determine it from the file contents as
1185usual for visiting a file."
1186 (interactive "zCoding system for visited file (default, nil): \nP")
1187 (check-coding-system coding-system)
1188 (if (and coding-system buffer-file-coding-system (null force))
1189 (setq coding-system
1190 (merge-coding-systems coding-system buffer-file-coding-system)))
1191 (let ((coding-system-for-read coding-system))
1192 (revert-buffer)))
1193
701414e3
KH
1194(defun set-file-name-coding-system (coding-system)
1195 "Set coding system for decoding and encoding file names to CODING-SYSTEM.
1196It actually just set the variable `file-name-coding-system' (which
1197see) to CODING-SYSTEM."
1198 (interactive "zCoding system for file names (default, nil): ")
1199 (check-coding-system coding-system)
43b557d5
KH
1200 (if (and coding-system
1201 (coding-system-get coding-system 'ascii-incompatible))
1202 (error "%s is not ASCII-compatible" coding-system))
701414e3
KH
1203 (setq file-name-coding-system coding-system))
1204
358d28fb
RS
1205(defvar default-terminal-coding-system nil
1206 "Default value for the terminal coding system.
1207This is normally set according to the selected language environment.
1208See also the command `set-terminal-coding-system'.")
1209
df100398
KH
1210(defun set-terminal-coding-system (coding-system)
1211 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
1212All text output to the terminal will be encoded
1213with the specified coding system.
1214For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1215The default is determined by the selected language environment
1216or by the previous use of this command."
1217 (interactive
2e02a76f
RS
1218 (list (let ((default (if (and (not (terminal-coding-system))
1219 default-terminal-coding-system)
1220 default-terminal-coding-system)))
1221 (read-coding-system
1222 (format "Coding system for terminal display (default, %s): "
1223 default)
1224 default))))
358d28fb
RS
1225 (if (and (not coding-system)
1226 (not (terminal-coding-system)))
1227 (setq coding-system default-terminal-coding-system))
1228 (if coding-system
521d4010 1229 (setq default-terminal-coding-system coding-system))
df100398
KH
1230 (set-terminal-coding-system-internal coding-system)
1231 (redraw-frame (selected-frame)))
1232
358d28fb
RS
1233(defvar default-keyboard-coding-system nil
1234 "Default value of the keyboard coding system.
1235This is normally set according to the selected language environment.
1236See also the command `set-keyboard-coding-system'.")
1237
df100398 1238(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
1239 "Set coding system for keyboard input to CODING-SYSTEM.
1240In addition, this command enables Encoded-kbd minor mode.
6d34f495
DL
1241\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off -- see
1242`encoded-kbd-mode'.)
358d28fb
RS
1243For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1244The default is determined by the selected language environment
1245or by the previous use of this command."
1246 (interactive
2e02a76f
RS
1247 (list (let ((default (if (and (not (keyboard-coding-system))
1248 default-keyboard-coding-system)
1249 default-keyboard-coding-system)))
1250 (read-coding-system
1251 (format "Coding system for keyboard input (default, %s): "
1252 default)
1253 default))))
358d28fb
RS
1254 (if (and (not coding-system)
1255 (not (keyboard-coding-system)))
1256 (setq coding-system default-keyboard-coding-system))
1257 (if coding-system
1258 (setq default-keyboard-coding-system coding-system))
43b557d5
KH
1259 (if (and coding-system
1260 (coding-system-get coding-system 'ascii-incompatible))
1261 (error "%s is not ASCII-compatible" coding-system))
df100398 1262 (set-keyboard-coding-system-internal coding-system)
b23bad0b 1263 (setq keyboard-coding-system coding-system)
df100398
KH
1264 (encoded-kbd-mode (if coding-system 1 0)))
1265
6d34f495
DL
1266(defcustom keyboard-coding-system nil
1267 "Specify coding system for keyboard input.
1268If you set this on a terminal which can't distinguish Meta keys from
12698-bit characters, you will have to use ESC to type Meta characters.
1270See Info node `Specify Coding' and Info node `Single-Byte Character Support'.
1271
237e5993
DL
1272On non-windowing terminals, this is set from the locale by default.
1273
6d34f495 1274Setting this variable directly does not take effect;
c4131d96 1275use either \\[customize] or \\[set-keyboard-coding-system]."
6d34f495
DL
1276 :type '(coding-system :tag "Coding system")
1277 :link '(info-link "(emacs)Specify Coding")
1278 :link '(info-link "(emacs)Single-Byte Character Support")
1279 :set (lambda (symbol value)
1280 ;; Don't load encoded-kbd-mode unnecessarily.
1281 (if (or value (boundp 'encoded-kbd-mode))
1282 (set-keyboard-coding-system value)
1283 (set-default 'keyboard-coding-system nil))) ; must initialize
bf247b6e 1284 :version "22.1"
6d34f495
DL
1285 :group 'keyboard
1286 :group 'mule)
1287
df100398 1288(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 1289 "Set coding systems for the process associated with the current buffer.
df100398 1290DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
1291ENCODING is the coding system to be used to encode output to the process.
1292
1293For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869 1294 (interactive
83911021 1295 "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
4ed46869
KH
1296 (let ((proc (get-buffer-process (current-buffer))))
1297 (if (null proc)
521d4010 1298 (error "No process")
df100398
KH
1299 (check-coding-system decoding)
1300 (check-coding-system encoding)
1301 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
1302 (force-mode-line-update))
1303
d0b99881
RS
1304(defalias 'set-clipboard-coding-system 'set-selection-coding-system)
1305
14915c37 1306(defun set-selection-coding-system (coding-system)
8c52d564 1307 "Make CODING-SYSTEM used for communicating with other X clients.
b25eef20
KH
1308When sending or receiving text via cut_buffer, selection, and clipboard,
1309the text is encoded or decoded by CODING-SYSTEM."
a03b3ce1 1310 (interactive "zCoding system for X selection: ")
b25eef20 1311 (check-coding-system coding-system)
14915c37 1312 (setq selection-coding-system coding-system))
b25eef20 1313
e8dd0160 1314;; Coding system lastly specified by the command
a03b3ce1
KH
1315;; set-next-selection-coding-system.
1316(defvar last-next-selection-coding-system nil)
1317
1318(defun set-next-selection-coding-system (coding-system)
1319 "Make CODING-SYSTEM used for the next communication with other X clients.
1320This setting is effective for the next communication only."
1321 (interactive
1322 (list (read-coding-system
1323 (if last-next-selection-coding-system
1324 (format "Coding system for the next X selection (default, %S): "
1325 last-next-selection-coding-system)
1326 "Coding system for the next X selection: ")
1327 last-next-selection-coding-system)))
1328 (if coding-system
1329 (setq last-next-selection-coding-system coding-system)
1330 (setq coding-system last-next-selection-coding-system))
1331 (check-coding-system coding-system)
1332
1333 (setq next-selection-coding-system coding-system))
1334
4ed46869 1335(defun set-coding-priority (arg)
521d4010
DL
1336 "Set priority of coding categories according to ARG.
1337ARG is a list of coding categories ordered by priority."
d9e3229d
KH
1338 (let ((l arg)
1339 (current-list (copy-sequence coding-category-list)))
e8dd0160 1340 ;; Check the validity of ARG while deleting coding categories in
d9e3229d
KH
1341 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
1342 ;; contains all coding categories.
1343 (while l
1344 (if (or (null (get (car l) 'coding-category-index))
1345 (null (memq (car l) current-list)))
1346 (error "Invalid or duplicated element in argument: %s" arg))
1347 (setq current-list (delq (car l) current-list))
1348 (setq l (cdr l)))
4ed46869 1349 ;; Update `coding-category-list' and return it.
2feaf204
KH
1350 (setq coding-category-list (append arg current-list))
1351 (set-coding-priority-internal)))
4ed46869 1352
835cbadb
EZ
1353;;; X selections
1354
cc926903 1355(defvar ctext-non-standard-encodings-alist
6c805023
KH
1356 '(("big5-0" big5 2 (chinese-big5-1 chinese-big5-2))
1357 ("ISO8859-14" iso-8859-14 1 latin-iso8859-14)
1358 ("ISO8859-15" iso-8859-15 1 latin-iso8859-15))
1359 "Alist of non-standard encoding names vs the corresponding usages in CTEXT.
1360
1361It controls how extended segments of a compound text are handled
1362by the coding system `compound-text-with-extensions'.
1363
1364Each element has the form (ENCODING-NAME CODING-SYSTEM N-OCTET CHARSET).
1365
1366ENCODING-NAME is an encoding name of an \"extended segments\".
1367
1368CODING-SYSTEM is the coding-system to encode (or decode) the
1369characters into (or from) the extended segment.
1370
1371N-OCTET is the number of octets (bytes) that encodes a character
1372in the segment. It can be 0 (meaning the number of octets per
1373character is variable), 1, 2, 3, or 4.
1374
1375CHARSET is a charater set containing characters that are encoded
1376in the segment. It can be a list of character sets. It can also
1377be a char-table, in which case characters that have non-nil value
1378in the char-table are the target.
1379
1380On decoding CTEXT, all encoding names listed here are recognized.
1381
1382On encoding CTEXT, encoding names in the variable
1383`ctext-non-standard-encodings' (which see) and in the information
1384listed for the current language environment under the key
1385`ctext-non-standard-encodings' are used.")
1386
1387(defvar ctext-non-standard-encodings
1388 '("big5-0")
1389 "List of non-standard encoding names used in extended segments of CTEXT.
1390Each element must be one of the names listed in the variable
1391`ctext-non-standard-encodings-alist' (which see).")
cc926903
KH
1392
1393(defvar ctext-non-standard-encodings-regexp
1394 (string-to-multibyte
1395 (concat
1396 ;; For non-standard encodings.
1397 "\\(\e%/[0-4][\200-\377][\200-\377]\\([^\002]+\\)\002\\)"
1398 "\\|"
1399 ;; For UTF-8 encoding.
1400 "\\(\e%G[^\e]*\e%@\\)")))
835cbadb
EZ
1401
1402;; Functions to support "Non-Standard Character Set Encodings" defined
6c805023
KH
1403;; by the COMPOUND-TEXT spec. They also support "The UTF-8 encoding"
1404;; described in the section 7 of the documentation of COMPOUND-TEXT
1405;; distributed with XFree86.
5c88a01e 1406
835cbadb
EZ
1407(defun ctext-post-read-conversion (len)
1408 "Decode LEN characters encoded as Compound Text with Extended Segments."
835cbadb 1409 (save-match-data
cc926903
KH
1410 (save-restriction
1411 (let ((case-fold-search nil)
1412 (in-workbuf (string= (buffer-name) " *code-converting-work*"))
1413 last-coding-system-used
1414 pos bytes)
1415 (or in-workbuf
1416 (narrow-to-region (point) (+ (point) len)))
cc926903
KH
1417 (if in-workbuf
1418 (set-buffer-multibyte t))
1419 (while (re-search-forward ctext-non-standard-encodings-regexp
1420 nil 'move)
1421 (setq pos (match-beginning 0))
1422 (if (match-beginning 1)
1423 ;; ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
1424 (let* ((M (char-after (+ pos 4)))
1425 (L (char-after (+ pos 5)))
1426 (encoding (match-string 2))
9857367f 1427 (encoding-info (assoc-string
6c805023 1428 encoding
c12bc1fb 1429 ctext-non-standard-encodings-alist t))
6c805023
KH
1430 (coding (if encoding-info
1431 (nth 1 encoding-info)
1432 (setq encoding (intern (downcase encoding)))
1433 (and (coding-system-p encoding)
1434 encoding))))
cc926903
KH
1435 (setq bytes (- (+ (* (- M 128) 128) (- L 128))
1436 (- (point) (+ pos 6))))
1437 (when coding
1438 (delete-region pos (point))
1439 (forward-char bytes)
1440 (decode-coding-region (- (point) bytes) (point) coding)))
1441 ;; ESC % G --UTF-8-BYTES-- ESC % @
6c805023
KH
1442 (delete-char -3)
1443 (delete-region pos (+ pos 3))
1444 (decode-coding-region pos (point) 'utf-8))))
cc926903
KH
1445 (goto-char (point-min))
1446 (- (point-max) (point)))))
835cbadb 1447
6c805023
KH
1448;; Return a char table of extended segment usage for each character.
1449;; Each value of the char table is nil, one of the elements of
1450;; `ctext-non-standard-encodings-alist', or the symbol `utf-8'.
1451
1452(defun ctext-non-standard-encodings-table ()
1453 (let ((table (make-char-table 'translation-table)))
1454 (aset table (make-char 'mule-unicode-0100-24ff) 'utf-8)
1455 (aset table (make-char 'mule-unicode-2500-33ff) 'utf-8)
1456 (aset table (make-char 'mule-unicode-e000-ffff) 'utf-8)
1457 (dolist (encoding (reverse
1458 (append
1459 (get-language-info current-language-environment
1460 'ctext-non-standard-encodings)
1461 ctext-non-standard-encodings)))
1462 (let* ((slot (assoc encoding ctext-non-standard-encodings-alist))
1463 (charset (nth 3 slot)))
1464 (if charset
1465 (cond ((charsetp charset)
1466 (aset table (make-char charset) slot))
1467 ((listp charset)
1468 (dolist (elt charset)
1469 (aset table (make-char elt) slot)))
1470 ((char-table-p charset)
9857367f 1471 (map-char-table #'(lambda (k v)
6c805023
KH
1472 (if (and v (> k 128)) (aset table k slot)))
1473 charset))))))
1474 table))
835cbadb
EZ
1475
1476(defun ctext-pre-write-conversion (from to)
5dde3c71
EZ
1477 "Encode characters between FROM and TO as Compound Text w/Extended Segments.
1478
1479If FROM is a string, or if the current buffer is not the one set up for us
cc926903 1480by encode-coding-string, generate a new temp buffer, insert the
5dde3c71 1481text, and convert it in the temporary buffer. Otherwise, convert in-place."
835cbadb 1482 (save-match-data
002dc239
KH
1483 (let ((workbuf (get-buffer-create " *code-conversion-work*")))
1484 ;; Setup a working buffer if necessary.
1485 (cond ((stringp from)
1486 (set-buffer workbuf)
1487 (erase-buffer)
cc926903 1488 (set-buffer-multibyte (multibyte-string-p from))
002dc239
KH
1489 (insert from))
1490 ((not (eq (current-buffer) workbuf))
1491 (let ((buf (current-buffer))
1492 (multibyte enable-multibyte-characters))
1493 (set-buffer workbuf)
1494 (erase-buffer)
1495 (set-buffer-multibyte multibyte)
1496 (insert-buffer-substring buf from to)))))
cc926903
KH
1497
1498 ;; Now we can encode the whole buffer.
6c805023 1499 (let ((encoding-table (ctext-non-standard-encodings-table))
cc926903 1500 last-coding-system-used
6c805023
KH
1501 last-pos last-encoding-info
1502 encoding-info end-pos)
1503 (goto-char (setq last-pos (point-min)))
1504 (setq end-pos (point-marker))
1505 (while (re-search-forward "[^\000-\177]+" nil t)
1506 ;; Found a sequence of non-ASCII characters.
1507 (setq last-pos (match-beginning 0)
1508 last-encoding-info (aref encoding-table (char-after last-pos)))
1509 (set-marker end-pos (match-end 0))
1510 (goto-char (1+ last-pos))
1511 (catch 'tag
1512 (while t
1513 (setq encoding-info
1514 (if (< (point) end-pos)
1515 (aref encoding-table (following-char))))
1516 (unless (eq last-encoding-info encoding-info)
1517 (cond ((consp last-encoding-info)
1518 ;; Encode the previous range using an extended
1519 ;; segment.
1520 (let ((encoding-name (car last-encoding-info))
1521 (coding-system (nth 1 last-encoding-info))
1522 (noctets (nth 2 last-encoding-info))
1523 len)
1524 (encode-coding-region last-pos (point) coding-system)
1525 (setq len (+ (length encoding-name) 1
1526 (- (point) last-pos)))
1527 (save-excursion
1528 (goto-char last-pos)
9857367f 1529 (insert (string-to-multibyte
6c805023
KH
1530 (format "\e%%/%d%c%c%s\ 2"
1531 noctets
1532 (+ (/ len 128) 128)
1533 (+ (% len 128) 128)
1534 encoding-name))))))
1535 ((eq last-encoding-info 'utf-8)
1536 ;; Encode the previous range using UTF-8 encoding
1537 ;; extention.
1538 (encode-coding-region last-pos (point) 'mule-utf-8)
1539 (save-excursion
1540 (goto-char last-pos)
1541 (insert "\e%G"))
1542 (insert "\e%@")))
1543 (setq last-pos (point)
1544 last-encoding-info encoding-info))
1545 (if (< (point) end-pos)
1546 (forward-char 1)
1547 (throw 'tag nil)))))
1548 (set-marker end-pos nil)
cc926903 1549 (goto-char (point-min))))
5dde3c71 1550 ;; Must return nil, as build_annotations_2 expects that.
835cbadb
EZ
1551 nil)
1552
4ed46869
KH
1553;;; FILE I/O
1554
e76938e7 1555(defcustom auto-coding-alist
d8df8af1
EZ
1556 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|[jew]ar\\|xpi\\)\\'" . no-conversion)
1557 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|[JEW]AR\\|XPI\\)\\'" . no-conversion)
e3668698 1558 ("\\.\\(sx[dmicw]\\|tar\\|tgz\\)\\'" . no-conversion)
6caef2da 1559 ("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion)
ba40634b 1560 ("\\.\\(jpe?g\\|png\\|gif\\|tiff?\\|p[bpgn]m\\)\\'" . no-conversion)
45885400 1561 ("/#[^/]+#\\'" . emacs-mule))
835f49b8
KH
1562 "Alist of filename patterns vs corresponding coding systems.
1563Each element looks like (REGEXP . CODING-SYSTEM).
558b0c86 1564A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
835f49b8 1565
7fed493a
RS
1566The settings in this alist take priority over `coding:' tags
1567in the file (see the function `set-auto-coding')
e76938e7
DL
1568and the contents of `file-coding-system-alist'."
1569 :group 'files
1570 :group 'mule
1571 :type '(repeat (cons (regexp :tag "File name regexp")
1572 (symbol :tag "Coding system"))))
835f49b8 1573
502522b2
GM
1574(defcustom auto-coding-regexp-alist
1575 '(("^BABYL OPTIONS:[ \t]*-\\*-[ \t]*rmail[ \t]*-\\*-" . no-conversion))
1576 "Alist of patterns vs corresponding coding systems.
1577Each element looks like (REGEXP . CODING-SYSTEM).
1578A file whose first bytes match REGEXP is decoded by CODING-SYSTEM on reading.
1579
1580The settings in this alist take priority over `coding:' tags
1581in the file (see the function `set-auto-coding')
1582and the contents of `file-coding-system-alist'."
1583 :group 'files
1584 :group 'mule
1585 :type '(repeat (cons (regexp :tag "Regexp")
1586 (symbol :tag "Coding system"))))
1587
d9f6dfe6 1588;; See the bottom of this file for built-in auto coding functions.
447404a3
CW
1589(defcustom auto-coding-functions '(sgml-xml-auto-coding-function
1590 sgml-html-meta-auto-coding-function)
d9f6dfe6
CW
1591 "A list of functions which attempt to determine a coding system.
1592
66643502
RS
1593Each function in this list should be written to operate on the
1594current buffer, but should not modify it in any way. The buffer
1595will contain undecoded text of parts of the file. Each function
1596should take one argument, SIZE, which says how many
1597characters (starting from point) it should look at.
1598
1599If one of these functions succeeds in determining a coding
1600system, it should return that coding system. Otherwise, it
1601should return nil.
1602
1603If a file has a `coding:' tag, that takes precedence over these
1604functions, so they won't be called at all."
d9f6dfe6
CW
1605 :group 'files
1606 :group 'mule
1607 :type '(repeat function))
1608
1c4cc63a
KH
1609(defvar set-auto-coding-for-load nil
1610 "Non-nil means look for `load-coding' property instead of `coding'.
1611This is used for loading and byte-compiling Emacs Lisp files.")
1612
8a592131
RS
1613(defun auto-coding-alist-lookup (filename)
1614 "Return the coding system specified by `auto-coding-alist' for FILENAME."
1615 (let ((alist auto-coding-alist)
c60ee5e7 1616 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos cygwin)))
8a592131
RS
1617 coding-system)
1618 (while (and alist (not coding-system))
1619 (if (string-match (car (car alist)) filename)
1620 (setq coding-system (cdr (car alist)))
1621 (setq alist (cdr alist))))
1622 coding-system))
1623
835f49b8
KH
1624(defun set-auto-coding (filename size)
1625 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1c4cc63a
KH
1626These bytes should include at least the first 1k of the file
1627and the last 3k of the file, but the middle may be omitted.
63561304 1628
d21ba5e0
DL
1629The function checks FILENAME against the variable `auto-coding-alist'.
1630If FILENAME doesn't match any entries in the variable, it checks the
502522b2 1631contents of the current buffer following point against
447404a3 1632`auto-coding-regexp-alist'. If no match is found, it checks for a
502522b2 1633`coding:' tag in the first one or two lines following point. If no
d21ba5e0 1634`coding:' tag is found, it checks any local variables list in the last
447404a3 16353K bytes out of the SIZE bytes. Finally, if none of these methods
d21ba5e0
DL
1636succeed, it checks to see if any function in `auto-coding-functions'
1637gives a match.
63561304 1638
d21ba5e0
DL
1639The return value is the specified coding system, or nil if nothing is
1640specified.
87aba788 1641
ba74e833 1642The variable `set-auto-coding-function' (which see) is set to this
87aba788 1643function by default."
502522b2 1644 (or (auto-coding-alist-lookup filename)
447404a3
CW
1645 ;; Try using `auto-coding-regexp-alist'.
1646 (save-excursion
1647 (let ((alist auto-coding-regexp-alist)
1648 coding-system)
1649 (while (and alist (not coding-system))
1650 (let ((regexp (car (car alist))))
1651 (when (re-search-forward regexp (+ (point) size) t)
1652 (setq coding-system (cdr (car alist)))))
0bca779a 1653 (setq alist (cdr alist)))
447404a3 1654 coding-system))
502522b2
GM
1655 (let* ((case-fold-search t)
1656 (head-start (point))
1657 (head-end (+ head-start (min size 1024)))
1658 (tail-start (+ head-start (max (- size 3072) 0)))
1659 (tail-end (+ head-start size))
1660 coding-system head-found tail-found pos)
1661 ;; Try a short cut by searching for the string "coding:"
1662 ;; and for "unibyte:" at the head and tail of SIZE bytes.
1663 (setq head-found (or (search-forward "coding:" head-end t)
1664 (search-forward "unibyte:" head-end t)))
1665 (if (and head-found (> head-found tail-start))
1666 ;; Head and tail are overlapped.
1667 (setq tail-found head-found)
1668 (goto-char tail-start)
1669 (setq tail-found (or (search-forward "coding:" tail-end t)
1670 (search-forward "unibyte:" tail-end t))))
1671
1672 ;; At first check the head.
1673 (when head-found
1674 (goto-char head-start)
6b66d028
RS
1675 (setq head-end (set-auto-mode-1))
1676 (setq head-start (point))
1d8e9a7c 1677 (when (and head-end (< head-found head-end))
835f49b8 1678 (goto-char head-start)
502522b2
GM
1679 (when (and set-auto-coding-for-load
1680 (re-search-forward
6b66d028 1681 "\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
502522b2
GM
1682 head-end t))
1683 (setq coding-system 'raw-text))
1684 (when (and (not coding-system)
1685 (re-search-forward
6b66d028 1686 "\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
502522b2
GM
1687 head-end t))
1688 (setq coding-system (intern (match-string 2)))
1689 (or (coding-system-p coding-system)
1690 (setq coding-system nil)))))
1691
1692 ;; If no coding: tag in the head, check the tail.
c251286b
KH
1693 ;; Here we must pay attention to the case that the end-of-line
1694 ;; is just "\r" and we can't use "^" nor "$" in regexp.
502522b2
GM
1695 (when (and tail-found (not coding-system))
1696 (goto-char tail-start)
c251286b 1697 (re-search-forward "[\r\n]\^L" nil t)
502522b2 1698 (if (re-search-forward
9857367f 1699 "[\r\n]\\([^[\r\n]*\\)[ \t]*Local Variables:[ \t]*\\([^\r\n]*\\)[\r\n]"
c251286b
KH
1700 tail-end t)
1701 ;; The prefix is what comes before "local variables:" in its
1702 ;; line. The suffix is what comes after "local variables:"
502522b2
GM
1703 ;; in its line.
1704 (let* ((prefix (regexp-quote (match-string 1)))
1705 (suffix (regexp-quote (match-string 2)))
1706 (re-coding
1707 (concat
c251286b 1708 "[\r\n]" prefix
cfe98f50
GM
1709 ;; N.B. without the \n below, the regexp can
1710 ;; eat newlines.
c251286b
KH
1711 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
1712 suffix "[\r\n]"))
502522b2
GM
1713 (re-unibyte
1714 (concat
c251286b
KH
1715 "[\r\n]" prefix
1716 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
1717 suffix "[\r\n]"))
502522b2 1718 (re-end
9857367f 1719 (concat "[\r\n]" prefix "[ \t]*End *:[ \t]*" suffix
c251286b
KH
1720 "[\r\n]?"))
1721 (pos (1- (point))))
1722 (forward-char -1) ; skip back \r or \n.
502522b2
GM
1723 (re-search-forward re-end tail-end 'move)
1724 (setq tail-end (point))
1725 (goto-char pos)
1726 (when (and set-auto-coding-for-load
1727 (re-search-forward re-unibyte tail-end t))
1728 (setq coding-system 'raw-text))
1729 (when (and (not coding-system)
1730 (re-search-forward re-coding tail-end t))
1731 (setq coding-system (intern (match-string 1)))
1732 (or (coding-system-p coding-system)
1733 (setq coding-system nil))))))
447404a3
CW
1734 coding-system)
1735 ;; Finally, try all the `auto-coding-functions'.
1736 (let ((funcs auto-coding-functions)
1737 (coding-system nil))
1738 (while (and funcs (not coding-system))
1739 (setq coding-system (condition-case e
1740 (save-excursion
1741 (goto-char (point-min))
1742 (funcall (pop funcs) size))
1743 (error nil))))
502522b2 1744 coding-system)))
63561304
KH
1745
1746(setq set-auto-coding-function 'set-auto-coding)
87aba788 1747
0436cc1b
KH
1748;; This variable is set in these two cases:
1749;; (1) A file is read by a coding system specified explicitly.
1750;; after-insert-file-set-coding sets this value to
1751;; coding-system-for-read.
1752;; (2) A buffer is saved.
1753;; After writing, basic-save-buffer-1 sets this value to
1754;; last-coding-system-used.
1755;; This variable is used for decoding in revert-buffer.
e205f8c1 1756(defvar buffer-file-coding-system-explicit nil
0436cc1b
KH
1757 "The file coding system explicitly specified for the current buffer.
1758Internal use only.")
e205f8c1
KH
1759(make-variable-buffer-local 'buffer-file-coding-system-explicit)
1760(put 'buffer-file-coding-system-explicit 'permanent-local t)
0436cc1b
KH
1761
1762(defun after-insert-file-set-coding (inserted &optional visit)
872a0a6f
RS
1763 "Set `buffer-file-coding-system' of current buffer after text is inserted.
1764INSERTED is the number of characters that were inserted, as figured
1765in the situation before this function. Return the number of characters
1766inserted, as figured in the situation after. The two numbers can be
0436cc1b
KH
1767different if the buffer has become unibyte.
1768The optional second arg VISIT non-nil means that we are visiting a file."
1769 (if (and visit
1770 coding-system-for-read
1771 (not (eq coding-system-for-read 'auto-save-coding)))
e205f8c1 1772 (setq buffer-file-coding-system-explicit coding-system-for-read))
4ed46869
KH
1773 (if last-coding-system-used
1774 (let ((coding-system
1775 (find-new-buffer-file-coding-system last-coding-system-used))
1776 (modified-p (buffer-modified-p)))
0269ddfb 1777 (when coding-system
14b3fa07
RS
1778 ;; Tell set-buffer-file-coding-system not to mark the file
1779 ;; as modified; we just read it, and it's supposed to be unmodified.
1780 ;; Marking it modified would try to lock it, which would
1781 ;; check the modtime, and we don't want to do that again now.
1782 (set-buffer-file-coding-system coding-system t t)
d0c26c63
KH
1783 (if (and enable-multibyte-characters
1784 (or (eq coding-system 'no-conversion)
ccb77b4e 1785 (eq (coding-system-type coding-system) 5))
136e48e4
KH
1786 ;; If buffer was unmodified and the size is the
1787 ;; same as INSERTED, we must be visiting it.
1788 (not modified-p)
1789 (= (buffer-size) inserted))
ccb77b4e
RS
1790 ;; For coding systems no-conversion and raw-text...,
1791 ;; edit the buffer as unibyte.
14b3fa07
RS
1792 (let ((pos-marker (copy-marker (+ (point) inserted)))
1793 ;; Prevent locking.
1794 (buffer-file-name nil))
d0c26c63 1795 (set-buffer-multibyte nil)
872a0a6f 1796 (setq inserted (- pos-marker (point)))))
0269ddfb 1797 (set-buffer-modified-p modified-p))))
d0c26c63 1798 inserted)
4ed46869 1799
8057896b 1800;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
1801;; independently in the following order.
1802;; 1. That of buffer-file-coding-system locally bound.
1803;; 2. That of CODING.
1804
1805(defun find-new-buffer-file-coding-system (coding)
1806 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
1807The local variable `buffer-file-coding-system' of the current buffer
1808is set to the returned value.
509064c5 1809Return nil if there's no need to set `buffer-file-coding-system'."
4ed46869 1810 (let (local-coding local-eol
b685f8d6 1811 found-coding found-eol
4ed46869
KH
1812 new-coding new-eol)
1813 (if (null coding)
1814 ;; Nothing found about coding.
1815 nil
1816
b685f8d6
RS
1817 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1818 ;; and LOCAL-CODING.
1819 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1820 (if (null (numberp local-eol))
1821 ;; But eol-type is not yet set.
1822 (setq local-eol nil))
0269ddfb
KH
1823 (if (and buffer-file-coding-system
1824 (not (eq (coding-system-type buffer-file-coding-system) t)))
1825 ;; This is not `undecided'.
1826 (setq local-coding (coding-system-base buffer-file-coding-system)))
b685f8d6
RS
1827
1828 (if (and (local-variable-p 'buffer-file-coding-system)
1829 local-eol local-coding)
4ed46869
KH
1830 ;; The current buffer has already set full coding-system, we
1831 ;; had better not change it.
1832 nil
1833
8057896b 1834 (setq found-eol (coding-system-eol-type coding))
4ed46869 1835 (if (null (numberp found-eol))
be02cd54
EZ
1836 ;; But eol-type is not found.
1837 ;; If EOL conversions are inhibited, force unix eol-type.
1838 (setq found-eol (if inhibit-eol-conversion 0)))
c76b5c99
KH
1839 (if (eq (coding-system-type coding) t)
1840 (setq found-coding 'undecided)
1841 (setq found-coding (coding-system-base coding)))
1842
1843 (if (and (not found-eol) (eq found-coding 'undecided))
1844 ;; No valid coding information found.
1845 nil
1846
1847 ;; Some coding information (eol or text) found.
1848
1849 ;; The local setting takes precedence over the found one.
1850 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1851 (or local-coding found-coding)
1852 (or found-coding local-coding)))
1853 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1854 (or local-eol found-eol)
1855 (or found-eol local-eol)))
1856
1857 (let ((eol-type (coding-system-eol-type new-coding)))
1858 (if (and (numberp new-eol) (vectorp eol-type))
1859 (aref eol-type new-eol)
1860 new-coding)))))))
4ed46869 1861
fe831d33
GV
1862(defun modify-coding-system-alist (target-type regexp coding-system)
1863 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
1864There are three of such tables, `file-coding-system-alist',
1865`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
1866
1867TARGET-TYPE specifies which of them to modify.
8c453b46
RS
1868If it is `file', it affects `file-coding-system-alist' (which see).
1869If it is `process', it affects `process-coding-system-alist' (which see).
e8dd0160 1870If it is `network', it affects `network-coding-system-alist' (which see).
fe831d33
GV
1871
1872REGEXP is a regular expression matching a target of I/O operation.
1873The target is a file name if TARGET-TYPE is `file', a program name if
1874TARGET-TYPE is `process', or a network service name or a port number
1875to connect to if TARGET-TYPE is `network'.
1876
1877CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
1878operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1879for decoding and encoding respectively,
1880or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
1881 (or (memq target-type '(file process network))
1882 (error "Invalid target type: %s" target-type))
1883 (or (stringp regexp)
1884 (and (eq target-type 'network) (integerp regexp))
1885 (error "Invalid regular expression: %s" regexp))
1886 (if (symbolp coding-system)
1887 (if (not (fboundp coding-system))
1888 (progn
1889 (check-coding-system coding-system)
1890 (setq coding-system (cons coding-system coding-system))))
1891 (check-coding-system (car coding-system))
1892 (check-coding-system (cdr coding-system)))
1893 (cond ((eq target-type 'file)
1894 (let ((slot (assoc regexp file-coding-system-alist)))
1895 (if slot
1896 (setcdr slot coding-system)
1897 (setq file-coding-system-alist
1898 (cons (cons regexp coding-system)
1899 file-coding-system-alist)))))
1900 ((eq target-type 'process)
1901 (let ((slot (assoc regexp process-coding-system-alist)))
1902 (if slot
1903 (setcdr slot coding-system)
1904 (setq process-coding-system-alist
1905 (cons (cons regexp coding-system)
1906 process-coding-system-alist)))))
1907 (t
1908 (let ((slot (assoc regexp network-coding-system-alist)))
1909 (if slot
1910 (setcdr slot coding-system)
1911 (setq network-coding-system-alist
1912 (cons (cons regexp coding-system)
1913 network-coding-system-alist)))))))
1914
db046b7d
KH
1915(defun decode-coding-inserted-region (from to filename
1916 &optional visit beg end replace)
f29387e8 1917 "Decode the region between FROM and TO as if it is read from file FILENAME.
9c848353 1918The idea is that the text between FROM and TO was just inserted somehow.
f29387e8 1919Optional arguments VISIT, BEG, END, and REPLACE are the same as those
9c848353
RS
1920of the function `insert-file-contents'.
1921Part of the job of this function is setting `buffer-undo-list' appropriately."
f29387e8
KH
1922 (save-excursion
1923 (save-restriction
9c848353
RS
1924 (let ((coding coding-system-for-read)
1925 undo-list-saved)
1926 (if visit
1927 ;; Temporarily turn off undo recording, if we're decoding the
1928 ;; text of a visited file.
1929 (setq buffer-undo-list t)
1930 ;; Otherwise, if we can recognize the undo elt for the insertion,
1931 ;; remove it and get ready to replace it later.
1932 ;; In the mean time, turn off undo recording.
bf247b6e 1933 (let ((last (car-safe buffer-undo-list)))
9c848353
RS
1934 (if (and (consp last) (eql (car last) from) (eql (cdr last) to))
1935 (setq undo-list-saved (cdr buffer-undo-list)
1936 buffer-undo-list t))))
1937 (narrow-to-region from to)
1938 (goto-char (point-min))
f29387e8
KH
1939 (or coding
1940 (setq coding (funcall set-auto-coding-function
1941 filename (- (point-max) (point-min)))))
1942 (or coding
b69ff51b
KH
1943 (setq coding (car (find-operation-coding-system
1944 'insert-file-contents
1945 filename visit beg end replace))))
f29387e8
KH
1946 (if (coding-system-p coding)
1947 (or enable-multibyte-characters
1948 (setq coding
1949 (coding-system-change-text-conversion coding 'raw-text)))
1950 (setq coding nil))
1951 (if coding
b12e19b2 1952 (decode-coding-region (point-min) (point-max) coding)
9c848353
RS
1953 (setq last-coding-system-used coding))
1954 ;; If we're decoding the text of a visited file,
1955 ;; the undo list should start out empty.
1956 (if visit
1957 (setq buffer-undo-list nil)
1958 ;; If we decided to replace the undo entry for the insertion,
1959 ;; do so now.
1960 (if undo-list-saved
1961 (setq buffer-undo-list
1962 (cons (cons from (point-max)) undo-list-saved))))))))
f29387e8 1963
27a91cf7
KH
1964(defun recode-region (start end new-coding coding)
1965 "Re-decode the region (previously decoded by CODING) by NEW-CODING."
1966 (interactive
1967 (list (region-beginning) (region-end)
1968 (read-coding-system "Text was really in: ")
1969 (let ((coding (or buffer-file-coding-system last-coding-system-used)))
1970 (read-coding-system
1971 (concat "But was interpreted as"
1972 (if coding (format " (default %S): " coding) ": "))
1973 coding))))
1974 (or (and new-coding coding)
1975 (error "Coding system not specified"))
1976 ;; Check it before we encode the region.
1977 (check-coding-system new-coding)
1978 (save-restriction
1979 (narrow-to-region start end)
1980 (encode-coding-region (point-min) (point-max) coding)
1981 (decode-coding-region (point-min) (point-max) new-coding)))
1982
b25eef20 1983(defun make-translation-table (&rest args)
a284eea3 1984 "Make a translation table from arguments.
d38b07f9 1985A translation table is a char table intended for character
a284eea3
DL
1986translation in CCL programs.
1987
d38b07f9 1988Each argument is a list of elements of the form (FROM . TO), where FROM
a284eea3 1989is a character to be translated to TO.
13d5617d 1990
452fdb31 1991FROM can be a generic character (see `make-char'). In this case, TO is
d38b07f9 1992a generic character containing the same number of characters, or an
452fdb31 1993ordinary character. If FROM and TO are both generic characters, all
b25eef20 1994characters belonging to FROM are translated to characters belonging to TO
4e003d37
KH
1995without changing their position code(s).
1996
1997The arguments and forms in each argument are processed in the given
1998order, and if a previous form already translates TO to some other
1999character, say TO-ALT, FROM is also translated to TO-ALT."
f967223b 2000 (let ((table (make-char-table 'translation-table))
a73a8c89
KH
2001 revlist)
2002 (while args
2003 (let ((elts (car args)))
2004 (while elts
13d5617d
KH
2005 (let* ((from (car (car elts)))
2006 (from-i 0) ; degree of freedom of FROM
2007 (from-rev (nreverse (split-char from)))
2008 (to (cdr (car elts)))
2009 (to-i 0) ; degree of freedom of TO
2010 (to-rev (nreverse (split-char to))))
2011 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
2012 (while (eq (car from-rev) 0)
2013 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
2014 (while (eq (car to-rev) 0)
2015 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
2016 (if (and (/= from-i to-i) (/= to-i 0))
2017 (error "Invalid character pair (%d . %d)" from to))
b25eef20
KH
2018 ;; If we have already translated TO to TO-ALT, FROM should
2019 ;; also be translated to TO-ALT. But, this is only if TO
2020 ;; is a generic character or TO-ALT is not a generic
13d5617d
KH
2021 ;; character.
2022 (let ((to-alt (aref table to)))
2023 (if (and to-alt
2024 (or (> to-i 0) (not (generic-char-p to-alt))))
2025 (setq to to-alt)))
2026 (if (> from-i 0)
2027 (set-char-table-default table from to)
2028 (aset table from to))
b25eef20
KH
2029 ;; If we have already translated some chars to FROM, they
2030 ;; should also be translated to TO.
a73a8c89
KH
2031 (let ((l (assq from revlist)))
2032 (if l
2033 (let ((ch (car l)))
2034 (setcar l to)
2035 (setq l (cdr l))
2036 (while l
2037 (aset table ch to)
2038 (setq l (cdr l)) ))))
2039 ;; Now update REVLIST.
2040 (let ((l (assq to revlist)))
2041 (if l
2042 (setcdr l (cons from (cdr l)))
2043 (setq revlist (cons (list to from) revlist)))))
2044 (setq elts (cdr elts))))
2045 (setq args (cdr args)))
2046 ;; Return TABLE just created.
2047 table))
2048
c76b5c99
KH
2049(defun make-translation-table-from-vector (vec)
2050 "Make translation table from decoding vector VEC.
9e3b6057
DL
2051VEC is an array of 256 elements to map unibyte codes to multibyte
2052characters. Elements may be nil for undefined code points.
c76b5c99
KH
2053See also the variable `nonascii-translation-table'."
2054 (let ((table (make-char-table 'translation-table))
2055 (rev-table (make-char-table 'translation-table))
c76b5c99 2056 ch)
9e3b6057 2057 (dotimes (i 256)
c76b5c99 2058 (setq ch (aref vec i))
9e3b6057
DL
2059 (when ch
2060 (aset table i ch)
2061 (if (>= ch 256)
2062 (aset rev-table ch i))))
c76b5c99
KH
2063 (set-char-table-extra-slot table 0 rev-table)
2064 table))
2065
f967223b 2066(defun define-translation-table (symbol &rest args)
a284eea3
DL
2067 "Define SYMBOL as the name of translation table made by ARGS.
2068This sets up information so that the table can be used for
2069translations in a CCL program.
b25eef20 2070
a284eea3
DL
2071If the first element of ARGS is a char-table whose purpose is
2072`translation-table', just define SYMBOL to name it. (Note that this
2073function does not bind SYMBOL.)
007c79c8 2074
a284eea3 2075Any other ARGS should be suitable as arguments of the function
007c79c8 2076`make-translation-table' (which see).
b25eef20 2077
452fdb31 2078This function sets properties `translation-table' and
521d4010
DL
2079`translation-table-id' of SYMBOL to the created table itself and the
2080identification number of the table respectively. It also registers
2081the table in `translation-table-vector'."
007c79c8
KH
2082 (let ((table (if (and (char-table-p (car args))
2083 (eq (char-table-subtype (car args))
2084 'translation-table))
2085 (car args)
2086 (apply 'make-translation-table args)))
f967223b 2087 (len (length translation-table-vector))
d9e3229d 2088 (id 0)
b25eef20 2089 (done nil))
f967223b 2090 (put symbol 'translation-table table)
b25eef20
KH
2091 (while (not done)
2092 (if (>= id len)
f967223b
KH
2093 (setq translation-table-vector
2094 (vconcat translation-table-vector (make-vector len nil))))
2095 (let ((slot (aref translation-table-vector id)))
b25eef20
KH
2096 (if (or (not slot)
2097 (eq (car slot) symbol))
2098 (progn
f967223b 2099 (aset translation-table-vector id (cons symbol table))
007c79c8
KH
2100 (setq done t))
2101 (setq id (1+ id)))))
f967223b 2102 (put symbol 'translation-table-id id)
d9e3229d
KH
2103 id))
2104
0e86dca1
KH
2105(defun translate-region (start end table)
2106 "From START to END, translate characters according to TABLE.
2107TABLE is a string or a char-table.
2108If TABLE is a string, the Nth character in it is the mapping
2109for the character with code N.
2110If TABLE is a char-table, the element for character N is the mapping
2111for the character with code N.
2112It returns the number of characters changed."
2113 (interactive
2114 (list (region-beginning)
2115 (region-end)
2116 (let (table l)
2117 (dotimes (i (length translation-table-vector))
2118 (if (consp (aref translation-table-vector i))
2119 (push (list (symbol-name
2120 (car (aref translation-table-vector i)))) l)))
2121 (if (not l)
2122 (error "No translation table defined"))
2123 (while (not table)
2124 (setq table (completing-read "Translation table: " l nil t)))
2125 (intern table))))
2126 (if (symbolp table)
2127 (let ((val (get table 'translation-table)))
2128 (or (char-table-p val)
2129 (error "Invalid translation table name: %s" table))
2130 (setq table val)))
2131 (translate-region-internal start end table))
2132
35554641
KH
2133(put 'with-category-table 'lisp-indent-function 1)
2134
ef6e365d
JPW
2135(defmacro with-category-table (table &rest body)
2136 "Evaluate BODY with category table of current buffer set to TABLE.
2137The category table of the current buffer is saved, BODY is evaluated,
2138then the saved table is restored, even in case of an abnormal exit.
2139Value is what BODY returns."
2140 (let ((old-table (make-symbol "old-table"))
2141 (old-buffer (make-symbol "old-buffer")))
2142 `(let ((,old-table (category-table))
2143 (,old-buffer (current-buffer)))
2144 (unwind-protect
2145 (progn
2146 (set-category-table ,table)
2147 ,@body)
2148 (save-current-buffer
2149 (set-buffer ,old-buffer)
2150 (set-category-table ,old-table))))))
35554641 2151
394e4eb0
DL
2152(defun define-translation-hash-table (symbol table)
2153 "Define SYMBOL as the name of the hash translation TABLE for use in CCL.
2154
2155Analogous to `define-translation-table', but updates
2156`translation-hash-table-vector' and the table is for use in the CCL
2157`lookup-integer' and `lookup-character' functions."
2158 (unless (and (symbolp symbol)
2159 (hash-table-p table))
2160 (error "Bad args to define-translation-hash-table"))
2161 (let ((len (length translation-hash-table-vector))
2162 (id 0)
2163 done)
2164 (put symbol 'translation-hash-table table)
2165 (while (not done)
2166 (if (>= id len)
2167 (setq translation-hash-table-vector
2168 (vconcat translation-hash-table-vector [nil])))
2169 (let ((slot (aref translation-hash-table-vector id)))
2170 (if (or (not slot)
2171 (eq (car slot) symbol))
2172 (progn
2173 (aset translation-hash-table-vector id (cons symbol table))
2174 (setq done t))
2175 (setq id (1+ id)))))
2176 (put symbol 'translation-hash-table-id id)
2177 id))
2178
69eba008
KH
2179;;; Initialize some variables.
2180
2181(put 'use-default-ascent 'char-table-extra-slots 0)
2182(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
2183(put 'ignore-relative-composition 'char-table-extra-slots 0)
2184(setq ignore-relative-composition
2185 (make-char-table 'ignore-relative-composition))
69eba008 2186
d9f6dfe6
CW
2187
2188;;; Built-in auto-coding-functions:
2189
2190(defun sgml-xml-auto-coding-function (size)
2191 "Determine whether the buffer is XML, and if so, its encoding.
2192This function is intended to be added to `auto-coding-functions'."
c069d3ac
SM
2193 (setq size (+ (point) size))
2194 (when (re-search-forward "\\`[[:space:]\n]*<\\?xml" size t)
d9f6dfe6
CW
2195 (let ((end (save-excursion
2196 ;; This is a hack.
c2c51a11 2197 (re-search-forward "[\"']\\s-*\\?>" size t))))
d9f6dfe6 2198 (when end
c2c51a11 2199 (if (re-search-forward "encoding=[\"']\\(.+?\\)[\"']" end t)
447404a3
CW
2200 (let* ((match (match-string 1))
2201 (sym (intern (downcase match))))
2202 (if (coding-system-p sym)
2203 sym
2204 (message "Warning: unknown coding system \"%s\"" match)
2205 nil))
d9f6dfe6
CW
2206 'utf-8)))))
2207
447404a3
CW
2208(defun sgml-html-meta-auto-coding-function (size)
2209 "If the buffer has an HTML meta tag, use it to determine encoding.
2210This function is intended to be added to `auto-coding-functions'."
c069d3ac 2211 (setq size (min (+ (point) size)
447404a3
CW
2212 ;; Only search forward 10 lines
2213 (save-excursion
2214 (forward-line 10)
2215 (point))))
81668eb2 2216 (when (and (search-forward "<html" size t)
447404a3
CW
2217 (re-search-forward "<meta\\s-+http-equiv=\"content-type\"\\s-+content=\"text/\\sw+;\\s-*charset=\\(.+?\\)\"" size t))
2218 (let* ((match (match-string 1))
2219 (sym (intern (downcase match))))
2220 (if (coding-system-p sym)
2221 sym
2222 (message "Warning: unknown coding system \"%s\"" match)
2223 nil))))
0bca779a 2224
69eba008 2225;;;
4ed46869
KH
2226(provide 'mule)
2227
ab5796a9 2228;;; arch-tag: 9aebaa6e-0e8a-40a9-b857-cb5d04a39e7c
4ed46869 2229;;; mule.el ends here