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

;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;; Licensed to the Free Software Foundation.
Colin Walters's avatar
Colin Walters committed
5
;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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
;;; Commentary:

Karl Heuer's avatar
Karl Heuer committed
28 29
;;; Code:

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

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

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

;; API (Application Program Interface) for charsets.

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

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

160 161 162 163
;; 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
164 165
(defmacro charset-id (charset)
  "Return charset identification number of CHARSET."
166
  (if (charset-quoted-standard-p charset)
Karl Heuer's avatar
Karl Heuer committed
167
      (aref (charset-info (nth 1 charset)) 0)
168
    (list 'aref (list 'charset-info charset) 0)))
Karl Heuer's avatar
Karl Heuer committed
169 170

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

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

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

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

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

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

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

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

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

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

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

(defmacro charset-plist (charset)
248 249
  "Return list charset property of CHARSET.
See the function `charset-info' for more detail."
250 251 252 253 254
  (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
255 256

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

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

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
272 273 274 275 276
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
277
  (make-char-internal (charset-id charset) code1 code2))
278 279

(put 'make-char 'byte-compile
Dave Love's avatar
Dave Love committed
280
     (function
281 282 283 284 285 286 287 288 289
      (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
290

291
(defun charset-list ()
292 293
  "Return list of charsets ever defined.

294
This function is provided for backward compatibility.
295
Now we have the variable `charset-list'."
296 297
  charset-list)

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

306
(defun decode-char (ccs code-point &optional restriction)
307
  "Return character specified by coded character set CCS and CODE-POINT in it.
308
Return nil if such a character is not supported.
309
Currently the only supported coded character set is `ucs' (ISO/IEC
310
10646: Universal Multi-Octet Coded Character Set), and the result is
311 312 313
translated through the translation-table named
`utf-translation-table-for-decode' or the translation-hash-table named
`utf-subst-table-for-decode'.
314 315

Optional argument RESTRICTION specifies a way to map the pair of CCS
316
and CODE-POINT to a character.  Currently not supported and just ignored."
317 318
  (cond
   ((eq ccs 'ucs)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    (or (gethash code-point
		 (get 'utf-subst-table-for-decode 'translation-hash-table))
	(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)))))))
343 344

(defun encode-char (char ccs &optional restriction)
345
  "Return code-point in coded character set CCS that corresponds to CHAR.
346
Return nil if CHAR is not included in CCS.
347
Currently the only supported coded character set is `ucs' (ISO/IEC
348
10646: Universal Multi-Octet Coded Character Set), and CHAR is first
349 350 351
translated through the translation-table named
`utf-translation-table-for-encode' or the translation-hash-table named
`utf-subst-table-for-encode'.
352 353

CHAR should be in one of these charsets:
354
  ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
Kenichi Handa's avatar
Kenichi Handa committed
355
  mule-unicode-e000-ffff, eight-bit-control
356 357 358 359 360
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))
361 362
	 (charset (car split))
	 trans)
363
    (cond ((eq ccs 'ucs)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	   (or (gethash char (get 'utf-subst-table-for-encode
				  'translation-hash-table))
	       (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)
			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)
				     (- (nth 2 split) 32))))
		       ((eq charset 'eight-bit-control)
			char))))))))
387

388

389
;; Coding system stuff
Karl Heuer's avatar
Karl Heuer committed
390

391
;; Coding system is a symbol that has the property `coding-system'.
Karl Heuer's avatar
Karl Heuer committed
392
;;
393 394
;; The value of the property `coding-system' is a vector of the
;; following format:
395
;;	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
396
;; We call this vector as coding-spec.  See comments in src/coding.c
Dave Love's avatar
Dave Love committed
397
;; for more detail.
398 399 400 401

(defconst coding-spec-type-idx 0)
(defconst coding-spec-mnemonic-idx 1)
(defconst coding-spec-doc-string-idx 2)
402
(defconst coding-spec-plist-idx 3)
403 404
(defconst coding-spec-flags-idx 4)

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

435 436

(defsubst coding-system-spec (coding-system)
Dave Love's avatar
Dave Love committed
437
  "Return coding-spec of CODING-SYSTEM."
438
  (get (check-coding-system coding-system) 'coding-system))
Karl Heuer's avatar
Karl Heuer committed
439 440

(defun coding-system-type (coding-system)
441 442 443
  "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."
444
  (aref (coding-system-spec coding-system) coding-spec-type-idx))
Karl Heuer's avatar
Karl Heuer committed
445 446

(defun coding-system-mnemonic (coding-system)
447
  "Return the mnemonic character of CODING-SYSTEM.
448 449 450 451
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
452

453
(defun coding-system-doc-string (coding-system)
454
  "Return the documentation string for CODING-SYSTEM."
455
  (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
Karl Heuer's avatar
Karl Heuer committed
456

457
(defun coding-system-plist (coding-system)
458
  "Return the property list of CODING-SYSTEM."
459
  (aref (coding-system-spec coding-system) coding-spec-plist-idx))
460

Karl Heuer's avatar
Karl Heuer committed
461
(defun coding-system-flags (coding-system)
462 463 464 465
  "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."
466
  (aref (coding-system-spec coding-system) coding-spec-flags-idx))
467 468 469 470 471 472 473 474 475 476

(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)
477 478
      (aset (coding-system-spec coding-system) coding-spec-plist-idx
	    (list prop val)))))
479 480

(defun coding-system-category (coding-system)
Dave Love's avatar
Dave Love committed
481 482
  "Return the coding category of CODING-SYSTEM.
See also `coding-category-list'."
483 484 485 486
  (coding-system-get coding-system 'coding-category))

(defun coding-system-base (coding-system)
  "Return the base coding system of CODING-SYSTEM.
487 488
A base coding system is what made by `make-coding-system'.
Any alias nor subsidiary coding systems are not base coding system."
489 490 491
  (car (coding-system-get coding-system 'alias-coding-systems)))

(defalias 'coding-system-parent 'coding-system-base)
492
(make-obsolete 'coding-system-parent 'coding-system-base "20.3")
493 494 495 496 497 498 499 500 501 502

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

504
(defun coding-system-eol-type (coding-system)
505 506
  "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
507

508 509 510 511 512 513 514
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))
515

516 517 518
(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))
519
	 (val (cond ((eq eol-type 0) eol-mnemonic-unix)
520 521
		    ((eq eol-type 1) eol-mnemonic-dos)
		    ((eq eol-type 2) eol-mnemonic-mac)
522
		    (t eol-mnemonic-undecided))))
523 524 525 526
    (if (stringp val)
	val
      (char-to-string val))))

527 528 529 530 531 532 533 534 535 536 537 538 539 540
(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
541
  "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
542 543 544 545 546 547 548 549 550 551 552 553 554
  (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))))))

555
(defun coding-system-list (&optional base-only)
556 557 558 559 560
  "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)."
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  (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
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
(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)))))))

595
(defun register-char-codings (coding-system safe-chars)
596
  "This is an obsolete function.
597 598 599 600 601 602 603 604
It exists just for backward compatibility, and it does nothing.")
(make-obsolete 'register-char-codings
	       "Unnecessary function.  Calling it has no effect."
	       "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.")
605

606
(defun make-subsidiary-coding-system (coding-system)
Dave Love's avatar
Dave Love committed
607
  "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
608 609
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
610 611
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
612 613
	(i 0)
	temp)
614
    (while (< i 3)
615
      (put (aref subsidiaries i) 'coding-system coding-spec)
616
      (put (aref subsidiaries i) 'eol-type i)
617
      (add-to-coding-system-list (aref subsidiaries i))
618 619 620
      (setq coding-system-alist
	    (cons (list (symbol-name (aref subsidiaries i)))
		  coding-system-alist))
621 622
      (setq i (1+ i)))
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
623

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
(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
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 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
     ((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))
697
     ((eq type 'ccl)
698
      `(,name 4 ,mnemonic ,doc-string
699
	      (,(plist-get props 'decode) . ,(plist-get props 'encode))
700
	      ,properties ,eol-type))
701
     (t
702
      (error "unsupported XEmacs style make-coding-style arguments: %S"
703 704
	     `(,name ,type ,doc-string ,props))))))

705
(defun make-coding-system (coding-system type mnemonic doc-string
706 707 708 709
					 &optional
					 flags
					 properties
					 eol-type)
710
  "Define a new coding system CODING-SYSTEM (symbol).
Dave Love's avatar
Dave Love committed
711
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
712
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
713
in the following format:
714
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
715 716

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

724
MNEMONIC is a character to be displayed on mode line for the coding system.
725

726
DOC-STRING is a documentation string for the coding system.
727

728
FLAGS specifies more detailed information of the coding system as follows:
729

730
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
731 732
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
733
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
734
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
735 736 737 738 739
    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.
740
        But, character set `ascii' can be designated only to G0.
Karl Heuer's avatar
Karl Heuer committed
741 742 743 744 745 746 747 748 749 750
    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.
751 752 753 754
    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.
755
    SAFE non-nil means convert unsafe characters to `?' on output.
756 757
      Characters not specified in the property `safe-charsets' nor
      `safe-chars' are unsafe.
758 759 760
    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.
761

762 763 764
  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.
765

766 767 768 769
PROPERTIES is an alist of properties vs the corresponding values.  The
following properties are recognized:

  o post-read-conversion
770

771 772
  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
773 774 775 776
  `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.
777

778
  o pre-write-conversion
779

780 781 782
  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
783 784
  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
785
  a region of text.
786

787
  o translation-table-for-decode
788

789 790
  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
791
  table.  This is not applicable to type 4 (CCL-based) coding systems.
792

793
  o translation-table-for-encode
794

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

798
  o safe-chars
799

800 801 802 803 804
  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
805

806 807 808
  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
809 810
  mean that the charset can't be encoded in the coding system;
  it just means that some other receiver of text encoded
811
  in the coding system won't be able to handle that charset.
812

813
  o mime-charset
814

815
  The value is a symbol whose name is the `MIME-charset' parameter of
816
  the coding system.
817

818
  o valid-codes (meaningful only for a coding system based on CCL)
819

820 821 822
  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
823
  latter case, the integers specify the range of valid byte codes.
824

825 826
  o composition (meaningful only when TYPE is 0 or 2)

827 828
  If the value is non-nil, the coding system preserves composition
  information.
829

830 831 832
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
833

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
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
852
	corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
853 854
	in this order.

855 856 857 858 859 860 861
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."
862 863

  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
Eli Zaretskii's avatar
Eli Zaretskii committed
864 865
  ;; is a symbol, perhaps, this function is called with XEmacs-style
  ;; arguments.  Here, try to transform that kind of arguments to
866 867 868 869 870
  ;; 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
871
	      type (nth 1 args)
872 873 874 875 876 877
	      mnemonic (nth 2 args)
	      doc-string (nth 3 args)
	      flags (nth 4 args)
	      properties (nth 5 args)
	      eol-type (nth 6 args))))

878
  ;; Set a value of `coding-system' property.
879
  (let ((coding-spec (make-vector 5 nil))
880 881
	(no-initial-designation t)
	(no-alternative-designation t)
882
	(accept-latin-extra-code nil)
883
	coding-category)
884
    (if (or (not (integerp type)) (< type 0) (> type 5))
885
	(error "TYPE argument must be 0..5"))
886
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
Dave Love's avatar
Dave Love committed
887
	(error "MNEMONIC argument must be an ASCII printable character"))
888 889 890
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
891
	  (purecopy (if (stringp doc-string) doc-string "")))
892 893 894 895 896
    (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
897
	   (let ((i 0)
898
		 (vec (make-vector 32 nil))
899 900
		 (g1-designation nil)
		 (fl flags))
Karl Heuer's avatar
Karl Heuer committed
901
	     (while (< i 4)
902
	       (let ((charset (car fl)))
903 904 905 906 907 908 909 910 911 912 913 914 915
		 (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))
916 917 918 919
			   (if (eq elt t)
			       (setq no-alternative-designation nil)
			     (if (and elt (not (charsetp elt)))
				 (error "Invalid charset: %s" elt)))
920 921
			   (setq tail (cdr tail)))
			 (setq g1-designation (car charset)))
922 923 924 925
		     (if charset
			 (if (eq charset t)
			     (setq no-alternative-designation nil)
			   (error "Invalid charset: %s" charset)))))
Karl Heuer's avatar
Karl Heuer committed
926
		 (aset vec i charset))
927 928 929
	       (setq fl (cdr fl) i (1+ i)))
	     (while (and (< i 32) fl)
	       (aset vec i (car fl))
930 931 932
	       (if (and (= i 16)	; ACCEPT-LATIN-EXTRA-CODE
			(car fl))
		   (setq accept-latin-extra-code t))
933
	       (setq fl (cdr fl) i (1+ i)))
934 935 936
	     (aset coding-spec 4 vec)
	     (setq coding-category
		   (if (aref vec 8)	; Use locking-shift.