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

Kenichi Handa's avatar
Kenichi Handa committed
3 4 5 6 7
;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
;;   Free Software Foundation, Inc.
;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H14PRO021
Karl Heuer's avatar
Karl Heuer committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

;; 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
24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
25 26
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Karl Heuer's avatar
Karl Heuer committed
27

28 29
;;; Commentary:

Karl Heuer's avatar
Karl Heuer committed
30 31
;;; Code:

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

35
(defconst mule-version-date "1999.12.7" "\
Karl Heuer's avatar
Karl Heuer committed
36 37 38
Distribution date of this version of MULE (multilingual environment).")

(defun load-with-code-conversion (fullname file &optional noerror nomessage)
39 40
  "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
41 42 43 44 45 46 47 48 49 50 51 52
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.
53 54 55 56 57 58
	    ;; 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
59 60 61
	      ;; We can't use `generate-new-buffer' because files.el
	      ;; is not yet loaded.
	      (get-buffer-create (generate-new-buffer-name " *load*"))))
62 63 64 65 66 67 68
	   (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
69
	(push file preloaded-file-list))
Karl Heuer's avatar
Karl Heuer committed
70
      (unwind-protect
71
	  (let ((load-file-name fullname)
72
		(set-auto-coding-for-load t)
73
		(inhibit-file-name-operation nil))
Karl Heuer's avatar
Karl Heuer committed
74 75
	    (save-excursion
	      (set-buffer buffer)
76 77 78
	      ;; Don't let deactivate-mark remain set.
	      (let (deactivate-mark)
		(insert-file-contents fullname))
79 80 81 82 83 84 85 86
	      ;; 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
87 88
	      ;; Make `kill-buffer' quiet.
	      (set-buffer-modified-p nil))
89
	    ;; Have the original buffer current while we eval.
90 91 92
	    (eval-buffer buffer nil
			 ;; This is compatible with what `load' does.
			 (if purify-flag file fullname)
93 94 95
			 ;; If this Emacs is running with --unibyte,
			 ;; convert multibyte strings to unibyte
			 ;; after reading them.
96
;;			 (not default-enable-multibyte-characters)
97
			 nil t
98
			 ))
99 100
	(let (kill-buffer-hook kill-buffer-query-functions)
	  (kill-buffer buffer)))
Karl Heuer's avatar
Karl Heuer committed
101
      (let ((hook (assoc file after-load-alist)))
102 103 104 105 106 107
	(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
108 109 110 111
      t)))

;; API (Application Program Interface) for charsets.

112
(defsubst charset-quoted-standard-p (obj)
Dave Love's avatar
Dave Love committed
113
  "Return t if OBJ is a quoted symbol, and is the name of a standard charset."
114 115 116 117 118
  (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
119 120

(defsubst charsetp (object)
121
  "T if OBJECT is a charset."
Karl Heuer's avatar
Karl Heuer committed
122 123 124 125 126 127 128 129 130 131 132 133
  (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.
134 135
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
136 137 138 139 140 141
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
142 143
  charset when rendering.  If 0, render from left to right, else
  render from right to left.
Karl Heuer's avatar
Karl Heuer committed
144 145 146 147 148
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
149 150
  corresponding ISO 2022 charset.  If the charset is not assigned
  any final character, the value is -1.
Karl Heuer's avatar
Karl Heuer committed
151 152 153
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
154
  If the charset is not assigned any final character, the value is -1.
Karl Heuer's avatar
Karl Heuer committed
155 156 157 158 159 160 161 162 163 164 165
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))

166 167 168 169
;; 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
170 171
(defmacro charset-id (charset)
  "Return charset identification number of CHARSET."
172
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
173
      (aref (charset-info (nth 1 charset)) 0)
174
    (list 'aref (list 'charset-info charset) 0)))
Karl Heuer's avatar
Karl Heuer committed
175 176

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

(defmacro charset-dimension (charset)
184 185
  "Return dimension of CHARSET.
See the function `charset-info' for more detail."
186
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
187
      (aref (charset-info (nth 1 charset)) 2)
188
    (list 'aref (list 'charset-info charset) 2)))
Karl Heuer's avatar
Karl Heuer committed
189 190

(defmacro charset-chars (charset)
191 192
  "Return character numbers contained in a dimension of CHARSET.
See the function `charset-info' for more detail."
193
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
194
      (aref (charset-info (nth 1 charset)) 3)
195
    (list 'aref (list 'charset-info charset) 3)))
Karl Heuer's avatar
Karl Heuer committed
196 197

(defmacro charset-width (charset)
198 199
  "Return width (how many column occupied on a screen) of CHARSET.
See the function `charset-info' for more detail."
200
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
201
      (aref (charset-info (nth 1 charset)) 4)
202
    (list 'aref (list 'charset-info charset) 4)))
Karl Heuer's avatar
Karl Heuer committed
203 204

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

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

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

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

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

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

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

(defmacro charset-plist (charset)
254 255
  "Return list charset property of CHARSET.
See the function `charset-info' for more detail."
256 257 258 259 260
  (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
261 262

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

Eli Zaretskii's avatar
Eli Zaretskii committed
266 267
(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
268
CODE1 and CODE2 are optional, but if you don't supply
269 270
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
271
A generic character can be used to index a char table (e.g. syntax-table).
272 273 274 275 276 277

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.

Dave Love's avatar
Dave Love committed
278 279 280 281 282
If CODE1 or CODE2 are invalid (out of range), this function signals an
error.  However, the eighth bit of both CODE1 and CODE2 is zeroed
before they are used to index CHARSET.  Thus you may use, say, the
actual ISO 8859 character code rather than subtracting 128, as you
would need to index the corresponding Emacs charset."
Eli Zaretskii's avatar
Eli Zaretskii committed
283
  (make-char-internal (charset-id charset) code1 code2))
284 285

(put 'make-char 'byte-compile
Dave Love's avatar
Dave Love committed
286
     (function
287 288 289 290 291 292 293 294 295
      (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
296

297
(defun charset-list ()
298 299
  "Return list of charsets ever defined.

300
This function is provided for backward compatibility.
301
Now we have the variable `charset-list'."
302 303
  charset-list)

304 305
(defsubst generic-char-p (char)
  "Return t if and only if CHAR is a generic character.
Dave Love's avatar
Dave Love committed
306
See also the documentation of `make-char'."
307 308 309 310
  (and (>= char 0400)
       (let ((l (split-char char)))
	 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
	      (not (eq (car l) 'composition))))))
311

312
(defun decode-char (ccs code-point &optional restriction)
313
  "Return character specified by coded character set CCS and CODE-POINT in it.
314
Return nil if such a character is not supported.
315
Currently the only supported coded character set is `ucs' (ISO/IEC
316
10646: Universal Multi-Octet Coded Character Set), and the result is
317
translated through the translation-table named
318 319 320
`utf-translation-table-for-decode', or through the
translation-hash-table named `utf-subst-table-for-decode'
\(if `utf-translate-cjk-mode' is non-nil).
321 322

Optional argument RESTRICTION specifies a way to map the pair of CCS
323
and CODE-POINT to a character.  Currently not supported and just ignored."
324 325
  (cond
   ((eq ccs 'ucs)
326 327
    (or (and utf-translate-cjk-mode
	     (utf-lookup-subst-table-for-decode code-point))
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	(let ((c (cond
		  ((< code-point 160)
		   code-point)
		  ((< code-point 256)
		   (make-char 'latin-iso8859-1 code-point))
		  ((< code-point #x2500)
		   (setq code-point (- code-point #x0100))
		   (make-char 'mule-unicode-0100-24ff
			      (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
		  ((< code-point #x3400)
		   (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))))))
	  (when c
	    (or (aref (get 'utf-translation-table-for-decode
			   'translation-table) c)
		c)))))))
350 351

(defun encode-char (char ccs &optional restriction)
352
  "Return code-point in coded character set CCS that corresponds to CHAR.
353
Return nil if CHAR is not included in CCS.
354
Currently the only supported coded character set is `ucs' (ISO/IEC
355
10646: Universal Multi-Octet Coded Character Set), and CHAR is first
356
translated through the translation-table named
357 358 359
`utf-translation-table-for-encode', or through the
translation-hash-table named `utf-subst-table-for-encode' \(if
`utf-translate-cjk-mode' is non-nil).
360 361

CHAR should be in one of these charsets:
362
  ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
Kenichi Handa's avatar
Kenichi Handa committed
363
  mule-unicode-e000-ffff, eight-bit-control
364 365 366 367 368
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))
369 370
	 (charset (car split))
	 trans)
371
    (cond ((eq ccs 'ucs)
372 373
	   (or (and utf-translate-cjk-mode
		    (utf-lookup-subst-table-for-encode char))
374 375 376 377 378 379 380
	       (let ((table (get 'utf-translation-table-for-encode
				 'translation-table)))
		 (setq trans (aref table char))
		 (if trans
		     (setq split (split-char trans)
			   charset (car split)))
		 (cond ((eq charset 'ascii)
381
			(or trans char))
382 383 384 385 386 387 388 389 390 391 392 393 394
		       ((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)
				     (- (nth 2 split) 32))))
		       ((eq charset 'eight-bit-control)
			char))))))))
395

396

397
;; Coding system stuff
Karl Heuer's avatar
Karl Heuer committed
398

399
;; Coding system is a symbol that has the property `coding-system'.
Karl Heuer's avatar
Karl Heuer committed
400
;;
401 402
;; The value of the property `coding-system' is a vector of the
;; following format:
403
;;	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
404
;; We call this vector as coding-spec.  See comments in src/coding.c
Dave Love's avatar
Dave Love committed
405
;; for more detail.
406 407 408 409

(defconst coding-spec-type-idx 0)
(defconst coding-spec-mnemonic-idx 1)
(defconst coding-spec-doc-string-idx 2)
410
(defconst coding-spec-plist-idx 3)
411 412
(defconst coding-spec-flags-idx 4)

413 414 415 416
;; 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.
417 418 419 420
;;
;; o coding-category
;;
;; The value is a coding category the coding system belongs to.  The
Dave Love's avatar
Dave Love committed
421
;; function `make-coding-system' sets this value automatically
422
;; unless its argument PROPERTIES specifies this property.
Karl Heuer's avatar
Karl Heuer committed
423
;;
424
;; o alias-coding-systems
Karl Heuer's avatar
Karl Heuer committed
425
;;
426 427
;; 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
428 429
;; `base coding system'.  The function `make-coding-system' sets this
;; value automatically and `define-coding-system-alias' updates it.
430
;;
Kenichi Handa's avatar
Kenichi Handa committed
431 432
;; See the documentation of make-coding-system for the meanings of the
;; following properties.
Karl Heuer's avatar
Karl Heuer committed
433
;;
Kenichi Handa's avatar
Kenichi Handa committed
434
;; o post-read-conversion
435
;; o pre-write-conversion
436 437
;; o translation-table-for-decode
;; o translation-table-for-encode
438
;; o safe-chars
439
;; o safe-charsets
440
;; o mime-charset
441 442
;; o valid-codes (meaningful only for a coding system based on CCL)

443 444

(defsubst coding-system-spec (coding-system)
Dave Love's avatar
Dave Love committed
445
  "Return coding-spec of CODING-SYSTEM."
446
  (get (check-coding-system coding-system) 'coding-system))
Karl Heuer's avatar
Karl Heuer committed
447 448

(defun coding-system-type (coding-system)
449 450 451
  "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."
452
  (aref (coding-system-spec coding-system) coding-spec-type-idx))
Karl Heuer's avatar
Karl Heuer committed
453 454

(defun coding-system-mnemonic (coding-system)
455
  "Return the mnemonic character of CODING-SYSTEM.
456 457 458 459
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
460

461
(defun coding-system-doc-string (coding-system)
462
  "Return the documentation string for CODING-SYSTEM."
463
  (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
Karl Heuer's avatar
Karl Heuer committed
464

465
(defun coding-system-plist (coding-system)
466
  "Return the property list of CODING-SYSTEM."
467
  (aref (coding-system-spec coding-system) coding-spec-plist-idx))
468

Karl Heuer's avatar
Karl Heuer committed
469
(defun coding-system-flags (coding-system)
470 471 472 473
  "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."
474
  (aref (coding-system-spec coding-system) coding-spec-flags-idx))
475 476 477 478 479 480 481 482 483 484

(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)
485 486
      (aset (coding-system-spec coding-system) coding-spec-plist-idx
	    (list prop val)))))
487 488

(defun coding-system-category (coding-system)
Dave Love's avatar
Dave Love committed
489 490
  "Return the coding category of CODING-SYSTEM.
See also `coding-category-list'."
491 492 493 494
  (coding-system-get coding-system 'coding-category))

(defun coding-system-base (coding-system)
  "Return the base coding system of CODING-SYSTEM.
495 496
A base coding system is what made by `make-coding-system'.
Any alias nor subsidiary coding systems are not base coding system."
497 498 499 500 501 502 503 504 505 506 507
  (car (coding-system-get coding-system 'alias-coding-systems)))

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

509
(defun coding-system-eol-type (coding-system)
510 511
  "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
512

513 514 515 516 517 518 519
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))
520

521 522 523
(defun coding-system-eol-type-mnemonic (coding-system)
  "Return the string indicating end-of-line format of CODING-SYSTEM."
  (let* ((eol-type (coding-system-eol-type coding-system))
524
	 (val (cond ((eq eol-type 0) eol-mnemonic-unix)
525 526
		    ((eq eol-type 1) eol-mnemonic-dos)
		    ((eq eol-type 2) eol-mnemonic-mac)
527
		    (t eol-mnemonic-undecided))))
528 529 530 531
    (if (stringp val)
	val
      (char-to-string val))))

532 533 534 535 536 537 538 539 540 541 542 543 544
(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)))))))

545 546 547 548 549 550 551 552 553 554 555 556
(defun coding-system-equal (coding-system-1 coding-system-2)
  "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
Two coding systems are identical if two symbols are equal
or one is an alias of the other."
  (or (eq coding-system-1 coding-system-2)
      (and (equal (coding-system-spec coding-system-1)
		  (coding-system-spec coding-system-2))
	   (let ((eol-type-1 (coding-system-eol-type coding-system-1))
		 (eol-type-2 (coding-system-eol-type coding-system-2)))
	     (or (eq eol-type-1 eol-type-2)
		 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))

557
(defun add-to-coding-system-list (coding-system)
Dave Love's avatar
Dave Love committed
558
  "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
559 560 561 562 563 564 565 566 567 568 569 570 571
  (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))))))

572
(defun coding-system-list (&optional base-only)
573 574 575 576 577
  "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)."
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  (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))

Dave Love's avatar
Dave Love committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
(defun map-charset-chars (func charset)
  "Use FUNC to map over all characters in CHARSET for side effects.
FUNC is a function of two args, the start and end (inclusive) of a
character code range.  Thus FUNC should iterate over [START, END]."
  (let* ((dim (charset-dimension charset))
	 (chars (charset-chars charset))
	 (start (if (= chars 94)
		    33
		  32)))
    (if (= dim 1)
	(funcall func
		 (make-char charset start)
		 (make-char charset (+ start chars -1)))
      (dotimes (i chars)
	(funcall func
		 (make-char charset (+ i start) start)
		 (make-char charset (+ i start) (+ start chars -1)))))))

612
(defalias 'register-char-codings 'ignore "")
613
(make-obsolete 'register-char-codings
614
               "it exists just for backward compatibility, and does nothing."
615 616 617 618 619
	       "21.3")

(defconst char-coding-system-table nil
  "This is an obsolete variable.
It exists just for backward compatibility, and the value is always nil.")
620

621
(defun make-subsidiary-coding-system (coding-system)
Dave Love's avatar
Dave Love committed
622
  "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
623 624
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
625 626
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
627 628 629 630 631 632 633 634 635 636
	elt)
    (dotimes (i 3)
      (setq elt (aref subsidiaries i))
      (put elt 'coding-system coding-spec)
      (put elt 'eol-type i)
      (put elt 'coding-system-define-form nil)
      (add-to-coding-system-list elt)
      (or (assoc (symbol-name elt) coding-system-alist)
	  (setq coding-system-alist
		(cons (list (symbol-name elt)) coding-system-alist))))
637
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
(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
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
     ((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))
712
     ((eq type 'ccl)
713
      `(,name 4 ,mnemonic ,doc-string
714
	      (,(plist-get props 'decode) . ,(plist-get props 'encode))
715
	      ,properties ,eol-type))
716
     (t
717
      (error "unsupported XEmacs style make-coding-style arguments: %S"
718 719
	     `(,name ,type ,doc-string ,props))))))

720
(defun make-coding-system (coding-system type mnemonic doc-string
721 722 723 724
					 &optional
					 flags
					 properties
					 eol-type)
725
  "Define a new coding system CODING-SYSTEM (symbol).
Dave Love's avatar
Dave Love committed
726
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
727
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
728
in the following format:
729
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
730 731

TYPE is an integer value indicating the type of the coding system as follows:
Karl Heuer's avatar
Karl Heuer committed
732
  0: Emacs internal format,
733
  1: Shift-JIS (or MS-Kanji) used mainly on Japanese PCs,
Karl Heuer's avatar
Karl Heuer committed
734
  2: ISO-2022 including many variants,
735
  3: Big5 used mainly on Chinese PCs,
736
  4: private, CCL programs provide encoding/decoding algorithm,
Dave Love's avatar
Dave Love committed
737
  5: Raw-text, which means that text contains random 8-bit codes.
738

739
MNEMONIC is a character to be displayed on mode line for the coding system.
740

741
DOC-STRING is a documentation string for the coding system.
742

743
FLAGS specifies more detailed information of the coding system as follows:
744

745
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
746 747
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
748
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
749
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
750 751 752 753 754
    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.
755
        But, character set `ascii' can be designated only to G0.
Karl Heuer's avatar
Karl Heuer committed
756 757 758 759 760 761 762 763 764 765
    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.
766 767 768 769
    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.
770
    SAFE non-nil means convert unsafe characters to `?' on output.
771 772
      Characters not specified in the property `safe-charsets' nor
      `safe-chars' are unsafe.
773 774 775
    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.
776

777 778 779
  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.
780

781 782 783 784
PROPERTIES is an alist of properties vs the corresponding values.  The
following properties are recognized:

  o post-read-conversion
785

786 787
  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
788 789 790 791
  `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.
792

793
  o pre-write-conversion
794

795 796 797
  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
798 799
  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
800
  a region of text.
801

802
  o translation-table-for-decode
803

804 805
  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
806
  table.  This is not applicable to type 4 (CCL-based) coding systems.
807

808
  o translation-table-for-encode
809

Dave Love's avatar
Dave Love committed
810 811
  The value is a translation table to be applied on encoding.  This is
  not applicable to type 4 (CCL-based) coding systems.
812

813
  o safe-chars
814

815 816 817 818 819
  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
820

821 822 823
  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
824 825
  mean that the charset can't be encoded in the coding system;
  it just means that some other receiver of text encoded
826
  in the coding system won't be able to handle that charset.
827

828
  o mime-charset
829

830
  The value is a symbol whose name is the `MIME-charset' parameter of
831
  the coding system.
832

Dave Love's avatar
Dave Love committed
833 834 835 836 837
  o mime-text-unsuitable

  A non-nil value means the `mime-charset' property names a charset
  which is unsuitable for the top-level media type \"text\".

838
  o valid-codes (meaningful only for a coding system based on CCL)
839

840 841 842
  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
Dave Love's avatar
Dave Love committed
843
  latter case, the integers specify the range of valid byte codes.
844

845 846
  o composition (meaningful only when TYPE is 0 or 2)

847 848
  If the value is non-nil, the coding system preserves composition
  information.
849

Kenichi Handa's avatar
Kenichi Handa committed
850 851 852 853 854 855
  o ascii-incompatible

  If the value is non-nil, the coding system is not compatible
  with ASCII, which means it encodes or decodes ASCII character
  string to the different byte sequence.

856 857 858
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
859

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
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
878
	corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
879 880
	in this order.

881 882 883 884 885 886 887
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."
888 889

  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
Eli Zaretskii's avatar
Eli Zaretskii committed
890 891
  ;; is a symbol, perhaps, this function is called with XEmacs-style
  ;; arguments.  Here, try to transform that kind of arguments to
892 893 894 895 896
  ;; 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
897
	      type (nth 1 args)
898 899 900 901 902 903
	      mnemonic (nth 2 args)
	      doc-string (nth 3 args)
	      flags (nth 4 args)
	      properties (nth 5 args)
	      eol-type (nth 6 args))))

904
  ;; Set a value of `coding-system' property.
905
  (let ((coding-spec (make-vector 5 nil))
906 907
	(no-initial-designation t)
	(no-alternative-designation t)
908
	(accept-latin-extra-code nil)
909
	coding-category)
910
    (if (or (not (integerp type)) (< type 0) (> type 5))
911
	(error "TYPE argument must be 0..5"))
912
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
Dave Love's avatar
Dave Love committed
913
	(error "MNEMONIC argument must be an ASCII printable character"))
914 915 916
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
917
	  (purecopy (if (stringp doc-string) doc-string "")))
918 919 920 921 922
    (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
923
	   (let ((i 0)
924
		 (vec (make-vector 32 nil))
925 926
		 (g1-designation nil)
		 (fl flags))
Karl Heuer's avatar
Karl Heuer committed
927
	     (while (< i 4)
928
	       (let ((charset (car fl)))
929 930 931 932 933 934 935 936 937 938 939 940 941
		 (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))