mule.el 79.8 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 67
	   (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
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)
Dave Love's avatar
Dave Love committed
596 597 598 599 600 601 602 603
  "Add entries for CODING-SYSTEM to `char-coding-system-table'.
If SAFE-CHARS is a char-table, its non-nil entries specify characters
which CODING-SYSTEM encodes safely.  If SAFE-CHARS is t, register
CODING-SYSTEM as a general one which can encode all characters."
  (let ((general (char-table-extra-slot char-coding-system-table 0))
	;; Charsets which have some members in the table, but not all
	;; of them (i.e. not just a generic character):
	(partials (char-table-extra-slot char-coding-system-table 1)))
604 605 606 607 608
    (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
Dave Love's avatar
Dave Love committed
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 645 646 647
       (lambda (key val)
	 (if (and (>= key 128) val)
	     (let ((codings (aref char-coding-system-table key))
		   (charset (char-charset key)))
	       (unless (memq coding-system codings)
		 (if (and (generic-char-p key)
			  (memq charset partials))
		     ;; The generic char would clobber individual
		     ;; entries already in the table.  First save the
		     ;; separate existing entries for all chars of the
		     ;; charset (with the generic entry added, if
		     ;; necessary).
		     (let (entry existing)
		       (map-charset-chars
			(lambda (start end)
			  (while (<= start end)
			    (setq entry (aref char-coding-system-table start))
			    (when entry
			      (push (cons
				     start
				     (if (memq coding-system entry)
					 entry
				       (cons coding-system entry)))
				    existing))
			    (setq start (1+ start))))
			charset)
		       ;; Update the generic entry.
		       (aset char-coding-system-table key
			     (cons coding-system codings))
		       ;; Override with the saved entries.
		       (dolist (elt existing)
			 (aset char-coding-system-table (car elt) (cdr elt))))
		   (aset char-coding-system-table key
			 (cons coding-system codings))
		   (unless (or (memq charset partials)
			       (generic-char-p key))
		     (push charset partials)))))))
       safe-chars)
      (set-char-table-extra-slot char-coding-system-table 1 partials))))
648 649


650
(defun make-subsidiary-coding-system (coding-system)
Dave Love's avatar
Dave Love committed
651
  "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
652 653
  (let ((coding-spec (coding-system-spec coding-system))
	(subsidiaries (vector (intern (format "%s-unix" coding-system))
654 655
			      (intern (format "%s-dos" coding-system))
			      (intern (format "%s-mac" coding-system))))
656 657
	(i 0)
	temp)
658
    (while (< i 3)
659
      (put (aref subsidiaries i) 'coding-system coding-spec)
660
      (put (aref subsidiaries i) 'eol-type i)
661
      (add-to-coding-system-list (aref subsidiaries i))
662 663 664
      (setq coding-system-alist
	    (cons (list (symbol-name (aref subsidiaries i)))
		  coding-system-alist))
665 666
      (setq i (1+ i)))
    subsidiaries))
Karl Heuer's avatar
Karl Heuer committed
667

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
(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
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 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
     ((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))
741
     ((eq type 'ccl)
742
      `(,name 4 ,mnemonic ,doc-string
743
	      (,(plist-get props 'decode) . ,(plist-get props 'encode))
744
	      ,properties ,eol-type))
745
     (t
746
      (error "unsupported XEmacs style make-coding-style arguments: %S"
747 748
	     `(,name ,type ,doc-string ,props))))))

749
(defun make-coding-system (coding-system type mnemonic doc-string
750 751 752 753
					 &optional
					 flags
					 properties
					 eol-type)
754
  "Define a new coding system CODING-SYSTEM (symbol).
Dave Love's avatar
Dave Love committed
755
Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
756
and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
757
in the following format:
758
	[TYPE MNEMONIC DOC-STRING PLIST FLAGS]
759 760

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

768
MNEMONIC is a character to be displayed on mode line for the coding system.
769

770
DOC-STRING is a documentation string for the coding system.
771

772
FLAGS specifies more detailed information of the coding system as follows:
773

774
  If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
Karl Heuer's avatar
Karl Heuer committed
775 776
      CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
      ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
777
      USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
778
      SAFE, ACCEPT-LATIN-EXTRA-CODE.
Karl Heuer's avatar
Karl Heuer committed
779 780 781 782 783
    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.
784
        But, character set `ascii' can be designated only to G0.
Karl Heuer's avatar
Karl Heuer committed
785 786 787 788 789 790 791 792 793 794
    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.
795 796 797 798
    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.
799
    SAFE non-nil means convert unsafe characters to `?' on output.
800 801
      Characters not specified in the property `safe-charsets' nor
      `safe-chars' are unsafe.
802 803 804
    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.
805

806 807 808
  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.
809

810 811 812 813
PROPERTIES is an alist of properties vs the corresponding values.  The
following properties are recognized:

  o post-read-conversion
814

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

822
  o pre-write-conversion
823

824 825 826
  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
827 828
  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
829
  a region of text.
830

831
  o translation-table-for-decode
832

833 834
  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
835
  table.  This is not applicable to type 4 (CCL-based) coding systems.
836

837
  o translation-table-for-encode
838

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

842
  o safe-chars
843

844 845 846 847 848
  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
849

850 851 852
  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
853 854
  mean that the charset can't be encoded in the coding system;
  it just means that some other receiver of text encoded
855
  in the coding system won't be able to handle that charset.
856

857
  o mime-charset
858

859
  The value is a symbol whose name is the `MIME-charset' parameter of
860
  the coding system.
861

862
  o valid-codes (meaningful only for a coding system based on CCL)
863

864 865 866
  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
867
  latter case, the integers specify the range of valid byte codes.
868

869 870 871 872 873
  o composition (meaningful only when TYPE is 0 or 2)

  If the value is non-nil, the coding system preserves information of
  composition.

874 875 876
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
877

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
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
896
	corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
897 898
	in this order.

899 900 901 902 903 904 905
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."
906 907

  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
Eli Zaretskii's avatar
Eli Zaretskii committed
908 909
  ;; is a symbol, perhaps, this function is called with XEmacs-style
  ;; arguments.  Here, try to transform that kind of arguments to
910 911 912 913 914
  ;; 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
915
	      type (nth 1 args)
916 917 918 919 920 921
	      mnemonic (nth 2 args)
	      doc-string (nth 3 args)
	      flags (nth 4 args)
	      properties (nth 5 args)
	      eol-type (nth 6 args))))

922
  ;; Set a value of `coding-system' property.
923
  (let ((coding-spec (make-vector 5 nil))
924 925
	(no-initial-designation t)
	(no-alternative-designation t)
926
	(accept-latin-extra-code nil)
927
	coding-category)
928
    (if (or (not (integerp type)) (< type 0) (> type 5))
929
	(error "TYPE argument must be 0..5"))
930
    (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
Dave Love's avatar
Dave Love committed
931
	(error "MNEMONIC argument must be an ASCII printable character"))
932 933 934
    (aset coding-spec coding-spec-type-idx type)
    (aset coding-spec coding-spec-mnemonic-idx mnemonic)
    (aset coding-spec coding-spec-doc-string-idx
935
	  (purecopy (if (stringp doc-string) doc-string "")))
936 937 938 939 940
    (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
941
	   (let ((i 0)
942
		 (vec (make-vector 32 nil))
943 944
		 (g1-designation nil)
		 (fl flags))
Karl Heuer's avatar
Karl Heuer committed
945
	     (while (< i 4)
946
	       (let ((charset (car fl)))
947 948 949 950 951 952 953 954 955 956 957 958 959
		 (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))
960 961 962 963
			   (if (eq elt t)
			       (setq no-alternative-designation nil)
			     (if (and elt (not (charsetp elt)))
				 (error "Invalid charset: %s" elt)))
964 965
			   (setq tail (cdr tail)))
			 (setq g1-designation (car charset)))