mule.el 44.4 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1 2 3
;;; mule.el --- basic commands for mulitilingual environment

;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;; Licensed to the Free Software Foundation.
Karl Heuer's avatar
Karl Heuer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; Keywords: mule, multilingual, character set, coding system

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Karl Heuer's avatar
Karl Heuer committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Karl Heuer's avatar
Karl Heuer committed
24 25 26

;;; Code:

27
(defconst mule-version "4.0 (HANANOEN)" "\
Karl Heuer's avatar
Karl Heuer committed
28 29
Version number and name of this version of MULE (multilingual environment).")

30
(defconst mule-version-date "1998.7.1" "\
Karl Heuer's avatar
Karl Heuer committed
31 32 33
Distribution date of this version of MULE (multilingual environment).")

(defun load-with-code-conversion (fullname file &optional noerror nomessage)
34 35
  "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
The file contents are decoded before evaluation if necessary.
Karl Heuer's avatar
Karl Heuer committed
36 37 38 39 40 41 42 43 44 45 46 47
If optional second arg NOERROR is non-nil,
 report no error if FILE doesn't exist.
Print messages at start and end of loading unless
 optional third arg NOMESSAGE is non-nil.
Return t if file exists."
  (if (null (file-readable-p fullname))
      (and (null noerror)
	   (signal 'file-error (list "Cannot open load file" file)))
    ;; Read file with code conversion, and then eval.
    (let* ((buffer
	    ;; To avoid any autoloading, set default-major-mode to
	    ;; fundamental-mode.
48 49 50 51 52 53
	    ;; So that we don't get completely screwed if the
	    ;; file is encoded in some complicated character set,
	    ;; read it with real decoding, as a multibyte buffer,
	    ;; even if this is a --unibyte Emacs session.
	    (let ((default-major-mode 'fundamental-mode)
		  (default-enable-multibyte-characters t))
Karl Heuer's avatar
Karl Heuer committed
54 55 56
	      ;; We can't use `generate-new-buffer' because files.el
	      ;; is not yet loaded.
	      (get-buffer-create (generate-new-buffer-name " *load*"))))
57 58 59 60 61 62 63 64
	   (load-in-progress t)
	   (source (save-match-data (string-match "\\.el\\'" fullname))))
      (unless nomessage
	(if source
	    (message "Loading %s (source)..." file)
	  (message "Loading %s..." file)))
      (when purify-flag
	(setq preloaded-file-list (cons file preloaded-file-list)))
Karl Heuer's avatar
Karl Heuer committed
65
      (unwind-protect
66
	  (let ((load-file-name fullname)
67
		(set-auto-coding-for-load t)
68
		(inhibit-file-name-operation nil))
Karl Heuer's avatar
Karl Heuer committed
69 70 71 72 73
	    (save-excursion
	      (set-buffer buffer)
	      (insert-file-contents fullname)
	      ;; Make `kill-buffer' quiet.
	      (set-buffer-modified-p nil))
74
	    ;; Have the original buffer current while we eval.
75 76 77 78
	    (eval-buffer buffer nil file
			 ;; If this Emacs is running with --unibyte,
			 ;; convert multibyte strings to unibyte
			 ;; after reading them.
79 80
;;			 (not default-enable-multibyte-characters)
			 ))
81 82
	(let (kill-buffer-hook kill-buffer-query-functions)
	  (kill-buffer buffer)))
Karl Heuer's avatar
Karl Heuer committed
83
      (let ((hook (assoc file after-load-alist)))
84 85 86 87 88 89
	(when hook
	  (mapcar (function eval) (cdr hook))))
      (unless (or nomessage noninteractive)
	(if source
	    (message "Loading %s (source)...done" file)
	  (message "Loading %s...done" file)))
Karl Heuer's avatar
Karl Heuer committed
90 91 92 93
      t)))

;; API (Application Program Interface) for charsets.

94 95 96 97 98 99 100 101
;; Return t if OBJ is a quoted symbol
;; and the symbol is the name of a standard charset.
(defsubst charset-quoted-standard-p (obj)
  (and (listp obj) (eq (car obj) 'quote)
       (symbolp (car-safe (cdr obj)))
       (let ((vector (get (car-safe (cdr obj)) 'charset)))
	 (and (vectorp vector)
	      (< (aref vector 0) 160)))))
Karl Heuer's avatar
Karl Heuer committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

(defsubst charsetp (object)
  "T is OBJECT is a charset."
  (and (symbolp object) (vectorp (get object 'charset))))

(defsubst charset-info (charset)
  "Return a vector of information of CHARSET.
The elements of the vector are:
	CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
	LEADING-CODE-BASE, LEADING-CODE-EXT,
	ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
	REVERSE-CHARSET, SHORT-NAME, LONG-NAME,	DESCRIPTION,
	PLIST,
where
CHARSET-ID (integer) is the identification number of the charset.
DIMENSION (integer) is the number of bytes to represent a character of
the charset: 1 or 2.
CHARS (integer) is the number of characters in a dimension: 94 or 96.
BYTE (integer) is the length of multi-byte form of a character in
  the charset: one of 1, 2, 3, and 4.
WIDTH (integer) is the number of columns a character in the charset
  occupies on the screen: one of 0, 1, and 2.
DIRECTION (integer) is the rendering direction of characters in the
  charset when rendering.  If 0, render from right to left, else
  render from left to right.
LEADING-CODE-BASE (integer) is the base leading-code for the
  charset.
LEADING-CODE-EXT (integer) is the extended leading-code for the
  charset.  All charsets of less than 0xA0 has the value 0.
ISO-FINAL-CHAR (character) is the final character of the
  corresponding ISO 2022 charset.
ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
  while encoding to variants of ISO 2022 coding system, one of the
  following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
REVERSE-CHARSET (integer) is the charset which differs only in
  LEFT-TO-RIGHT value from the charset.  If there's no such a
  charset, the value is -1.
SHORT-NAME (string) is the short name to refer to the charset.
LONG-NAME (string) is the long name to refer to the charset
DESCRIPTION (string) is the description string of the charset.
PLIST (property list) may contain any type of information a user
  want to put and get by functions `put-charset-property' and
  `get-charset-property' respectively."
  (get charset 'charset))

(defmacro charset-id (charset)
  "Return charset identification number of CHARSET."
149
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
150 151 152 153
      (aref (charset-info (nth 1 charset)) 0)
    `(aref (charset-info ,charset) 0)))

(defmacro charset-bytes (charset)
154 155
  "Return bytes of CHARSET.
See the function `charset-info' for more detail."
156
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
157 158 159 160
      (aref (charset-info (nth 1 charset)) 1)
    `(aref (charset-info ,charset) 1)))

(defmacro charset-dimension (charset)
161 162
  "Return dimension of CHARSET.
See the function `charset-info' for more detail."
163
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
164 165 166 167
      (aref (charset-info (nth 1 charset)) 2)
    `(aref (charset-info ,charset) 2)))

(defmacro charset-chars (charset)
168 169
  "Return character numbers contained in a dimension of CHARSET.
See the function `charset-info' for more detail."
170
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
171 172 173 174
      (aref (charset-info (nth 1 charset)) 3)
    `(aref (charset-info ,charset) 3)))

(defmacro charset-width (charset)
175 176
  "Return width (how many column occupied on a screen) of CHARSET.
See the function `charset-info' for more detail."
177
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
178 179 180 181
      (aref (charset-info (nth 1 charset)) 4)
    `(aref (charset-info ,charset) 4)))

(defmacro charset-direction (charset)
182 183
  "Return direction of CHARSET.
See the function `charset-info' for more detail."
184
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
185 186 187 188
      (aref (charset-info (nth 1 charset)) 5)
    `(aref (charset-info ,charset) 5)))

(defmacro charset-iso-final-char (charset)
189 190
  "Return final char of CHARSET.
See the function `charset-info' for more detail."
191
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
192 193 194 195
      (aref (charset-info (nth 1 charset)) 8)
    `(aref (charset-info ,charset) 8)))

(defmacro charset-iso-graphic-plane (charset)
196 197
  "Return graphic plane of CHARSET.
See the function `charset-info' for more detail."
198
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
199 200 201 202
      (aref (charset-info (nth 1 charset)) 9)
    `(aref (charset-info ,charset) 9)))

(defmacro charset-reverse-charset (charset)
203 204
  "Return reverse charset of CHARSET.
See the function `charset-info' for more detail."
205
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
206 207 208 209
      (aref (charset-info (nth 1 charset)) 10)
    `(aref (charset-info ,charset) 10)))

(defmacro charset-short-name (charset)
210 211
  "Return short name of CHARSET.
See the function `charset-info' for more detail."
212
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
213 214 215 216
      (aref (charset-info (nth 1 charset)) 11)
    `(aref (charset-info ,charset) 11)))

(defmacro charset-long-name (charset)
217 218
  "Return long name of CHARSET.
See the function `charset-info' for more detail."
219
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
220 221 222 223
      (aref (charset-info (nth 1 charset)) 12)
    `(aref (charset-info ,charset) 12)))

(defmacro charset-description (charset)
224 225
  "Return descriptoin of CHARSET.
See the function `charset-info' for more detail."
226
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
227 228 229 230
      (aref (charset-info (nth 1 charset)) 13)
    `(aref (charset-info ,charset) 13)))

(defmacro charset-plist (charset)
231 232
  "Return list charset property of CHARSET.
See the function `charset-info' for more detail."
233
  (if (charset-quoted-standard-p charset)
Kenichi Handa's avatar
Kenichi Handa committed
234
      `(aref ,(charset-info (nth 1 charset)) 14)
Karl Heuer's avatar
Karl Heuer committed
235 236 237
    `(aref (charset-info ,charset) 14)))

(defun set-charset-plist (charset plist)
238
  "Set CHARSET's property list to PLIST, and retrun PLIST."
Karl Heuer's avatar
Karl Heuer committed
239 240
  (aset (charset-info  charset) 14 plist))

241
(defun make-char (charset &optional c1 c2)
Kenichi Handa's avatar
Kenichi Handa committed
242 243
  "Return a character of CHARSET and position-codes CODE1 and CODE2.
CODE1 and CODE2 are optional, but if you don't supply
244
sufficient position-codes, return a generic character which stands for
Kenichi Handa's avatar
Kenichi Handa committed
245
all characters or group of characters in the character sets.
246
A generic character can be used to index a char table (e.g. syntax-table)."
247 248 249 250 251 252 253 254 255 256 257 258 259
  (make-char-internal (charset-id charset) c1 c2))

(put 'make-char 'byte-compile
     (function 
      (lambda (form)
	(let ((charset (nth 1 form)))
	  (if (charset-quoted-standard-p charset)
	      (byte-compile-normal-call
	       (cons 'make-char-internal
		     (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
	    (byte-compile-normal-call
	     (cons 'make-char-internal
		   (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
Karl Heuer's avatar
Karl Heuer committed
260

261
(defun charset-list ()
262 263
  "Return list of charsets ever defined.

264
This function is provided for backward compatibility.
265
Now we have the variable `charset-list'."
266 267
  charset-list)

268 269 270
(defsubst generic-char-p (char)
  "Return t if and only if CHAR is a generic character.
See also the documentation of make-char."
271 272 273 274
  (and (>= char 0400)
       (let ((l (split-char char)))
	 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
	      (not (eq (car l) 'composition))))))
275

276

277
;; Coding system staffs
Karl Heuer's avatar
Karl Heuer committed
278

279
;; Coding system is a symbol that has the property `coding-system'.
Karl Heuer's avatar
Karl Heuer committed
280
;;
281 282
;; The value of the property `coding-system' is a vector of the
;; following format:
283
;;	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
284
;; We call this vector as coding-spec.  See comments in src/coding.c
285
;; for more detail.  
286 287 288 289

(defconst coding-spec-type-idx 0)
(defconst coding-spec-mnemonic-idx 1)
(defconst coding-spec-doc-string-idx 2)
290
(defconst coding-spec-plist-idx 3)
291 292
(defconst coding-spec-flags-idx 4)

293 294 295 296
;; PLIST is a property list of a coding system.  To share PLIST among
;; alias coding systems, a coding system has PLIST in coding-spec
;; instead of having it in normal property list of Lisp symbol.
;; Here's a list of coding system properties currently being used.
297 298 299 300 301 302
;;
;; o coding-category
;;
;; The value is a coding category the coding system belongs to.  The
;; function `make-coding-system' and `define-coding-system-alias' sets
;; this value automatically.
Karl Heuer's avatar
Karl Heuer committed
303
;;
304
;; o alias-coding-systems
Karl Heuer's avatar
Karl Heuer committed
305
;;
306 307 308 309 310 311 312 313 314 315 316
;; The value is a list of coding systems of the same alias group.  The
;; first element is the coding system made at first, which we call as
;; `base coding system'.  The function `make-coding-system' and
;; `define-coding-system-alias' set this value automatically.
;;
;; o post-read-conversion
;;
;; The value is a function to call after some text is inserted and
;; decoded by the coding system itself and before any functions in
;; `after-insert-functions' are called.  The arguments to this
;; function is the same as those of a function in
Karl Heuer's avatar
Karl Heuer committed
317 318 319
;; `after-insert-functions', i.e. LENGTH of a text while putting point
;; at the head of the text to be decoded
;;
320 321 322 323 324 325 326 327 328
;; o pre-write-conversion
;;
;; The value is a function to call after all functions in
;; `write-region-annotate-functions' and `buffer-file-format' are
;; called, and before the text is encoded by the coding system itself.
;; The arguments to this function is the same as those of a function
;; in `write-region-annotate-functions', i.e. FROM and TO specifying
;; region of a text.
;;
329
;; o translation-table-for-decode
330
;;
331 332 333
;; The value is a translation table to be applied on decoding.  See
;; the function `make-translation-table' for the format of translation
;; table.
334
;;
335
;; o translation-table-for-encode
336
;;
337
;; The value is a translation table to be applied on encoding.
338 339 340 341 342 343 344 345 346
;;
;; o safe-charsets
;;
;; The value is a list of charsets safely supported by the coding
;; system.  The value t means that all charsets Emacs handles are
;; supported.  Even if some charset is not in this list, it doesn't
;; mean that the charset can't be encoded in the coding system,
;; instead, it just means that some other receiver of a text encoded
;; in the coding system won't be able to handle that charset.
347 348 349 350 351
;;
;; o mime-charset
;;
;; The value is a symbol of which name is `MIME-charset' parameter of
;; the coding system.
352 353 354 355

;; Return coding-spec of CODING-SYSTEM
(defsubst coding-system-spec (coding-system)
  (get (check-coding-system coding-system) 'coding-system))
Karl Heuer's avatar
Karl Heuer committed
356 357

(defun coding-system-type (coding-system)
358 359 360 361
  "Return the coding type of CODING-SYSTEM.
A coding type is an integer value indicating the encoding method
of CODING-SYSTEM.  See the function `make-coding-system' for more detail."
  (aref (coding-system-spec coding-system) coding-spec-type-idx))
Karl Heuer's avatar
Karl Heuer committed
362 363

(defun coding-system-mnemonic (coding-system)
364
  "Return the mnemonic character of CODING-SYSTEM.
365 366 367 368
The mnemonic character of a coding system is used in mode line
to indicate the coding system.  If the arg is nil, return ?-."
  (let ((spec (coding-system-spec coding-system)))
    (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
Karl Heuer's avatar
Karl Heuer committed
369

370
(defun coding-system-doc-string (coding-system)
371 372
  "Return the documentation string for CODING-SYSTEM."
  (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
Karl Heuer's avatar
Karl Heuer committed
373

374
(defun coding-system-plist (coding-system)
375 376
  "Return the property list of CODING-SYSTEM."
  (aref (coding-system-spec coding-system) coding-spec-plist-idx))
377

Karl Heuer's avatar
Karl Heuer committed
378
(defun coding-system-flags (coding-system)
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  "Return `flags' of CODING-SYSTEM.
A `flags' of a coding system is a vector of length 32 indicating detailed
information of a coding system.  See the function `make-coding-system'
for more detail."
  (aref (coding-system-spec coding-system) coding-spec-flags-idx))

(defun coding-system-get (coding-system prop)
  "Extract a value from CODING-SYSTEM's property list for property PROP."
  (plist-get (coding-system-plist coding-system) prop))

(defun coding-system-put (coding-system prop val)
  "Change value in CODING-SYSTEM's property list PROP to VAL."
  (let ((plist (coding-system-plist coding-system)))
    (if plist
	(plist-put plist prop val)
      (aset (coding-system-spec coding-system) coding-spec-plist-idx
	    (list prop val)))))

(defun coding-system-category (coding-system)
  "Return the coding category of CODING-SYSTEM."
  (coding-system-get coding-system 'coding-category))

(defun coding-system-base (coding-system)
  "Return the base coding system of CODING-SYSTEM.
403 404
A base coding system is what made by `make-coding-system'.
Any alias nor subsidiary coding systems are not base coding system."
405 406 407 408 409 410 411 412 413 414 415 416 417 418
  (car (coding-system-get coding-system 'alias-coding-systems)))

(defalias 'coding-system-parent 'coding-system-base)
(make-obsolete 'coding-system-parent 'coding-system-base)

;; Coding system also has a property `eol-type'.
;;
;; This property indicates how the coding system handles end-of-line
;; format.  The value is integer 0, 1, 2, or a vector of three coding
;; systems.  Each integer value 0, 1, and 2 indicates the format of
;; end-of-line LF, CRLF, and CR respectively.  A vector value
;; indicates that the format of end-of-line should be detected
;; automatically.  Nth element of the vector is the subsidiary coding
;; system whose `eol-type' property is N.
Karl Heuer's avatar
Karl Heuer committed
419

420
(defun coding-system-eol-type (coding-system)
421 422
  "Return eol-type of CODING-SYSTEM.
An eol-type is integer 0, 1, 2, or a vector of coding systems.
Karl Heuer's avatar
Karl Heuer committed
423

424 425 426 427 428 429 430
Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
CRLF, and CR respectively.

A vector value indicates that a format of end-of-line should be
detected automatically.  Nth element of the vector is the subsidiary
coding system whose eol-type is N."
  (get coding-system 'eol-type))
431

432 433
;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
(defun make-subsidiary-coding-system (coding-system)
434 435
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
436 437
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
438 439
	(i 0)
	temp)
440
    (while (< i 3)
441
      (put (aref subsidiaries i) 'coding-system coding-spec)
442
      (put (aref subsidiaries i) 'eol-type i)
443 444 445 446 447
      (setq coding-system-list
	    (cons (aref subsidiaries i) coding-system-list))
      (setq coding-system-alist
	    (cons (list (symbol-name (aref subsidiaries i)))
		  coding-system-alist))
448 449
      (setq i (1+ i)))
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
450

451
(defun make-coding-system (coding-system type mnemonic doc-string
452
					 &optional flags properties)
453
  "Define a new coding system CODING-SYSTEM (symbol).
454
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional), 
455
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
456
in the following format:
457
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
458 459

TYPE is an integer value indicating the type of the coding system as follows:
Karl Heuer's avatar
Karl Heuer committed
460 461 462 463
  0: Emacs internal format,
  1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
  2: ISO-2022 including many variants,
  3: Big5 used mainly on Chinese PC,
464 465
  4: private, CCL programs provide encoding/decoding algorithm,
  5: Raw-text, which means that text contains random 8-bit codes. 
466

467
MNEMONIC is a character to be displayed on mode line for the coding system.
468

469
DOC-STRING is a documentation string for the coding system.
470

471
FLAGS specifies more detailed information of the coding system as follows:
472

473
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
474 475
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
476
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
477
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    CHARSETn are character sets initially designated to Gn graphic registers.
      If CHARSETn is nil, Gn is never used.
      If CHARSETn is t, Gn can be used but nothing designated initially.
      If CHARSETn is a list of character sets, those character sets are
        designated to Gn on output, but nothing designated to Gn initially.
    SHORT-FORM non-nil means use short designation sequence on output.
    ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
    ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
      SPACE on output.
    SEVEN non-nil means use 7-bit code only on output.
    LOCKING-SHIFT non-nil means use locking-shift.
    SINGLE-SHIFT non-nil means use single-shift.
    USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
    USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
    NO-ISO6429 non-nil means not use ISO6429's direction specification.
493 494 495 496
    INIT-BOL non-nil means any designation state is assumed to be reset
      to initial at each beginning of line on output.
    DESIGNATION-BOL non-nil means designation sequences should be placed
      at beginning of line on output.
497 498
    SAFE non-nil means convert unsafe characters to `?' on output.
      Unsafe characters are what not specified in SAFE-CHARSET.
499 500 501
    ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
      a code specified in `latin-extra-code-table' (which see) as a valid
      code of the coding system.
502

503 504 505
  If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
    decoding and encoding.  CCL programs should be specified by their
    symbols.
506 507 508 509 510

PROPERTIES is an alist of properties vs the corresponding values.
These properties are set in PLIST, a property list.  This function
also sets properties `coding-category' and `alias-coding-systems'
automatically.
Karl Heuer's avatar
Karl Heuer committed
511

512 513 514 515 516 517 518
Kludgy features for backward compatibility:

1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
treated as a compiled CCL code.

2. If PROPERTIES is just a list of character sets, the list is set as
a value of `safe-charsets' in PLIST."
519
  (if (memq coding-system coding-system-list)
520
      (error "Coding system %s already exists" coding-system))
521 522

  ;; Set a value of `coding-system' property.
523
  (let ((coding-spec (make-vector 5 nil))
524 525
	(no-initial-designation t)
	(no-alternative-designation t)
526
	coding-category)
527
    (if (or (not (integerp type)) (< type 0) (> type 5))
528
	(error "TYPE argument must be 0..5"))
529
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
530 531 532 533 534
	(error "MNEMONIC arguemnt must be an ASCII printable character."))
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
	  (if (stringp doc-string) doc-string ""))
535 536 537 538 539
    (cond ((= type 0)
	   (setq coding-category 'coding-category-emacs-mule))
	  ((= type 1)
	   (setq coding-category 'coding-category-sjis))
	  ((= type 2)			; ISO2022
Karl Heuer's avatar
Karl Heuer committed
540
	   (let ((i 0)
541 542
		 (vec (make-vector 32 nil))
		 (g1-designation nil))
Karl Heuer's avatar
Karl Heuer committed
543 544
	     (while (< i 4)
	       (let ((charset (car flags)))
545 546 547 548 549 550 551 552 553 554 555 556 557
		 (if (and no-initial-designation
			  (> i 0)
			  (or (charsetp charset)
			      (and (consp charset)
				   (charsetp (car charset)))))
		     (setq no-initial-designation nil))
		 (if (charsetp charset)
		     (if (= i 1) (setq g1-designation charset))
		   (if (consp charset)
		       (let ((tail charset)
			     elt)
			 (while tail
			   (setq elt (car tail))
558 559 560 561
			   (if (eq elt t)
			       (setq no-alternative-designation nil)
			     (if (and elt (not (charsetp elt)))
				 (error "Invalid charset: %s" elt)))
562 563
			   (setq tail (cdr tail)))
			 (setq g1-designation (car charset)))
564 565 566 567
		     (if charset
			 (if (eq charset t)
			     (setq no-alternative-designation nil)
			   (error "Invalid charset: %s" charset)))))
Karl Heuer's avatar
Karl Heuer committed
568 569 570 571 572
		 (aset vec i charset))
	       (setq flags (cdr flags) i (1+ i)))
	     (while (and (< i 32) flags)
	       (aset vec i (car flags))
	       (setq flags (cdr flags) i (1+ i)))
573 574 575
	     (aset coding-spec 4 vec)
	     (setq coding-category
		   (if (aref vec 8)	; Use locking-shift.
576 577
		       (or (and (aref vec 7) 'coding-category-iso-7-else)
			   'coding-category-iso-8-else)
578 579
		     (if (aref vec 7)	; 7-bit only.
			 (if (aref vec 9) ; Use single-shift.
580
			     'coding-category-iso-7-else
581 582 583
			   (if no-alternative-designation
			       'coding-category-iso-7-tight
			     'coding-category-iso-7))
584 585
		       (if (or no-initial-designation
			       (not no-alternative-designation))
586
			   'coding-category-iso-8-else
587 588 589 590 591 592 593 594
			 (if (and (charsetp g1-designation)
				  (= (charset-dimension g1-designation) 2))
			     'coding-category-iso-8-2
			   'coding-category-iso-8-1)))))))
	  ((= type 3)
	   (setq coding-category 'coding-category-big5))
	  ((= type 4)			; private
	   (setq coding-category 'coding-category-binary)
595 596 597 598 599 600 601 602 603 604 605
	   (if (not (consp flags))
	       (error "Invalid FLAGS argument for TYPE 4 (CCL)")
	     (let ((decoder (check-ccl-program
			     (car flags)
			     (intern (format "%s-decoder" coding-system))))
		   (encoder (check-ccl-program
			     (cdr flags)
			     (intern (format "%s-encoder" coding-system)))))
	       (if (and decoder encoder)
		   (aset coding-spec 4 (cons decoder encoder))
		 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
606 607
	  (t				; i.e. (= type 5)
	   (setq coding-category 'coding-category-raw-text)))
608 609

    (let ((plist (list 'coding-category coding-category
610
		       'alias-coding-systems (list coding-system))))
611
      (if no-initial-designation
612 613 614 615 616 617 618 619 620 621
	  (plist-put plist 'no-initial-designation t))
      (if (and properties
	       (or (eq properties t)
		   (not (consp (car properties)))))
	  ;; In the old version, the arg PROPERTIES is a list to be
	  ;; set in PLIST as a value of property `safe-charsets'.
	  (plist-put plist 'safe-charsets properties)
	(while properties
	  (plist-put plist (car (car properties)) (cdr (car properties)))
	  (setq properties (cdr properties))))
622
      (aset coding-spec coding-spec-plist-idx plist))
623 624 625
    (put coding-system 'coding-system coding-spec)
    (put coding-category 'coding-systems
	 (cons coding-system (get coding-category 'coding-systems))))
Karl Heuer's avatar
Karl Heuer committed
626 627

  ;; Next, set a value of `eol-type' property.  The value is a vector
628
  ;; of subsidiary coding systems, each corresponds to a coding system
Karl Heuer's avatar
Karl Heuer committed
629
  ;; for the detected end-of-line format.
630
  (put coding-system 'eol-type
631
       (if (or (<= type 3) (= type 5))
632
	   (make-subsidiary-coding-system coding-system)
633 634 635 636 637 638
	 0))

  ;; At last, register CODING-SYSTEM in `coding-system-list' and
  ;; `coding-system-alist'.
  (setq coding-system-list (cons coding-system coding-system-list))
  (setq coding-system-alist (cons (list (symbol-name coding-system))
639 640
				  coding-system-alist))
  coding-system)
641

642
(defun define-coding-system-alias (alias coding-system)
Richard M. Stallman's avatar
Richard M. Stallman committed
643
  "Define ALIAS as an alias for coding system CODING-SYSTEM."
644 645 646 647 648 649 650 651 652
  (put alias 'coding-system (coding-system-spec coding-system))
  (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
  (setq coding-system-list (cons alias coding-system-list))
  (setq coding-system-alist (cons (list (symbol-name alias))
				  coding-system-alist))
  (let ((eol-type (coding-system-eol-type coding-system)))
    (if (vectorp eol-type)
	(put alias 'eol-type (make-subsidiary-coding-system alias))
      (put alias 'eol-type eol-type))))
Karl Heuer's avatar
Karl Heuer committed
653 654

(defun set-buffer-file-coding-system (coding-system &optional force)
Richard M. Stallman's avatar
Richard M. Stallman committed
655 656 657 658 659 660 661 662 663
  "Set the file coding-system of the current buffer to CODING-SYSTEM.
This means that when you save the buffer, it will be converted
according to CODING-SYSTEM.  For a list of possible values of CODING-SYSTEM,
use \\[list-coding-systems].

If the buffer's previous file coding-system value specifies end-of-line
conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
merged with the already-specified end-of-line conversion.
However, if the optional prefix argument FORCE is non-nil,
664
then CODING-SYSTEM is used exactly as specified."
665
  (interactive "zCoding system for visited file (default, nil): \nP")
Karl Heuer's avatar
Karl Heuer committed
666 667
  (check-coding-system coding-system)
  (if (null force)
668 669
      (let ((x (coding-system-eol-type buffer-file-coding-system))
	    (y (coding-system-eol-type coding-system)))
Karl Heuer's avatar
Karl Heuer committed
670 671 672 673 674 675
	(if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
	    (setq coding-system (aref y x)))))
  (setq buffer-file-coding-system coding-system)
  (set-buffer-modified-p t)
  (force-mode-line-update))

Richard M. Stallman's avatar
Richard M. Stallman committed
676 677 678 679 680
(defvar default-terminal-coding-system nil
  "Default value for the terminal coding system.
This is normally set according to the selected language environment.
See also the command `set-terminal-coding-system'.")

681 682
(defun set-terminal-coding-system (coding-system)
  "Set coding system of your terminal to CODING-SYSTEM.
Richard M. Stallman's avatar
Richard M. Stallman committed
683 684 685 686 687 688
All text output to the terminal will be encoded
with the specified coding system.
For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
The default is determined by the selected language environment
or by the previous use of this command."
  (interactive
689 690 691 692 693 694 695
   (list (let ((default (if (and (not (terminal-coding-system))
				 default-terminal-coding-system)
			    default-terminal-coding-system)))
	   (read-coding-system
	    (format "Coding system for terminal display (default, %s): "
		    default)
	    default))))
Richard M. Stallman's avatar
Richard M. Stallman committed
696 697 698 699 700
  (if (and (not coding-system)
	   (not (terminal-coding-system)))
      (setq coding-system default-terminal-coding-system))
  (if coding-system
      (setq default-terminal-coding-system coding-system))      
701 702 703
  (set-terminal-coding-system-internal coding-system)
  (redraw-frame (selected-frame)))

Richard M. Stallman's avatar
Richard M. Stallman committed
704 705 706 707 708
(defvar default-keyboard-coding-system nil
  "Default value of the keyboard coding system.
This is normally set according to the selected language environment.
See also the command `set-keyboard-coding-system'.")

709
(defun set-keyboard-coding-system (coding-system)
Richard M. Stallman's avatar
Richard M. Stallman committed
710 711
  "Set coding system for keyboard input to CODING-SYSTEM.
In addition, this command enables Encoded-kbd minor mode.
712
\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
Richard M. Stallman's avatar
Richard M. Stallman committed
713 714 715 716
For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
The default is determined by the selected language environment
or by the previous use of this command."
  (interactive
717 718 719 720 721 722 723
   (list (let ((default (if (and (not (keyboard-coding-system))
				 default-keyboard-coding-system)
			    default-keyboard-coding-system)))
	   (read-coding-system
	    (format "Coding system for keyboard input (default, %s): "
		    default)
	    default))))
Richard M. Stallman's avatar
Richard M. Stallman committed
724 725 726 727 728
  (if (and (not coding-system)
	   (not (keyboard-coding-system)))
      (setq coding-system default-keyboard-coding-system))
  (if coding-system
      (setq default-keyboard-coding-system coding-system))
729 730 731 732
  (set-keyboard-coding-system-internal coding-system)
  (encoded-kbd-mode (if coding-system 1 0)))

(defun set-buffer-process-coding-system (decoding encoding)
Richard M. Stallman's avatar
Richard M. Stallman committed
733
  "Set coding systems for the process associated with the current buffer.
734
DECODING is the coding system to be used to decode input from the process,
Richard M. Stallman's avatar
Richard M. Stallman committed
735 736 737
ENCODING is the coding system to be used to encode output to the process.

For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
Karl Heuer's avatar
Karl Heuer committed
738 739 740 741 742
  (interactive
   "zCoding-system for process input: \nzCoding-system for process output: ")
  (let ((proc (get-buffer-process (current-buffer))))
    (if (null proc)
	(error "no process")
743 744 745
      (check-coding-system decoding)
      (check-coding-system encoding)
      (set-process-coding-system proc decoding encoding)))
Karl Heuer's avatar
Karl Heuer committed
746 747
  (force-mode-line-update))

748 749
(defalias 'set-clipboard-coding-system 'set-selection-coding-system)

750
(defun set-selection-coding-system (coding-system)
751 752 753 754
  "Make CODING-SYSTEM used for communicating with other X clients .
When sending or receiving text via cut_buffer, selection, and clipboard,
the text is encoded or decoded by CODING-SYSTEM."
  (check-coding-system coding-system)
755
  (setq selection-coding-system coding-system))
756

Karl Heuer's avatar
Karl Heuer committed
757
(defun set-coding-priority (arg)
758 759 760 761 762 763 764 765 766 767 768 769 770
  "Set priority of coding categories according to LIST.
LIST is a list of coding categories ordered by priority."
  (let ((l arg)
	(current-list (copy-sequence coding-category-list)))
    ;; Check the varidity of ARG while deleting coding categories in
    ;; ARG from CURRENT-LIST.  We assume that CODING-CATEGORY-LIST
    ;; contains all coding categories.
    (while l
      (if (or (null (get (car l) 'coding-category-index))
	      (null (memq (car l) current-list)))
	  (error "Invalid or duplicated element in argument: %s" arg))
      (setq current-list (delq (car l) current-list))
      (setq l (cdr l)))
Karl Heuer's avatar
Karl Heuer committed
771
    ;; Update `coding-category-list' and return it.
Kenichi Handa's avatar
Kenichi Handa committed
772 773
    (setq coding-category-list (append arg current-list))
    (set-coding-priority-internal)))
Karl Heuer's avatar
Karl Heuer committed
774 775 776

;;; FILE I/O

777
(defvar auto-coding-alist
778 779
  '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\)\\'" . no-conversion)
    ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\)\\'" . no-conversion))
780 781
  "Alist of filename patterns vs corresponding coding systems.
Each element looks like (REGEXP . CODING-SYSTEM).
782
A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
783

784 785 786
The settings in this alist take priority over `coding:' tags
in the file (see the function `set-auto-coding')
and the contents of `file-coding-system-alist'.")
787

788 789 790 791
(defvar set-auto-coding-for-load nil
  "Non-nil means look for `load-coding' property instead of `coding'.
This is used for loading and byte-compiling Emacs Lisp files.")

792 793
(defun set-auto-coding (filename size)
  "Return coding system for a file FILENAME of which SIZE bytes follow point.
794 795
These bytes should include at least the first 1k of the file
and the last 3k of the file, but the middle may be omitted.
796

797 798 799
It checks FILENAME against the variable `auto-coding-alist'.
If FILENAME doesn't match any entries in the variable,
it checks for a `coding:' tag in the first one or two lines following
800 801
point.  If no `coding:' tag is found, it checks for alocal variables
list in the last 3K bytes out of the SIZE bytes.
802 803 804

The return value is the specified coding system,
or nil if nothing specified.
805

806
The variable `set-auto-coding-function' (which see) is set to this
807
function by default."
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
  (let ((alist auto-coding-alist)
	(case-fold-search (memq system-type '(vax-vms windows-nt)))
	coding-system)
    (while (and alist (not coding-system))
      (if (string-match (car (car alist)) filename)
	  (setq coding-system (cdr (car alist)))
	(setq alist (cdr alist))))

    (or coding-system
	(let* ((case-fold-search t)
	       (head-start (point))
	       (head-end (+ head-start (min size 1024)))
	       (tail-start (+ head-start (max (- size 3072) 0)))
	       (tail-end (+ head-start size))
	       coding-system head-found tail-found pos)
	  ;; Try a short cut by searching for the string "coding:"
	  ;; and for "unibyte:" at th ehead and tail of SIZE bytes.
	  (setq head-found (or (search-forward "coding:" head-end t)
			       (search-forward "unibyte:" head-end t)))
	  (if (and head-found (> head-found tail-start))
	      ;; Head and tail are overlapped.
	      (setq tail-found head-found)
	    (goto-char tail-start)
	    (setq tail-found (or (search-forward "coding:" tail-end t)
				 (search-forward "unibyte:" tail-end t))))

	  ;; At first check the head.
	  (when head-found
	    (goto-char head-start)
	    (setq pos (re-search-forward "[\n\r]" head-end t))
	    (if (and pos
		     (= (char-after head-start) ?#)
		     (= (char-after (1+ head-start)) ?!))
		;; If the file begins with "#!" (exec interpreter magic),
		;; look for coding frobs in the first two lines.  You cannot
		;; necessarily put them in the first line of such a file
		;; without screwing up the interpreter invocation.
		(setq pos (search-forward "\n" head-end t)))
	    (if pos (setq head-end pos))
	    (when (< head-found head-end)
	      (goto-char head-start)
	      (when (and set-auto-coding-for-load
			 (re-search-forward
			  "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
			  head-end t))
		(setq coding-system 'raw-text))
	      (when (and (not coding-system)
			 (re-search-forward
			  "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
			  head-end t))
		(setq coding-system (intern (match-string 2)))
		(or (coding-system-p coding-system)
		    (setq coding-system nil)))))

	  ;; If no coding: tag in the head, check the tail.
	  (when (and tail-found (not coding-system))
	    (goto-char tail-start)
	    (search-forward "\n\^L" nil t)
	    (if (re-search-forward
		 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
		;; The prefix is what comes before "local variables:" in its
		;; line.  The suffix is what comes after "local variables:"
		;; in its line.
		(let* ((prefix (regexp-quote (match-string 1)))
		       (suffix (regexp-quote (match-string 2)))
		       (re-coding (concat
				   "^" prefix
				   "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
				   suffix "$"))
		       (re-unibyte (concat
				    "^" prefix
				    "unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
				    suffix "$"))
		       (re-end (concat
				"^" prefix "end *:[ \t]*" suffix "$"))
		       (pos (point)))
		  (re-search-forward re-end tail-end 'move)
		  (setq tail-end (point))
		  (goto-char pos)
		  (when (and set-auto-coding-for-load
			     (re-search-forward re-unibyte tail-end t))
		    (setq coding-system 'raw-text))
		  (when (and (not coding-system)
			     (re-search-forward re-coding tail-end t))
		    (setq coding-system (intern (match-string 1)))
		    (or (coding-system-p coding-system)
			(setq coding-system nil))))))
	  coding-system))))
896 897

(setq set-auto-coding-function 'set-auto-coding)
898

Karl Heuer's avatar
Karl Heuer committed
899 900 901 902 903 904 905
;; Set buffer-file-coding-system of the current buffer after some text
;; is inserted.
(defun after-insert-file-set-buffer-file-coding-system (inserted)
  (if last-coding-system-used
      (let ((coding-system
	     (find-new-buffer-file-coding-system last-coding-system-used))
	    (modified-p (buffer-modified-p)))
906 907
	(when coding-system
	  (set-buffer-file-coding-system coding-system)
908 909 910 911 912 913
	  (if (and (or (eq coding-system 'no-conversion)
		       (eq (coding-system-type coding-system) 5))
		   ;; If buffer was unmodified, we must be visiting it.
		   (not modified-p))
	      ;; For coding systems no-conversion and raw-text...,
	      ;; edit the buffer as unibyte.
914
	      (set-buffer-multibyte nil))
915
	  (set-buffer-modified-p modified-p))))
Karl Heuer's avatar
Karl Heuer committed
916 917
  nil)

Karl Heuer's avatar
Karl Heuer committed
918 919
(add-hook 'after-insert-file-functions
	  'after-insert-file-set-buffer-file-coding-system)
Karl Heuer's avatar
Karl Heuer committed
920

921
;; The coding-spec and eol-type of coding-system returned is decided
Karl Heuer's avatar
Karl Heuer committed
922 923 924 925 926 927
;; independently in the following order.
;;	1. That of buffer-file-coding-system locally bound.
;;	2. That of CODING.

(defun find-new-buffer-file-coding-system (coding)
  "Return a coding system for a buffer when a file of CODING is inserted.
928 929
The local variable `buffer-file-coding-system' of the current buffer
is set to the returned value.
930
Return nil if there's no need of setting new buffer-file-coding-system."
Karl Heuer's avatar
Karl Heuer committed
931
  (let (local-coding local-eol
932
	found-coding found-eol
Karl Heuer's avatar
Karl Heuer committed
933 934 935 936 937
	new-coding new-eol)
    (if (null coding)
	;; Nothing found about coding.
	nil

938 939 940 941 942 943
      ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
      ;; and LOCAL-CODING.
      (setq local-eol (coding-system-eol-type buffer-file-coding-system))
      (if (null (numberp local-eol))
	  ;; But eol-type is not yet set.
	  (setq local-eol nil))
944 945 946 947
      (if (and buffer-file-coding-system
	       (not (eq (coding-system-type buffer-file-coding-system) t)))
	  ;; This is not `undecided'.
	  (setq local-coding (coding-system-base buffer-file-coding-system)))
948 949 950

      (if (and (local-variable-p 'buffer-file-coding-system)
	       local-eol local-coding)
Karl Heuer's avatar
Karl Heuer committed
951 952 953 954
	  ;; The current buffer has already set full coding-system, we
	  ;; had better not change it.
	  nil

955
	(setq found-eol (coding-system-eol-type coding))
Karl Heuer's avatar
Karl Heuer committed
956 957 958
	(if (null (numberp found-eol))
	    ;; But eol-type is not found.
	    (setq found-eol nil))
959 960 961
	(if (not (eq (coding-system-type coding) t))
	    ;; This is not `undecided'.
	    (setq found-coding (coding-system-base coding)))
Karl Heuer's avatar
Karl Heuer committed
962 963

	;; The local setting takes precedence over the found one.
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	(setq new-coding (or (and (local-variable-p 'buffer-file-coding-system)
				  local-coding)
			     found-coding
			     local-coding))
	(setq new-eol (or (and (local-variable-p 'buffer-file-coding-system)
			       local-eol)
			  found-eol
			  local-eol))
	(when (numberp new-eol)
	  (or new-coding
	      (setq new-coding 'undecided))
	  (if (vectorp (coding-system-eol-type new-coding))
	      (setq new-coding
		    (aref (coding-system-eol-type new-coding) new-eol))))
	;; Return a new coding system only when it is different from
	;; the current one.
	(if (not (eq buffer-file-coding-system new-coding))
	    new-coding)))))
Karl Heuer's avatar
Karl Heuer committed
982

983 984
(defun modify-coding-system-alist (target-type regexp coding-system)
  "Modify one of look up tables for finding a coding system on I/O operation.
985 986
There are three of such tables, `file-coding-system-alist',
`process-coding-system-alist', and `network-coding-system-alist'.
987 988

TARGET-TYPE specifies which of them to modify.
989 990 991
If it is `file', it affects `file-coding-system-alist' (which see).
If it is `process', it affects `process-coding-system-alist' (which see).
If it is `network', it affects `network-codign-system-alist' (which see).
992 993 994 995 996 997 998

REGEXP is a regular expression matching a target of I/O operation.
The target is a file name if TARGET-TYPE is `file', a program name if
TARGET-TYPE is `process', or a network service name or a port number
to connect to if TARGET-TYPE is `network'.

CODING-SYSTEM is a coding system to perform code conversion on the I/O
999 1000 1001
operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
for decoding and encoding respectively,
or a function symbol which, when called, returns such a cons cell."
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  (or (memq target-type '(file process network))
      (error "Invalid target type: %s" target-type))
  (or (stringp regexp)
      (and (eq target-type 'network) (integerp regexp))
      (error "Invalid regular expression: %s" regexp))
  (if (symbolp coding-system)
      (if (not (fboundp coding-system))
	  (progn
	    (check-coding-system coding-system)
	    (setq coding-system (cons coding-system coding-system))))
    (check-coding-system (car coding-system))
    (check-coding-system (cdr coding-system)))
  (cond ((eq target-type 'file)
	 (let ((slot (assoc regexp file-coding-system-alist)))
	   (if slot
	       (setcdr slot coding-system)
	     (setq file-coding-system-alist
		   (cons (cons regexp coding-system)
			 file-coding-system-alist)))))
	((eq target-type 'process)
	 (let ((slot (assoc regexp process-coding-system-alist)))
	   (if slot
	       (setcdr slot coding-system)
	     (setq process-coding-system-alist
		   (cons (cons regexp coding-system)
			 process-coding-system-alist)))))
	(t
	 (let ((slot (assoc regexp network-coding-system-alist)))
	   (if slot
	       (setcdr slot coding-system)
	     (setq network-coding-system-alist
		   (cons (cons regexp coding-system)
			 network-coding-system-alist)))))))

1036
(defun make-translation-table (&rest args)
1037
  "Make a translation table (char table) from arguments.
1038
Each argument is a list of the form (FROM . TO),
1039
where FROM is a character to be translated to TO.
1040

1041 1042 1043
FROM can be a generic character (see `make-char').  In this case, TO is
a generic character containing the same number of characters, or a
ordinary character.  If FROM and TO are both generic characters, all
1044
characters belonging to FROM are translated to characters belonging to TO
1045
without changing their position code(s)."
1046
  (let ((table (make-char-table 'translation-table))
1047 1048 1049 1050
	revlist)
    (while args
      (let ((elts (car args)))
	(while elts
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	  (let* ((from (car (car elts)))
		 (from-i 0)		; degree of freedom of FROM
		 (from-rev (nreverse (split-char from)))
		 (to (cdr (car elts)))
		 (to-i 0)		; degree of freedom of TO
		 (to-rev (nreverse (split-char to))))
	    ;; Check numbers of heading 0s in FROM-REV and TO-REV.
	    (while (eq (car from-rev) 0)
	      (setq from-i (1+ from-i) from-rev (cdr from-rev)))
	    (while (eq (car to-rev) 0)
	      (setq to-i (1+ to-i) to-rev (cdr to-rev)))
	    (if (and (/= from-i to-i) (/= to-i 0))
		(error "Invalid character pair (%d . %d)" from to))
1064 1065 1066
	    ;; If we have already translated TO to TO-ALT, FROM should
	    ;; also be translated to TO-ALT.  But, this is only if TO
	    ;; is a generic character or TO-ALT is not a generic
1067 1068 1069 1070 1071 1072 1073 1074
	    ;; character.
	    (let ((to-alt (aref table to)))
	      (if (and to-alt
		       (or (> to-i 0) (not (generic-char-p to-alt))))
		  (setq to to-alt)))
	    (if (> from-i 0)
		(set-char-table-default table from to)
	      (aset table from to))
1075 1076
	    ;; If we have already translated some chars to FROM, they
	    ;; should also be translated to TO.
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	    (let ((l (assq from revlist