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