mule.el 64.5 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.
Gerd Moellmann's avatar
Gerd Moellmann committed
4
;; Copyright (C) 2001 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5
;; Licensed to the Free Software Foundation.
Karl Heuer's avatar
Karl Heuer committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

;; 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
22 23 24
;; 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
25 26 27

;;; Code:

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

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

(defun load-with-code-conversion (fullname file &optional noerror nomessage)
35 36
  "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
37 38 39 40 41 42 43 44 45 46 47 48
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.
49 50 51 52 53 54
	    ;; 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
55 56 57
	      ;; We can't use `generate-new-buffer' because files.el
	      ;; is not yet loaded.
	      (get-buffer-create (generate-new-buffer-name " *load*"))))
58 59 60 61 62 63 64 65
	   (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
66
      (unwind-protect
67
	  (let ((load-file-name fullname)
68
		(set-auto-coding-for-load t)
69
		(inhibit-file-name-operation nil))
Karl Heuer's avatar
Karl Heuer committed
70 71 72
	    (save-excursion
	      (set-buffer buffer)
	      (insert-file-contents fullname)
73 74 75 76 77 78 79 80
	      ;; If the loaded file was inserted with no-conversion or
	      ;; raw-text coding system, make the buffer unibyte.
	      ;; Otherwise, eval-buffer might try to interpret random
	      ;; binary junk as multibyte characters.
	      (if (and enable-multibyte-characters
		       (or (eq (coding-system-type last-coding-system-used) 5)
			   (eq last-coding-system-used 'no-conversion)))
		  (set-buffer-multibyte nil))
Karl Heuer's avatar
Karl Heuer committed
81 82
	      ;; Make `kill-buffer' quiet.
	      (set-buffer-modified-p nil))
83
	    ;; Have the original buffer current while we eval.
84 85 86 87
	    (eval-buffer buffer nil file
			 ;; If this Emacs is running with --unibyte,
			 ;; convert multibyte strings to unibyte
			 ;; after reading them.
88
;;			 (not default-enable-multibyte-characters)
89
			 nil t
90
			 ))
91 92
	(let (kill-buffer-hook kill-buffer-query-functions)
	  (kill-buffer buffer)))
Karl Heuer's avatar
Karl Heuer committed
93
      (let ((hook (assoc file after-load-alist)))
94 95 96 97 98 99
	(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
100 101 102 103
      t)))

;; API (Application Program Interface) for charsets.

104
(defsubst charset-quoted-standard-p (obj)
Dave Love's avatar
Dave Love committed
105
  "Return t if OBJ is a quoted symbol, and is the name of a standard charset."
106 107 108 109 110
  (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
111 112

(defsubst charsetp (object)
113
  "T if OBJECT is a charset."
Karl Heuer's avatar
Karl Heuer committed
114 115 116 117 118 119 120 121 122 123 124 125
  (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.
126 127
BYTES (integer) is the length of multi-byte form of a character in
  the charset: one of 1, 2, 3, and 4.
Karl Heuer's avatar
Karl Heuer committed
128 129 130 131 132 133
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.
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
134 135
  charset when rendering.  If 0, render from left to right, else
  render from right to left.
Karl Heuer's avatar
Karl Heuer committed
136 137 138 139 140
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
Kenichi Handa's avatar
Kenichi Handa committed
141 142
  corresponding ISO 2022 charset.  If the charset is not assigned
  any final character, the value is -1.
Karl Heuer's avatar
Karl Heuer committed
143 144 145
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).
Kenichi Handa's avatar
Kenichi Handa committed
146
  If the charset is not assigned any final character, the value is -1.
Karl Heuer's avatar
Karl Heuer committed
147 148 149 150 151 152 153 154 155 156 157
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))

158 159 160 161
;; It is better not to use backquote in this file,
;; because that makes a bootstrapping problem
;; if you need to recompile all the Lisp files using interpreted code.

Karl Heuer's avatar
Karl Heuer committed
162 163
(defmacro charset-id (charset)
  "Return charset identification number of CHARSET."
164
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
165
      (aref (charset-info (nth 1 charset)) 0)
166
    (list 'aref (list 'charset-info charset) 0)))
Karl Heuer's avatar
Karl Heuer committed
167 168

(defmacro charset-bytes (charset)
169 170
  "Return bytes of CHARSET.
See the function `charset-info' for more detail."
171
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
172
      (aref (charset-info (nth 1 charset)) 1)
173
    (list 'aref (list 'charset-info charset) 1)))
Karl Heuer's avatar
Karl Heuer committed
174 175

(defmacro charset-dimension (charset)
176 177
  "Return dimension of CHARSET.
See the function `charset-info' for more detail."
178
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
179
      (aref (charset-info (nth 1 charset)) 2)
180
    (list 'aref (list 'charset-info charset) 2)))
Karl Heuer's avatar
Karl Heuer committed
181 182

(defmacro charset-chars (charset)
183 184
  "Return character numbers contained in a dimension of CHARSET.
See the function `charset-info' for more detail."
185
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
186
      (aref (charset-info (nth 1 charset)) 3)
187
    (list 'aref (list 'charset-info charset) 3)))
Karl Heuer's avatar
Karl Heuer committed
188 189

(defmacro charset-width (charset)
190 191
  "Return width (how many column occupied on a screen) of CHARSET.
See the function `charset-info' for more detail."
192
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
193
      (aref (charset-info (nth 1 charset)) 4)
194
    (list 'aref (list 'charset-info charset) 4)))
Karl Heuer's avatar
Karl Heuer committed
195 196

(defmacro charset-direction (charset)
197 198
  "Return direction of CHARSET.
See the function `charset-info' for more detail."
199
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
200
      (aref (charset-info (nth 1 charset)) 5)
201
    (list 'aref (list 'charset-info charset) 5)))
Karl Heuer's avatar
Karl Heuer committed
202 203

(defmacro charset-iso-final-char (charset)
204 205
  "Return final char of CHARSET.
See the function `charset-info' for more detail."
206
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
207
      (aref (charset-info (nth 1 charset)) 8)
208
    (list 'aref (list 'charset-info charset) 8)))
Karl Heuer's avatar
Karl Heuer committed
209 210

(defmacro charset-iso-graphic-plane (charset)
211 212
  "Return graphic plane of CHARSET.
See the function `charset-info' for more detail."
213
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
214
      (aref (charset-info (nth 1 charset)) 9)
215
    (list 'aref (list 'charset-info charset) 9)))
Karl Heuer's avatar
Karl Heuer committed
216 217

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

(defmacro charset-short-name (charset)
225 226
  "Return short name of CHARSET.
See the function `charset-info' for more detail."
227
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
228
      (aref (charset-info (nth 1 charset)) 11)
229
    (list 'aref (list 'charset-info charset) 11)))
Karl Heuer's avatar
Karl Heuer committed
230 231

(defmacro charset-long-name (charset)
232 233
  "Return long name of CHARSET.
See the function `charset-info' for more detail."
234
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
235
      (aref (charset-info (nth 1 charset)) 12)
236
    (list 'aref (list 'charset-info charset) 12)))
Karl Heuer's avatar
Karl Heuer committed
237 238

(defmacro charset-description (charset)
Paul Eggert's avatar
Paul Eggert committed
239
  "Return description of CHARSET.
240
See the function `charset-info' for more detail."
241
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
242
      (aref (charset-info (nth 1 charset)) 13)
243
    (list 'aref (list 'charset-info charset) 13)))
Karl Heuer's avatar
Karl Heuer committed
244 245

(defmacro charset-plist (charset)
246 247
  "Return list charset property of CHARSET.
See the function `charset-info' for more detail."
248 249 250 251 252
  (list 'aref
	(if (charset-quoted-standard-p charset)
	    (charset-info (nth 1 charset))
	  (list 'charset-info charset))
	14))
Karl Heuer's avatar
Karl Heuer committed
253 254

(defun set-charset-plist (charset plist)
Karl Heuer's avatar
Karl Heuer committed
255
  "Set CHARSET's property list to PLIST, and return PLIST."
Karl Heuer's avatar
Karl Heuer committed
256 257
  (aset (charset-info  charset) 14 plist))

Eli Zaretskii's avatar
Eli Zaretskii committed
258 259
(defun make-char (charset &optional code1 code2)
  "Return a character of CHARSET whose position codes are CODE1 and CODE2.
Kenichi Handa's avatar
Kenichi Handa committed
260
CODE1 and CODE2 are optional, but if you don't supply
261 262
sufficient position codes, return a generic character which stands for
all characters or group of characters in the character set.
Eli Zaretskii's avatar
Eli Zaretskii committed
263
A generic character can be used to index a char table (e.g. syntax-table).
264 265 266 267 268 269

Such character sets as ascii, eight-bit-control, and eight-bit-graphic
don't have corresponding generic characters.  If CHARSET is one of
them and you don't supply CODE1, return the character of the smallest
code in CHARSET.

Eli Zaretskii's avatar
Eli Zaretskii committed
270
If CODE1 or CODE2 are invalid (out of range), this function signals an error."
Eli Zaretskii's avatar
Eli Zaretskii committed
271
  (make-char-internal (charset-id charset) code1 code2))
272 273

(put 'make-char 'byte-compile
Dave Love's avatar
Dave Love committed
274
     (function
275 276 277 278 279 280 281 282 283
      (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
284

285
(defun charset-list ()
286 287
  "Return list of charsets ever defined.

288
This function is provided for backward compatibility.
289
Now we have the variable `charset-list'."
290 291
  charset-list)

292 293
(defsubst generic-char-p (char)
  "Return t if and only if CHAR is a generic character.
Dave Love's avatar
Dave Love committed
294
See also the documentation of `make-char'."
295 296 297 298
  (and (>= char 0400)
       (let ((l (split-char char)))
	 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
	      (not (eq (car l) 'composition))))))
299

300
(defun decode-char (ccs code-point &optional restriction)
301
  "Return character specified by coded character set CCS and CODE-POINT in it.
302
Return nil if such a character is not supported.
303 304
Currently the only supported coded character set is `ucs' (ISO/IEC
10646: Universal Multi-Octet Coded Character Set).
305 306 307 308

Optional argument RESTRICTION specifies a way to map the pair of CCS
and CODE-POINT to a chracter.   Currently not supported and just ignored."
  (cond ((eq ccs 'ucs)
Kenichi Handa's avatar
Kenichi Handa committed
309
	 (cond ((< code-point 160)
310 311 312 313 314
		code-point)
	       ((< code-point 256)
		(make-char 'latin-iso8859-1 code-point))
	       ((< code-point #x2500)
		(setq code-point (- code-point #x0100))
Dave Love's avatar
Dave Love committed
315
		(make-char 'mule-unicode-0100-24ff
316
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
Kenichi Handa's avatar
Kenichi Handa committed
317
	       ((< code-point #x3400)
318 319 320 321 322 323 324 325 326 327
		(setq code-point (- code-point #x2500))
		(make-char 'mule-unicode-2500-33ff
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
	       ((and (>= code-point #xe000) (< code-point #x10000))
		(setq code-point (- code-point #xe000))
		(make-char 'mule-unicode-e000-ffff
			   (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
	       ))))

(defun encode-char (char ccs &optional restriction)
328
  "Return code-point in coded character set CCS that corresponds to CHAR.
329
Return nil if CHAR is not included in CCS.
330 331
Currently the only supported coded character set is `ucs' (ISO/IEC
10646: Universal Multi-Octet Coded Character Set).
332 333

CHAR should be in one of these charsets:
334
  ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
Kenichi Handa's avatar
Kenichi Handa committed
335
  mule-unicode-e000-ffff, eight-bit-control
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
Otherwise, return nil.

Optional argument RESTRICTION specifies a way to map CHAR to a
code-point in CCS.  Currently not supported and just ignored."
  (let* ((split (split-char char))
	 (charset (car split)))
    (cond ((eq ccs 'ucs)
	   (cond ((eq charset 'ascii)
		  char)
		 ((eq charset 'latin-iso8859-1)
		  (+ (nth 1 split) 128))
		 ((eq charset 'mule-unicode-0100-24ff)
		  (+ #x0100 (+ (* (- (nth 1 split) 32) 96)
			       (- (nth 2 split) 32))))
		 ((eq charset 'mule-unicode-2500-33ff)
		  (+ #x2500 (+ (* (- (nth 1 split) 32) 96)
			       (- (nth 2 split) 32))))
		 ((eq charset 'mule-unicode-e000-ffff)
		  (+ #xe000 (+ (* (- (nth 1 split) 32) 96)
Kenichi Handa's avatar
Kenichi Handa committed
355 356 357
			       (- (nth 2 split) 32))))
		 ((eq charset 'eight-bit-control)
		  char))))))
358

359

360
;; Coding system stuff
Karl Heuer's avatar
Karl Heuer committed
361

362
;; Coding system is a symbol that has the property `coding-system'.
Karl Heuer's avatar
Karl Heuer committed
363
;;
364 365
;; The value of the property `coding-system' is a vector of the
;; following format:
366
;;	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
367
;; We call this vector as coding-spec.  See comments in src/coding.c
Dave Love's avatar
Dave Love committed
368
;; for more detail.
369 370 371 372

(defconst coding-spec-type-idx 0)
(defconst coding-spec-mnemonic-idx 1)
(defconst coding-spec-doc-string-idx 2)
373
(defconst coding-spec-plist-idx 3)
374 375
(defconst coding-spec-flags-idx 4)

376 377 378 379
;; 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.
380 381 382 383
;;
;; o coding-category
;;
;; The value is a coding category the coding system belongs to.  The
Dave Love's avatar
Dave Love committed
384
;; function `make-coding-system' sets this value automatically
385
;; unless its argument PROPERTIES specifies this property.
Karl Heuer's avatar
Karl Heuer committed
386
;;
387
;; o alias-coding-systems
Karl Heuer's avatar
Karl Heuer committed
388
;;
389 390
;; 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
391 392
;; `base coding system'.  The function `make-coding-system' sets this
;; value automatically and `define-coding-system-alias' updates it.
393
;;
Kenichi Handa's avatar
Kenichi Handa committed
394 395
;; See the documentation of make-coding-system for the meanings of the
;; following properties.
Karl Heuer's avatar
Karl Heuer committed
396
;;
Kenichi Handa's avatar
Kenichi Handa committed
397
;; o post-read-conversion
398
;; o pre-write-conversion
399 400
;; o translation-table-for-decode
;; o translation-table-for-encode
401
;; o safe-chars
402
;; o safe-charsets
403
;; o mime-charset
404 405
;; o valid-codes (meaningful only for a coding system based on CCL)

406 407

(defsubst coding-system-spec (coding-system)
Dave Love's avatar
Dave Love committed
408
  "Return coding-spec of CODING-SYSTEM."
409
  (get (check-coding-system coding-system) 'coding-system))
Karl Heuer's avatar
Karl Heuer committed
410 411

(defun coding-system-type (coding-system)
412 413 414 415
  "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
416 417

(defun coding-system-mnemonic (coding-system)
418
  "Return the mnemonic character of CODING-SYSTEM.
419 420 421 422
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
423

424
(defun coding-system-doc-string (coding-system)
425 426
  "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
427

428
(defun coding-system-plist (coding-system)
429 430
  "Return the property list of CODING-SYSTEM."
  (aref (coding-system-spec coding-system) coding-spec-plist-idx))
431

Karl Heuer's avatar
Karl Heuer committed
432
(defun coding-system-flags (coding-system)
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  "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)
Dave Love's avatar
Dave Love committed
452 453
  "Return the coding category of CODING-SYSTEM.
See also `coding-category-list'."
454 455 456 457
  (coding-system-get coding-system 'coding-category))

(defun coding-system-base (coding-system)
  "Return the base coding system of CODING-SYSTEM.
458 459
A base coding system is what made by `make-coding-system'.
Any alias nor subsidiary coding systems are not base coding system."
460 461 462
  (car (coding-system-get coding-system 'alias-coding-systems)))

(defalias 'coding-system-parent 'coding-system-base)
463
(make-obsolete 'coding-system-parent 'coding-system-base "20.3")
464 465 466 467 468 469 470 471 472 473

;; 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
474

475
(defun coding-system-eol-type (coding-system)
476 477
  "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
478

479 480 481 482 483 484 485
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))
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500
(defun coding-system-lessp (x y)
  (cond ((eq x 'no-conversion) t)
	((eq y 'no-conversion) nil)
	((eq x 'emacs-mule) t)
	((eq y 'emacs-mule) nil)
	((eq x 'undecided) t)
	((eq y 'undecided) nil)
	(t (let ((c1 (coding-system-mnemonic x))
		 (c2 (coding-system-mnemonic y)))
	     (or (< (downcase c1) (downcase c2))
		 (and (not (> (downcase c1) (downcase c2)))
		      (< c1 c2)))))))

(defun add-to-coding-system-list (coding-system)
Dave Love's avatar
Dave Love committed
501
  "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
502 503 504 505 506 507 508 509 510 511 512 513 514
  (if (or (null coding-system-list)
	  (coding-system-lessp coding-system (car coding-system-list)))
      (setq coding-system-list (cons coding-system coding-system-list))
    (let ((len (length coding-system-list))
	  mid (tem coding-system-list))
      (while (> len 1)
	(setq mid (nthcdr (/ len 2) tem))
	(if (coding-system-lessp (car mid) coding-system)
	    (setq tem mid
		  len (- len (/ len 2)))
	  (setq len (/ len 2))))
      (setcdr tem (cons coding-system (cdr tem))))))

515
(defun coding-system-list (&optional base-only)
516 517 518 519 520
  "Return a list of all existing non-subsidiary coding systems.
If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
The value doesn't include subsidiary coding systems which are what
made from bases and aliases automatically for various end-of-line
formats (e.g. iso-latin-1-unix, koi8-r-dos)."
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  (let* ((codings (copy-sequence coding-system-list))
	 (tail (cons nil codings)))
    ;; Remove subsidiary coding systems (eol variants) and alias
    ;; coding systems (if necessary).
    (while (cdr tail)
      (let* ((coding (car (cdr tail)))
	     (aliases (coding-system-get coding 'alias-coding-systems)))
	(if (or
	     ;; CODING is an eol variant if not in ALIASES.
	     (not (memq coding aliases))
	     ;; CODING is an alias if it is not car of ALIASES.
	     (and base-only (not (eq coding (car aliases)))))
	    (setcdr tail (cdr (cdr tail)))
	  (setq tail (cdr tail)))))
    codings))

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
(defun register-char-codings (coding-system safe-chars)
  (let ((general (char-table-extra-slot char-coding-system-table 0)))
    (if (eq safe-chars t)
	(or (memq coding-system general)
	    (set-char-table-extra-slot char-coding-system-table 0
				       (cons coding-system general)))
      (map-char-table
       (function
	(lambda (key val)
	  (if (and (>= key 128) val)
	      (let ((codings (aref char-coding-system-table key)))
		(or (memq coding-system codings)
		    (aset char-coding-system-table key
			  (cons coding-system codings)))))))
       safe-chars))))


554
(defun make-subsidiary-coding-system (coding-system)
Dave Love's avatar
Dave Love committed
555
  "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
556 557
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
558 559
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
560 561
	(i 0)
	temp)
562
    (while (< i 3)
563
      (put (aref subsidiaries i) 'coding-system coding-spec)
564
      (put (aref subsidiaries i) 'eol-type i)
565
      (add-to-coding-system-list (aref subsidiaries i))
566 567 568
      (setq coding-system-alist
	    (cons (list (symbol-name (aref subsidiaries i)))
		  coding-system-alist))
569 570
      (setq i (1+ i)))
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
(defun transform-make-coding-system-args (name type &optional doc-string props)
  "For internal use only.
Transform XEmacs style args for `make-coding-system' to Emacs style.
Value is a list of transformed arguments."
  (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
	(eol-type (plist-get props 'eol-type))
	properties tmp)
    (cond
     ((eq eol-type 'lf) (setq eol-type 'unix))
     ((eq eol-type 'crlf) (setq eol-type 'dos))
     ((eq eol-type 'cr) (setq eol-type 'mac)))
    (if (setq tmp (plist-get props 'post-read-conversion))
	(setq properties (plist-put properties 'post-read-conversion tmp)))
    (if (setq tmp (plist-get props 'pre-write-conversion))
	(setq properties (plist-put properties 'pre-write-conversion tmp)))
    (cond
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
     ((eq type 'shift-jis)
      `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
     ((eq type 'iso2022) ; This is not perfect.
      (if (plist-get props 'escape-quoted)
	  (error "escape-quoted is not supported: %S"
		 `(,name ,type ,doc-string ,props)))
      (let ((g0 (plist-get props 'charset-g0))
      	    (g1 (plist-get props 'charset-g1))
      	    (g2 (plist-get props 'charset-g2))
      	    (g3 (plist-get props 'charset-g3))
      	    (use-roman
             (and
	      (eq (cadr (assoc 'latin-jisx0201
			       (plist-get props 'input-charset-conversion)))
		  'ascii)
	      (eq (cadr (assoc 'ascii
			       (plist-get props 'output-charset-conversion)))
		  'latin-jisx0201)))
            (use-oldjis
             (and
	      (eq (cadr (assoc 'japanese-jisx0208-1978
			       (plist-get props 'input-charset-conversion)))
		  'japanese-jisx0208)
	      (eq (cadr (assoc 'japanese-jisx0208
			       (plist-get props 'output-charset-conversion)))
		  'japanese-jisx0208-1978))))
	(if (charsetp g0)
	    (if (plist-get props 'force-g0-on-output)
		(setq g0 `(nil ,g0))
	      (setq g0 `(,g0 t))))
	(if (charsetp g1)
	    (if (plist-get props 'force-g1-on-output)
		(setq g1 `(nil ,g1))
	      (setq g1 `(,g1 t))))
	(if (charsetp g2)
	    (if (plist-get props 'force-g2-on-output)
		(setq g2 `(nil ,g2))
	      (setq g2 `(,g2 t))))
	(if (charsetp g3)
	    (if (plist-get props 'force-g3-on-output)
		(setq g3 `(nil ,g3))
	      (setq g3 `(,g3 t))))
	`(,name 2 ,mnemonic ,doc-string
	  (,g0 ,g1 ,g2 ,g3
	   ,(plist-get props 'short)
	   ,(not (plist-get props 'no-ascii-eol))
	   ,(not (plist-get props 'no-ascii-cntl))
	   ,(plist-get props 'seven)
	   t
	   ,(not (plist-get props 'lock-shift))
	   ,use-roman
	   ,use-oldjis
	   ,(plist-get props 'no-iso6429)
	   nil nil nil nil)
	,properties ,eol-type)))
     ((eq type 'big5)
      `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
645
     ((eq type 'ccl)
646
      `(,name 4 ,mnemonic ,doc-string
647
	      (,(plist-get props 'decode) . ,(plist-get props 'encode))
648
	      ,properties ,eol-type))
649
     (t
650
      (error "unsupported XEmacs style make-coding-style arguments: %S"
651 652
	     `(,name ,type ,doc-string ,props))))))

653
(defun make-coding-system (coding-system type mnemonic doc-string
654 655 656 657
					 &optional
					 flags
					 properties
					 eol-type)
658
  "Define a new coding system CODING-SYSTEM (symbol).
Dave Love's avatar
Dave Love committed
659
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
660
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
661
in the following format:
662
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
663 664

TYPE is an integer value indicating the type of the coding system as follows:
Karl Heuer's avatar
Karl Heuer committed
665 666 667 668
  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,
669
  4: private, CCL programs provide encoding/decoding algorithm,
Dave Love's avatar
Dave Love committed
670
  5: Raw-text, which means that text contains random 8-bit codes.
671

672
MNEMONIC is a character to be displayed on mode line for the coding system.
673

674
DOC-STRING is a documentation string for the coding system.
675

676
FLAGS specifies more detailed information of the coding system as follows:
677

678
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
679 680
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
681
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
682
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
683 684 685 686 687
    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.
688
        But, character set `ascii' can be designated only to G0.
Karl Heuer's avatar
Karl Heuer committed
689 690 691 692 693 694 695 696 697 698
    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.
699 700 701 702
    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.
703
    SAFE non-nil means convert unsafe characters to `?' on output.
704 705
      Characters not specified in the property `safe-charsets' nor
      `safe-chars' are unsafe.
706 707 708
    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.
709

710 711 712
  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.
713

714 715 716 717 718 719 720
PROPERTIES is an alist of properties vs the corresponding values.  The
following properties are recognized:

  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
Dave Love's avatar
Dave Love committed
721 722 723 724
  `after-insert-functions' are called.  The argument of this
  function is the same as for a function in
  `after-insert-file-functions', i.e. LENGTH of the text inserted,
  with point at the head of the text to be decoded.
725 726 727 728 729 730
 
  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.
Dave Love's avatar
Dave Love committed
731 732
  The arguments to this function are the same as those of a function
  in `write-region-annotate-functions', i.e. FROM and TO, specifying
Dave Love's avatar
Dave Love committed
733
  a region of text.
734 735 736 737 738
 
  o translation-table-for-decode
 
  The value is a translation table to be applied on decoding.  See
  the function `make-translation-table' for the format of translation
Dave Love's avatar
Dave Love committed
739
  table.  This is not applicable to type 4 (CCL-based) coding systems.
740 741 742
 
  o translation-table-for-encode
 
Dave Love's avatar
Dave Love committed
743 744
  The value is a translation table to be applied on encoding.  This is
  not applicable to type 4 (CCL-based) coding systems.
745 746 747 748 749 750 751 752
 
  o safe-chars
 
  The value is a char table.  If a character has non-nil value in it,
  the character is safely supported by the coding system.  This
  overrides the specification of safe-charsets.

  o safe-charsets
Dave Love's avatar
Dave Love committed
753

754 755 756
  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
Dave Love's avatar
Dave Love committed
757 758
  mean that the charset can't be encoded in the coding system;
  it just means that some other receiver of text encoded
759 760 761 762 763 764 765 766 767 768 769 770 771 772
  in the coding system won't be able to handle that charset.
 
  o mime-charset
 
  The value is a symbol of which name is `MIME-charset' parameter of
  the coding system.
 
  o valid-codes (meaningful only for a coding system based on CCL)
 
  The value is a list to indicate valid byte ranges of the encoded
  file.  Each element of the list is an integer or a cons of integer.
  In the former case, the integer value is a valid byte code.  In the
  latter case, the integers specifies the range of valid byte codes.

773 774 775
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
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
EOL-TYPE specifies the EOL type of the coding-system in one of the
following formats:

  o symbol (unix, dos, or mac)

	The symbol `unix' means Unix-like EOL (LF), `dos' means
	DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).

  o number (0, 1, or 2)

	The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
	respectively.

  o vector of coding-systems of length 3

	The EOL type is detected automatically for the coding system.
	And, according to the detected EOL type, one of the coding
	systems in the vector is selected.  Elements of the vector
Stefan Monnier's avatar
Typo.  
Stefan Monnier committed
795
	corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
796 797
	in this order.

798 799 800 801 802 803 804
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."
805 806

  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
Eli Zaretskii's avatar
Eli Zaretskii committed
807 808
  ;; is a symbol, perhaps, this function is called with XEmacs-style
  ;; arguments.  Here, try to transform that kind of arguments to
809 810 811 812 813
  ;; Emacs style.
  (if (symbolp type)
      (let ((args (transform-make-coding-system-args coding-system type
						     mnemonic doc-string)))
	(setq coding-system (car args)
Dave Love's avatar
Dave Love committed
814
	      type (nth 1 args)
815 816 817 818 819 820
	      mnemonic (nth 2 args)
	      doc-string (nth 3 args)
	      flags (nth 4 args)
	      properties (nth 5 args)
	      eol-type (nth 6 args))))

821
  ;; Set a value of `coding-system' property.
822
  (let ((coding-spec (make-vector 5 nil))
823 824
	(no-initial-designation t)
	(no-alternative-designation t)
825
	(accept-latin-extra-code nil)
826
	coding-category)
827
    (if (or (not (integerp type)) (< type 0) (> type 5))
828
	(error "TYPE argument must be 0..5"))
829
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
Dave Love's avatar
Dave Love committed
830
	(error "MNEMONIC argument must be an ASCII printable character"))
831 832 833
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
834
	  (purecopy (if (stringp doc-string) doc-string "")))
835 836 837 838 839
    (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
840
	   (let ((i 0)
841
		 (vec (make-vector 32 nil))
842 843
		 (g1-designation nil)
		 (fl flags))
Karl Heuer's avatar
Karl Heuer committed
844
	     (while (< i 4)
845
	       (let ((charset (car fl)))
846 847 848 849 850 851 852 853 854 855 856 857 858
		 (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))
859 860 861 862
			   (if (eq elt t)
			       (setq no-alternative-designation nil)
			     (if (and elt (not (charsetp elt)))
				 (error "Invalid charset: %s" elt)))
863 864
			   (setq tail (cdr tail)))
			 (setq g1-designation (car charset)))
865 866 867 868
		     (if charset
			 (if (eq charset t)
			     (setq no-alternative-designation nil)
			   (error "Invalid charset: %s" charset)))))
Karl Heuer's avatar
Karl Heuer committed
869
		 (aset vec i charset))
870 871 872
	       (setq fl (cdr fl) i (1+ i)))
	     (while (and (< i 32) fl)
	       (aset vec i (car fl))
873 874 875
	       (if (and (= i 16)	; ACCEPT-LATIN-EXTRA-CODE
			(car fl))
		   (setq accept-latin-extra-code t))
876
	       (setq fl (cdr fl) i (1+ i)))
877 878 879
	     (aset coding-spec 4 vec)
	     (setq coding-category
		   (if (aref vec 8)	; Use locking-shift.
880 881
		       (or (and (aref vec 7) 'coding-category-iso-7-else)
			   'coding-category-iso-8-else)
882 883
		     (if (aref vec 7)	; 7-bit only.
			 (if (aref vec 9) ; Use single-shift.
884
			     'coding-category-iso-7-else
885 886 887
			   (if no-alternative-designation
			       'coding-category-iso-7-tight
			     'coding-category-iso-7))
888 889
		       (if (or no-initial-designation
			       (not no-alternative-designation))
890
			   'coding-category-iso-8-else
891 892 893 894 895 896 897
			 (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
898
	   (setq coding-category 'coding-category-ccl)
899 900 901 902 903 904 905 906 907 908 909
	   (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)")))))
910 911
	  (t				; i.e. (= type 5)
	   (setq coding-category 'coding-category-raw-text)))
912 913

    (let ((plist (list 'coding-category coding-category
914
		       'alias-coding-systems (list coding-system))))
915
      (if no-initial-designation
916 917 918 919 920 921
	  (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'.
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
	  (setq properties (list (cons 'safe-charsets properties))))
      ;; In the current version PROPERTIES is a property list.
      ;; Reflect it into PLIST one by one while handling safe-chars
      ;; specially.
      (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
	    (safe-chars (cdr (assq 'safe-chars properties)))
	    (l properties)
	    prop val)
	;; If only safe-charsets is specified, make a char-table from
	;; it, and store that char-table as the value of `safe-chars'.
	(if (and (not safe-chars) safe-charsets)
	    (let (charset)
	      (if (eq safe-charsets t)
		  (setq safe-chars t)
		(setq safe-chars (make-char-table 'safe-chars))
		(while safe-charsets
		  (setq charset (car safe-charsets)
			safe-charsets (cdr safe-charsets))
		  (cond ((eq charset 'ascii)) ; just ignore
			((eq charset 'eight-bit-control)
			 (let ((i 128))
			   (while (< i 160)
			     (aset safe-chars i t)
			     (setq i (1+ i)))))
			((eq charset 'eight-bit-graphic)
			 (let ((i 160))
			   (while (< i 256)
			     (aset safe-chars i t)
			     (setq i (1+ i)))))
			(t
952 953 954 955 956 957 958
			 (aset safe-chars (make-char charset) t))))
		(if accept-latin-extra-code
		    (let ((i 128))
		      (while (< i 160)
			(if (aref latin-extra-code-table i)
			    (aset safe-chars i t))
			(setq i (1+ i))))))
959 960 961 962 963
	      (setq l (cons (cons 'safe-chars safe-chars) l))))
	(while l
	  (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
	  (if (eq prop 'safe-chars)
	      (progn
964 965 966 967 968
		(if (and (symbolp val)
			 (get val 'translation-table))
		    (setq safe-chars (get val 'translation-table)))
		(register-char-codings coding-system safe-chars)
		(setq val safe-chars)))
969
	  (plist-put plist prop val)))
970 971 972
      ;; The property `coding-category' may have been set differently
      ;; through PROPERTIES.
      (setq coding-category (plist-get plist 'coding-category))
973
      (aset coding-spec coding-spec-plist-idx plist))
974 975 976
    (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
977

978
  ;; Next, set a value of `eol-type' property.
979
  (if (not eol-type)
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
      ;; If EOL-TYPE is nil, set a vector of subsidiary coding
      ;; systems, each corresponds to a coding system for the detected
      ;; EOL format.
      (setq eol-type (make-subsidiary-coding-system coding-system)))
  (setq eol-type
	(cond ((or (eq eol-type 'unix) (null eol-type))
	       0)
	      ((eq eol-type 'dos)
	       1)
	      ((eq eol-type 'mac)
	       2)
	      ((or (and (vectorp eol-type)
			(= (length eol-type) 3))
		   (and (numberp eol-type)
			(and (>= eol-type 0)
			     (<= eol-type 2))))
	       eol-type)
	      (t
	       (error "Invalid EOL-TYPE spec:%S" eol-type))))
  (put coding-system 'eol-type eol-type)
1000 1001 1002

  ;; At last, register CODING-SYSTEM in `coding-system-list' and
  ;; `coding-system-alist'.
1003
  (add-to-coding-system-list coding-system)
1004
  (setq coding-system-alist (cons (list (symbol-name coding-system))
1005
				  coding-system-alist))
1006 1007 1008 1009 1010 1011 1012

  ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
  ;; XXX-with-esc variants.
  (let ((coding-category (coding-system-category coding-system)))
    (if (or (eq coding-category 'coding-category-iso-8-1)
	    (eq coding-category 'coding-category-iso-8-2))
	(let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
1013 1014 1015 1016 1017 1018 1019 1020
	      (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
	      (safe-charsets (assq 'safe-charsets properties))
	      (mime-charset (assq 'mime-charset properties)))
	  (if safe-charsets
	      (setcdr safe-charsets t)
	    (setq properties (cons (cons 'safe-charsets t) properties)))
	  (if mime-charset
	      (setcdr mime-charset nil))