mule.el 88.3 KB
Newer Older
Dave Love's avatar
Dave Love committed
1
;;; mule.el --- basic commands for multilingual environment
Karl Heuer's avatar
Karl Heuer committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
Kenichi Handa's avatar
Kenichi Handa committed
4
;;   Free Software Foundation, Inc.
Kenichi Handa's avatar
Kenichi Handa committed
5
;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
6
;;   2005, 2006, 2007, 2008
Kenichi Handa's avatar
Kenichi Handa committed
7 8
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H14PRO021
Kenichi Handa's avatar
Kenichi Handa committed
9
;; Copyright (C) 2003
10 11
;;   National Institute of Advanced Industrial Science and Technology (AIST)
;;   Registration Number H13PRO009
Karl Heuer's avatar
Karl Heuer committed
12 13 14 15 16

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

;; This file is part of GNU Emacs.

17
;; GNU Emacs is free software: you can redistribute it and/or modify
Karl Heuer's avatar
Karl Heuer committed
18
;; it under the terms of the GNU General Public License as published by
19 20
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Karl Heuer's avatar
Karl Heuer committed
21 22 23 24 25 26 27

;; 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
28
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Karl Heuer's avatar
Karl Heuer committed
29

30 31
;;; Commentary:

Karl Heuer's avatar
Karl Heuer committed
32 33
;;; Code:

Kenichi Handa's avatar
Kenichi Handa committed
34
(defconst mule-version "6.0 (HANACHIRUSATO)" "\
Karl Heuer's avatar
Karl Heuer committed
35 36
Version number and name of this version of MULE (multilingual environment).")

Kenichi Handa's avatar
Kenichi Handa committed
37
(defconst mule-version-date "2003.9.1" "\
Karl Heuer's avatar
Karl Heuer committed
38 39
Distribution date of this version of MULE (multilingual environment).")

40 41 42

;;; CHARSET

Kenichi Handa's avatar
Kenichi Handa committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
;; Backward compatibility code for handling emacs-mule charsets.
(defvar private-char-area-1-min #xF0000)
(defvar private-char-area-1-max #xFFFFE)
(defvar private-char-area-2-min #x100000)
(defvar private-char-area-2-max #x10FFFE)

;; Table of emacs-mule charsets indexed by their emacs-mule ID.
(defvar emacs-mule-charset-table (make-vector 256 nil))
(aset emacs-mule-charset-table 0 'ascii)

;; Convert the argument of old-style calll of define-charset to a
;; property list used by the new-style.
;; INFO-VECTOR is a vector of the format:
;;   [DIMENSION CHARS WIDTH DIRECTION ISO-FINAL-CHAR ISO-GRAPHIC-PLANE
;;    SHORT-NAME LONG-NAME DESCRIPTION]

(defun convert-define-charset-argument (emacs-mule-id info-vector)
  (let* ((dim (aref info-vector 0))
	 (chars (aref info-vector 1))
	 (total (if (= dim 1) chars (* chars chars)))
	 (code-space (if (= dim 1) (if (= chars 96) [32 127] [33 126])
		       (if (= chars 96) [32 127 32 127] [33 126 33 126])))
	 code-offset)
    (if (integerp emacs-mule-id)
	(or (= emacs-mule-id 0)
	    (and (>= emacs-mule-id 129) (< emacs-mule-id 256))
	    (error "Invalid CHARSET-ID: %d" emacs-mule-id))
      (let (from-id to-id)
	(if (= dim 1) (setq from-id 160 to-id 224)
	  (setq from-id 224 to-id 255))
	(while (and (< from-id to-id)
		    (not (aref emacs-mule-charset-table from-id)))
	  (setq from-id (1+ from-id)))
	(if (= from-id to-id)
	    (error "No more room for the new Emacs-mule charset"))
	(setq emacs-mule-id from-id)))
    (if (> (- private-char-area-1-max private-char-area-1-min) total)
	(setq code-offset private-char-area-1-min
	      private-char-area-1-min (+ private-char-area-1-min total))
      (if (> (- private-char-area-2-max private-char-area-2-min) total)
	  (setq code-offset private-char-area-2-min
		private-char-area-2-min (+ private-char-area-2-min total))
85
	(error "No more space for a new charset")))
Kenichi Handa's avatar
Kenichi Handa committed
86 87 88 89 90 91
    (list :dimension dim
	  :code-space code-space
	  :iso-final-char (aref info-vector 4)
	  :code-offset code-offset
	  :emacs-mule-id emacs-mule-id)))

92 93 94
(defun define-charset (name docstring &rest props)
  "Define NAME (symbol) as a charset with DOCSTRING.
The remaining arguments must come in pairs ATTRIBUTE VALUE.  ATTRIBUTE
Dave Love's avatar
Dave Love committed
95
may be any symbol.  The following have special meanings, and one of
Kenichi Handa's avatar
Kenichi Handa committed
96
`:code-offset', `:map', `:subset', `:superset' must be specified.
97 98 99 100 101 102 103 104 105

`:short-name'

VALUE must be a short string to identify the charset.  If omitted,
NAME is used.

`:long-name'

VALUE must be a string longer than `:short-name' to identify the
Dave Love's avatar
Dave Love committed
106
charset.  If omitted, the value of the `:short-name' attribute is used.
107 108 109 110

`:dimension'

VALUE must be an integer 0, 1, 2, or 3, specifying the dimension of
Dave Love's avatar
Dave Love committed
111 112
code-points of the charsets.  If omitted, it is calculated from the
value of the `:code-space' attribute.
113 114 115 116 117 118

`:code-space'

VALUE must be a vector of length at most 8 specifying the byte code
range of each dimension in this format:
	[ MIN-1 MAX-1 MIN-2 MAX-2 ... ]
Dave Love's avatar
Dave Love committed
119
where MIN-N is the minimum byte value of Nth dimension of code-point,
120 121
MAX-N is the maximum byte value of that.

122 123 124 125 126 127 128
`:min-code'

VALUE must be an integer specifying the mininum code point of the
charset.  If omitted, it is calculated from `:code-space'.  VALUE may
be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
the code point and LOW is the least significant 16 bits.

129
`:max-code'
130 131 132 133 134 135

VALUE must be an integer specifying the maxinum code point of the
charset.  If omitted, it is calculated from `:code-space'.  VALUE may
be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of
the code point and LOW is the least significant 16 bits.

136 137 138 139 140 141 142 143 144 145 146 147 148 149
`:iso-final-char'

VALUE must be a character in the range 32 to 127 (inclusive)
specifying the final char of the charset for ISO-2022 encoding.  If
omitted, the charset can't be encoded by ISO-2022 based
coding-systems.

`:iso-revision-number'

VALUE must be an integer in the range 0..63, specifying the revision
number of the charset for ISO-2022 encoding.

`:emacs-mule-id'

Kenichi Handa's avatar
Kenichi Handa committed
150
VALUE must be an integer of 0, 129..255.  If omitted, the charset
151 152 153 154
can't be encoded by coding-systems of type `emacs-mule'.

`:ascii-compatible-p'

Dave Love's avatar
Dave Love committed
155 156
VALUE must be nil or t (default nil).  If VALUE is t, the charset is
compatible with ASCII, i.e. the first 128 code points map to ASCII.
157 158 159 160

`:supplementary-p'

VALUE must be nil or t.  If the VALUE is t, the charset is
161 162 163
supplementary, which means it is used only as a parent or a
subset of some other charset, or it is provided just for backward
compatibility.
164 165 166 167 168 169 170 171 172 173

`:invalid-code'

VALUE must be a nonnegative integer that can be used as an invalid
code point of the charset.  If the minimum code is 0 and the maximum
code is greater than Emacs' maximum integer value, `:invalid-code'
should not be omitted.

`:code-offset'

Dave Love's avatar
Dave Love committed
174 175
VALUE must be an integer added to the index number of a character to
get the corresponding character code.
176 177 178 179 180 181 182

`:map'

VALUE must be vector or string.

If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
where CODE-n is a code-point of the charset, and CHAR-n is the
Dave Love's avatar
Dave Love committed
183
corresponding character code.
184 185

If it is a string, it is a name of file that contains the above
Kenichi Handa's avatar
Kenichi Handa committed
186 187 188 189 190
information.   Each line of the file must be this format:
	0xXXX 0xYYY
where XXX is a hexadecimal representation of CODE-n and YYY is a
hexadecimal representation of CHAR-n.  A line starting with `#' is a
comment line.
191

192 193 194 195 196 197 198 199 200 201
`:subset'

VALUE must be a list:
	( PARENT MIN-CODE MAX-CODE OFFSET )
PARENT is a parent charset.  MIN-CODE and MAX-CODE specify the range
of characters inherited from the parent.  OFFSET is an integer value
to add to a code point of the parent charset to get the corresponding
code point of this charset.

`:superset'
202 203 204 205

VALUE must be a list of parent charsets.  The charset inherits
characters from them.  Each element of the list may be a cons (PARENT
. OFFSET), where PARENT is a parent charset, and OFFSET is an offset
206 207
value to add to a code point of PARENT to get the corresponding code
point of this charset.
208 209 210 211 212 213 214

`:unify-map'

VALUE must be vector or string.

If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ],
where CODE-n is a code-point of the charset, and CHAR-n is the
Dave Love's avatar
Dave Love committed
215
corresponding Unicode character code.
216 217

If it is a string, it is a name of file that contains the above
Kenichi Handa's avatar
Kenichi Handa committed
218 219
information.  The file format is the same as what described for `:map'
attribute."
Kenichi Handa's avatar
Kenichi Handa committed
220 221 222 223 224 225 226 227
  (when (vectorp (car props))
    ;; Old style code:
    ;;   (define-charset CHARSET-ID CHARSET-SYMBOL INFO-VECTOR)
    ;; Convert the argument to make it fit with the current style.
    (let ((vec (car props)))
      (setq props (convert-define-charset-argument name vec)
	    name docstring
	    docstring (aref vec 8))))
228 229
  (let ((attrs (mapcar 'list '(:dimension
			       :code-space
230 231
			       :min-code
			       :max-code
232 233 234 235 236 237 238 239
			       :iso-final-char
			       :iso-revision-number
			       :emacs-mule-id
			       :ascii-compatible-p
			       :supplementary-p
			       :invalid-code
			       :code-offset
			       :map
240 241
			       :subset
			       :superset
242 243 244 245 246 247
			       :unify-map
			       :plist))))

    ;; If :dimension is omitted, get the dimension from :code-space.
    (let ((dimension (plist-get props :dimension)))
      (or dimension
248 249
	  (let ((code-space (plist-get props :code-space)))
	    (setq dimension (if code-space (/ (length code-space) 2) 4))
250 251
	    (setq props (plist-put props :dimension dimension)))))

252 253 254 255 256 257 258 259
    (let ((code-space (plist-get props :code-space)))
      (or code-space
	  (let ((dimension (plist-get props :dimension)))
	    (setq code-space (make-vector 8 0))
	    (dotimes (i dimension)
	      (aset code-space (1+ (* i 2)) #xFF))
	    (setq props (plist-put props :code-space code-space)))))

Kenichi Handa's avatar
Kenichi Handa committed
260 261 262 263 264
    ;; If :emacs-mule-id is specified, update emacs-mule-charset-table.
    (let ((emacs-mule-id (plist-get props :emacs-mule-id)))
      (if (integerp emacs-mule-id)
	  (aset emacs-mule-charset-table emacs-mule-id name)))

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    (dolist (slot attrs)
      (setcdr slot (plist-get props (car slot))))

    ;; Make sure that the value of :code-space is a vector of 8
    ;; elements.
    (let* ((slot (assq :code-space attrs))
	   (val (cdr slot))
	   (len (length val)))
      (if (< len 8)
	  (setcdr slot
		  (vconcat val (make-vector (- 8 len) 0)))))

    ;; Add :name and :docstring properties to PROPS.
    (setq props
	  (cons :name (cons name (cons :docstring (cons docstring props)))))
    (or (plist-get props :short-name)
	(plist-put props :short-name (symbol-name name)))
    (or (plist-get props :long-name)
	(plist-put props :long-name (plist-get props :short-name)))
Dave Love's avatar
Dave Love committed
284 285 286 287 288 289 290
    ;; We can probably get a worthwhile amount in purespace.
    (setq props
	  (mapcar (lambda (elt)
		    (if (stringp elt)
			(purecopy elt)
		      elt))
		  props))
291 292 293 294 295
    (setcdr (assq :plist attrs) props)

    (apply 'define-charset-internal name (mapcar 'cdr attrs))))


Karl Heuer's avatar
Karl Heuer committed
296
(defun load-with-code-conversion (fullname file &optional noerror nomessage)
297 298
  "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
The file contents are decoded before evaluation if necessary.
299
If optional third arg NOERROR is non-nil,
Karl Heuer's avatar
Karl Heuer committed
300 301
 report no error if FILE doesn't exist.
Print messages at start and end of loading unless
302
 optional fourth arg NOMESSAGE is non-nil.
Karl Heuer's avatar
Karl Heuer committed
303 304 305 306 307 308 309 310
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.
311
	    (let ((default-major-mode 'fundamental-mode))
Karl Heuer's avatar
Karl Heuer committed
312 313 314
	      ;; We can't use `generate-new-buffer' because files.el
	      ;; is not yet loaded.
	      (get-buffer-create (generate-new-buffer-name " *load*"))))
315 316 317 318 319 320 321
	   (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
322
	(push file preloaded-file-list))
Karl Heuer's avatar
Karl Heuer committed
323
      (unwind-protect
324
	  (let ((load-file-name fullname)
325
		(set-auto-coding-for-load t)
326
		(inhibit-file-name-operation nil))
327
	    (with-current-buffer buffer
328 329 330 331 332
              ;; 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.
              (set-buffer-multibyte t)
333 334 335
	      ;; Don't let deactivate-mark remain set.
	      (let (deactivate-mark)
		(insert-file-contents fullname))
336 337 338 339 340
	      ;; 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
Kenichi Handa's avatar
Kenichi Handa committed
341 342
		       (or (eq (coding-system-type last-coding-system-used)
			       'raw-text)))
343
		  (set-buffer-multibyte nil))
Karl Heuer's avatar
Karl Heuer committed
344 345
	      ;; Make `kill-buffer' quiet.
	      (set-buffer-modified-p nil))
346
	    ;; Have the original buffer current while we eval.
347 348 349
	    (eval-buffer buffer nil
			 ;; This is compatible with what `load' does.
			 (if purify-flag file fullname)
350 351 352
			 ;; If this Emacs is running with --unibyte,
			 ;; convert multibyte strings to unibyte
			 ;; after reading them.
353
;;			 (not default-enable-multibyte-characters)
354
			 nil t
355
			 ))
356 357
	(let (kill-buffer-hook kill-buffer-query-functions)
	  (kill-buffer buffer)))
358 359
      (unless purify-flag
 	(do-after-load-evaluation fullname))
360

361 362 363 364
      (unless (or nomessage noninteractive)
	(if source
	    (message "Loading %s (source)...done" file)
	  (message "Loading %s...done" file)))
Karl Heuer's avatar
Karl Heuer committed
365 366
      t)))

Kenichi Handa's avatar
Kenichi Handa committed
367
(defun charset-info (charset)
Karl Heuer's avatar
Karl Heuer committed
368
  "Return a vector of information of CHARSET.
Kenichi Handa's avatar
Kenichi Handa committed
369
This function is provided for backward compatibility.
Karl Heuer's avatar
Karl Heuer committed
370 371 372 373 374 375

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,
Kenichi Handa's avatar
Kenichi Handa committed
376
	PLIST.
Karl Heuer's avatar
Karl Heuer committed
377
where
Kenichi Handa's avatar
Kenichi Handa committed
378 379 380 381 382 383 384 385 386 387
CHARSET-ID is always 0.
BYTES is always 0.
DIMENSION is the number of bytes of a code-point of the charset:
  1, 2, 3, or 4.
CHARS is the number of characters in a dimension:
  94, 96, 128, or 256.
WIDTH is always 0.
DIRECTION is always 0.
LEADING-CODE-BASE is always 0.
LEADING-CODE-EXT is always 0.
Karl Heuer's avatar
Karl Heuer committed
388
ISO-FINAL-CHAR (character) is the final character of the
Kenichi Handa's avatar
Kenichi Handa committed
389 390
  corresponding ISO 2022 charset.  If the charset is not assigned
  any final character, the value is -1.
Kenichi Handa's avatar
Kenichi Handa committed
391 392
ISO-GRAPHIC-PLANE is always 0.
REVERSE-CHARSET is always -1.
Karl Heuer's avatar
Karl Heuer committed
393 394 395 396 397 398
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."
Kenichi Handa's avatar
Kenichi Handa committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  (vector 0
	  0
	  (charset-dimension charset)
	  (charset-chars charset)
	  0
	  0
	  0
	  0
	  (charset-iso-final-char charset)
	  0
	  -1
	  (get-charset-property charset :short-name)
	  (get-charset-property charset :short-name)
	  (charset-description charset)
	  (charset-plist charset)))
Karl Heuer's avatar
Karl Heuer committed
414

415 416 417 418
;; 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.

Kenichi Handa's avatar
Kenichi Handa committed
419 420 421
(defun charset-id (charset)
  "Always return 0.  This is provided for backward compatibility."
  0)
422
(make-obsolete 'charset-id "do not use it." "23.1")
Karl Heuer's avatar
Karl Heuer committed
423 424

(defmacro charset-bytes (charset)
Kenichi Handa's avatar
Kenichi Handa committed
425 426
  "Always return 0.  This is provided for backward compatibility."
  0)
427
(make-obsolete 'charset-bytes "do not use it." "23.1")
428 429 430 431 432 433 434 435

(defun get-charset-property (charset propname)
  "Return the value of CHARSET's PROPNAME property.
This is the last value stored with
 (put-charset-property CHARSET PROPNAME VALUE)."
  (plist-get (charset-plist charset) propname))

(defun put-charset-property (charset propname value)
436
  "Set CHARSETS's PROPNAME property to value VALUE.
437 438 439 440 441 442 443 444 445
It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
  (set-charset-plist charset
		     (plist-put (charset-plist charset) propname value)))

(defun charset-description (charset)
  "Return description string of CHARSET."
  (plist-get (charset-plist charset) :docstring))

(defun charset-dimension (charset)
Dave Love's avatar
Dave Love committed
446
  "Return dimension of CHARSET."
447 448
  (plist-get (charset-plist charset) :dimension))

449
(defun charset-chars (charset &optional dimension)
Dave Love's avatar
Dave Love committed
450
  "Return number of characters contained in DIMENSION of CHARSET.
451 452
DIMENSION defaults to the first dimension."
  (unless dimension (setq dimension 1))
Kenichi Handa's avatar
Kenichi Handa committed
453
  (let ((code-space (plist-get (charset-plist charset) :code-space)))
454 455
    (1+ (- (aref code-space (1- (* 2 dimension)))
	   (aref code-space (- (* 2 dimension) 2))))))
456 457

(defun charset-iso-final-char (charset)
Dave Love's avatar
Dave Love committed
458 459
  "Return ISO-2022 final character of CHARSET.
Return -1 if charset isn't an ISO 2022 one."
460 461
  (or (plist-get (charset-plist charset) :iso-final-char)
      -1))
Karl Heuer's avatar
Karl Heuer committed
462 463

(defmacro charset-short-name (charset)
464 465
  "Return short name of CHARSET."
  (plist-get (charset-plist charset) :short-name))
Karl Heuer's avatar
Karl Heuer committed
466 467

(defmacro charset-long-name (charset)
468 469
  "Return long name of CHARSET."
  (plist-get (charset-plist charset) :long-name))
Karl Heuer's avatar
Karl Heuer committed
470

471
(defun charset-list ()
472
  "Return list of all charsets ever defined."
473
  charset-list)
474
(make-obsolete 'charset-list "use variable `charset-list'." "23.1")
475

Kenichi Handa's avatar
Kenichi Handa committed
476 477

;;; CHARACTER
478
(define-obsolete-function-alias 'char-valid-p 'characterp "23.1")
Kenichi Handa's avatar
Kenichi Handa committed
479

480
(defun generic-char-p (char)
Kenichi Handa's avatar
Kenichi Handa committed
481
  "Always return nil.  This is provided for backward compatibility."
482
  nil)
483
(make-obsolete 'generic-char-p "generic characters no longer exist." "23.1")
Kenichi Handa's avatar
Kenichi Handa committed
484 485 486 487 488 489

(defun make-char-internal (charset-id &optional code1 code2)
  (let ((charset (aref emacs-mule-charset-table charset-id)))
    (or charset
	(error "Invalid Emacs-mule charset ID: %d" charset-id))
    (make-char charset code1 code2)))
490

Chong Yidong's avatar
Chong Yidong committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504
;; Save the ASCII case table in case we need it later.  Some locales
;; (such as Turkish) modify the case behavior of ASCII characters,
;; which can interfere with networking code that uses ASCII strings.

(defvar ascii-case-table
  ;; Code copied from copy-case-table to avoid requiring case-table.el
  (let ((tbl (copy-sequence (standard-case-table)))
	(up  (char-table-extra-slot (standard-case-table) 0)))
    (if up (set-char-table-extra-slot tbl 0 (copy-sequence up)))
    (set-char-table-extra-slot tbl 1 nil)
    (set-char-table-extra-slot tbl 2 nil)
    tbl)
  "Case table for the ASCII character set.")

505
;; Coding system stuff
Karl Heuer's avatar
Karl Heuer committed
506

507 508
;; Coding system is a symbol that has been defined by the function
;; `define-coding-system'.
Karl Heuer's avatar
Karl Heuer committed
509

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
(defconst coding-system-iso-2022-flags
  '(long-form
    ascii-at-eol
    ascii-at-cntl
    7-bit
    locking-shift
    single-shift
    designation
    revision
    direction
    init-at-bol
    designate-at-bol
    safe
    latin-extra
    composition
525 526 527
    euc-tw-shift
    use-roman
    use-oldjis)
528
  "List of symbols that control ISO-2022 encoder/decoder.
Karl Heuer's avatar
Karl Heuer committed
529

Dave Love's avatar
Dave Love committed
530
The value of the `:flags' attribute in the argument of the function
531
`define-coding-system' must be one of them.
Karl Heuer's avatar
Karl Heuer committed
532

533 534 535
If `long-form' is specified, use a long designation sequence on
encoding for the charsets `japanese-jisx0208-1978', `chinese-gb2312',
and `japanese-jisx0208'.  The long designation sequence doesn't
Dave Love's avatar
Dave Love committed
536
conform to ISO 2022, but is used by such coding systems as
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
`compound-text'.

If `ascii-at-eol' is specified, designate ASCII to g0 at end of line
on encoding.

If `ascii-at-cntl' is specified, designate ASCII to g0 before control
codes and SPC on encoding.

If `7-bit' is specified, use 7-bit code only on encoding.

If `locking-shift' is specified, decode locking-shift code correctly
on decoding, and use locking-shift to invoke a graphic element on
encoding.

If `single-shift' is specified, decode single-shift code correctly on
decoding, and use single-shift to invoke a graphic element on encoding.

If `designation' is specified, decode designation code correctly on
decoding, and use designation to designate a charset to a graphic
element on encoding.

If `revision' is specified, produce an escape sequence to specify
revision number of a charset on encoding.  Such an escape sequence is
always correctly decoded on decoding.

If `direction' is specified, decode ISO6429's code for specifying
Dave Love's avatar
Dave Love committed
563
direction correctly, and produce the code on encoding.
564 565 566

If `init-at-bol' is specified, on encoding, it is assumed that
invocation and designation statuses are reset at each beginning of
Dave Love's avatar
Dave Love committed
567
line even if `ascii-at-eol' is not specified; thus no codes for
568 569 570 571 572
resetting them are produced.

If `safe' is specified, on encoding, characters not supported by a
coding are replaced with `?'.

Dave Love's avatar
Dave Love committed
573
If `latin-extra' is specified, the code-detection routine assumes that a
574 575 576
code specified in `latin-extra-code-table' (which see) is valid.

If `composition' is specified, an escape sequence to specify
Dave Love's avatar
Dave Love committed
577
composition sequence is correctly decoded on decoding, and is produced
578 579 580
on encoding.

If `euc-tw-shift' is specified, the EUC-TW specific shifting code is
Dave Love's avatar
Dave Love committed
581
correctly decoded on decoding, and is produced on encoding.
582

Dave Love's avatar
Dave Love committed
583 584 585 586 587 588
If `use-roman' is specified, JIS0201-1976-Roman is designated instead
of ASCII.

If `use-oldjis' is specified, JIS0208-1976 is designated instead of
JIS0208-1983.")

589
(defun define-coding-system (name docstring &rest props)
Dave Love's avatar
Dave Love committed
590
  "Define NAME (a symbol) as a coding system with DOCSTRING and attributes.
591 592 593
The remaining arguments must come in pairs ATTRIBUTE VALUE.  ATTRIBUTE
may be any symbol.

Dave Love's avatar
Dave Love committed
594 595
The following attributes have special meanings.  Those labeled as
\"(required)\", should not be omitted.
596 597 598 599 600 601 602 603

`:mnemonic' (required)

VALUE is a character to display on mode line for the coding system.

`:coding-type' (required)

VALUE must be one of `charset', `utf-8', `utf-16', `iso-2022',
Dave Love's avatar
Dave Love committed
604
`emacs-mule', `shift-jis', `ccl', `raw-text', `undecided'.
605

Dave Love's avatar
Dave Love committed
606
`:eol-type'
607

Dave Love's avatar
Dave Love committed
608
VALUE is the EOL (end-of-line) format of the coding system.  It must be
609 610 611
one of `unix', `dos', `mac'.  The symbol `unix' means Unix-like EOL
\(i.e. single LF), `dos' means DOS-like EOL \(i.e. sequence of CR LF),
and `mac' means MAC-like EOL \(i.e. single CR).  If omitted, on
Dave Love's avatar
Dave Love committed
612
decoding by the coding system, Emacs automatically detects the EOL
613 614
format of the source text.

Kenichi Handa's avatar
Kenichi Handa committed
615
`:charset-list'
616 617 618 619

VALUE must be a list of charsets supported by the coding system.  On
encoding by the coding system, if a character belongs to multiple
charsets in the list, a charset that comes earlier in the list is
Kenichi Handa's avatar
Kenichi Handa committed
620 621 622 623
selected.  If `:coding-type' is `iso-2022', VALUE may be `iso-2022',
which indicates that the coding system supports all ISO-2022 based
charsets.  If `:coding-type' is `emacs-mule', VALUE may be
`emacs-mule', which indicates that the coding system supports all
Dave Love's avatar
Dave Love committed
624
charsets that have the `:emacs-mule-id' property.
625

Dave Love's avatar
Dave Love committed
626
`:ascii-compatible-p'
627 628

If VALUE is non-nil, the coding system decodes all 7-bit bytes into
Dave Love's avatar
Dave Love committed
629
the corresponding ASCII characters, and encodes all ASCII characters
Dave Love's avatar
Dave Love committed
630
back to the corresponding 7-bit bytes.  VALUE defaults to nil.
631

Dave Love's avatar
Dave Love committed
632
`:decode-translation-table'
633 634 635

VALUE must be a translation table to use on decoding.

Dave Love's avatar
Dave Love committed
636
`:encode-translation-table'
637 638 639

VALUE must be a translation table to use on encoding.

Dave Love's avatar
Dave Love committed
640
`:post-read-conversion'
641 642 643 644

VALUE must be a function to call after some text is inserted and
decoded by the coding system itself and before any functions in
`after-insert-functions' are called.  The arguments to this function
Dave Love's avatar
Dave Love committed
645 646 647
are the same as those of a function in `after-insert-file-functions',
i.e. LENGTH of the text to be decoded with point at the head of it,
and the function should leave point unchanged.
648 649 650 651 652 653

`:pre-write-conversion'

VALUE must be a function to call after all functions in
`write-region-annotate-functions' and `buffer-file-format' are called,
and before the text is encoded by the coding system itself.  The
Dave Love's avatar
Dave Love committed
654 655
arguments to this function are the same as those of a function in
`write-region-annotate-functions'.
656 657 658 659 660 661

`:default-char'

VALUE must be a character.  On encoding, a character not supported by
the coding system is replaced with VALUE.

Kenichi Handa's avatar
Kenichi Handa committed
662 663 664 665 666
`:for-unibyte'

VALUE non-nil means that visiting a file with the coding system
results in a unibyte buffer.

667 668 669 670
`:eol-type'

VALUE must be `unix', `dos', `mac'.  The symbol `unix' means Unix-like
EOL (LF), `dos' means DOS-like EOL (CRLF), and `mac' means MAC-like
Dave Love's avatar
Dave Love committed
671 672
EOL (CR).  If omitted, on decoding, the coding system detects EOL
format automatically, and on encoding, uses Unix-like EOL.
673 674 675

`:mime-charset'

Dave Love's avatar
Dave Love committed
676 677
VALUE must be a symbol whose name is that of a MIME charset converted
to lower case.
678

Dave Love's avatar
Dave Love committed
679 680 681
`:mime-text-unsuitable'

VALUE non-nil means the `:mime-charset' property names a charset which
682
is unsuitable for the top-level media type \"text\".
Dave Love's avatar
Dave Love committed
683

684 685
`:flags'

Dave Love's avatar
Dave Love committed
686 687
VALUE must be a list of symbols that control the ISO-2022 converter.
Each must be a member of the list `coding-system-iso-2022-flags'
688 689 690 691 692
\(which see).  This attribute has a meaning only when `:coding-type'
is `iso-2022'.

`:designation'

Dave Love's avatar
Dave Love committed
693
VALUE must be a vector [G0-USAGE G1-USAGE G2-USAGE G3-USAGE].
694 695 696 697
GN-USAGE specifies the usage of graphic register GN as follows.

If it is nil, no charset can be designated to GN.

Dave Love's avatar
Dave Love committed
698
If it is a charset, the charset is initially designated to GN, and
699 700 701
never used by the other charsets.

If it is a list, the elements must be charsets, nil, 94, or 96.  GN
Dave Love's avatar
Dave Love committed
702 703 704 705 706
can be used by all the listed charsets.  If the list contains 94, any
iso-2022 charset whose code-space ranges are 94 long can be designated
to GN.  If the list contains 96, any charsets whose whose ranges are
96 long can be designated to GN.  If the first element is a charset,
that charset is initially designated to GN.
707 708 709 710 711

This attribute has a meaning only when `:coding-type' is `iso-2022'.

`:bom'

Dave Love's avatar
Dave Love committed
712 713 714
This attributes specifies whether the coding system uses a `byte order
mark'.  VALUE must nil, t, or cons of coding systems whose
`:coding-type' is `utf-16'.
715

716 717 718 719 720 721 722 723 724 725 726 727
If the value is nil, on decoding, don't treat the first two-byte as
BOM, and on encoding, don't produce BOM bytes.

If the value is t, on decoding, skip the first two-byte as BOM, and on
encoding, produce BOM bytes accoding to the value of `:endian'.

If the value is cons, on decoding, check the first two-byte.  If theyq
are 0xFE 0xFF, use the car part coding system of the value.  If they
are 0xFF 0xFE, use the car part coding system of the value.
Otherwise, treat them as bytes for a normal character.  On encoding,
produce BOM bytes accoding to the value of `:endian'.

728 729 730 731
This attribute has a meaning only when `:coding-type' is `utf-16'.

`:endian'

732 733
VALUE must be `big' or `little' specifying big-endian and
little-endian respectively.  The default value is `big'.
734 735 736 737 738

This attribute has a meaning only when `:coding-type' is `utf-16'.

`:ccl-decoder'

Dave Love's avatar
Dave Love committed
739 740 741
VALUE is a symbol representing the registered CCL program used for
decoding.  This attribute has a meaning only when `:coding-type' is
`ccl'.
742 743 744

`:ccl-encoder'

Dave Love's avatar
Dave Love committed
745 746 747
VALUE is a symbol representing the registered CCL program used for
encoding.  This attribute has a meaning only when `:coding-type' is
`ccl'."
748 749 750 751 752
  (let* ((common-attrs (mapcar 'list
			       '(:mnemonic
				 :coding-type
				 :charset-list
				 :ascii-compatible-p
753
				 :decode-translation-table
754 755 756 757
				 :encode-translation-table
				 :post-read-conversion
				 :pre-write-conversion
				 :default-char
758
				 :for-unibyte
759 760 761 762 763 764 765 766 767
				 :plist
				 :eol-type)))
	 (coding-type (plist-get props :coding-type))
	 (spec-attrs (mapcar 'list
			     (cond ((eq coding-type 'iso-2022)
				    '(:initial
				      :reg-usage
				      :request
				      :flags))
768 769
				   ((eq coding-type 'utf-8)
				    '(:bom))
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
				   ((eq coding-type 'utf-16)
				    '(:bom
				      :endian))
				   ((eq coding-type 'ccl)
				    '(:ccl-decoder
				      :ccl-encoder
				      :valids))))))

    (dolist (slot common-attrs)
      (setcdr slot (plist-get props (car slot))))

    (dolist (slot spec-attrs)
      (setcdr slot (plist-get props (car slot))))

    (if (eq coding-type 'iso-2022)
	(let ((designation (plist-get props :designation))
	      (flags (plist-get props :flags))
	      (initial (make-vector 4 nil))
	      (reg-usage (cons 4 4))
	      request elt)
	  (dotimes (i 4)
	    (setq elt (aref designation i))
	    (cond ((charsetp elt)
		   (aset initial i elt)
		   (setq request (cons (cons elt i) request)))
		  ((consp elt)
		   (aset initial i (car elt))
		   (if (charsetp (car elt))
		       (setq request (cons (cons (car elt) i) request)))
		   (dolist (e (cdr elt))
		     (cond ((charsetp e)
			    (setq request (cons (cons e i) request)))
			   ((eq e 94)
			    (setcar reg-usage i))
			   ((eq e 96)
			    (setcdr reg-usage i))
			   ((eq e t)
			    (setcar reg-usage i)
			    (setcdr reg-usage i)))))))
	  (setcdr (assq :initial spec-attrs) initial)
	  (setcdr (assq :reg-usage spec-attrs) reg-usage)
	  (setcdr (assq :request spec-attrs) request)

	  ;; Change :flags value from a list to a bit-mask.
	  (let ((bits 0)
		(i 0))
	    (dolist (elt coding-system-iso-2022-flags)
	      (if (memq elt flags)
		  (setq bits (logior bits (lsh 1 i))))
	      (setq i (1+ i)))
	    (setcdr (assq :flags spec-attrs) bits))))

    ;; Add :name and :docstring properties to PROPS.
    (setq props
Dave Love's avatar
Dave Love committed
824 825
	  (cons :name (cons name (cons :docstring (cons (purecopy docstring)
							props)))))
826
    (setcdr (assq :plist common-attrs) props)
827
    (apply 'define-coding-system-internal
828
	   name (mapcar 'cdr (append common-attrs spec-attrs)))))
Karl Heuer's avatar
Karl Heuer committed
829

830
(defun coding-system-doc-string (coding-system)
831
  "Return the documentation string for CODING-SYSTEM."
832
  (plist-get (coding-system-plist coding-system) :docstring))
Karl Heuer's avatar
Karl Heuer committed
833 834

(defun coding-system-mnemonic (coding-system)
835
  "Return the mnemonic character of CODING-SYSTEM.
Dave Love's avatar
Dave Love committed
836
The mnemonic character of a coding system is used in mode line to
837
indicate the coding system.  If CODING-SYSTEM is nil, return ?=."
838
  (plist-get (coding-system-plist coding-system) :mnemonic))
Karl Heuer's avatar
Karl Heuer committed
839

840 841 842 843 844
(defun coding-system-type (coding-system)
  "Return the coding type of CODING-SYSTEM.
A coding type is a symbol indicating the encoding method of CODING-SYSTEM.
See the function `define-coding-system' for more detail."
  (plist-get (coding-system-plist coding-system) :coding-type))
845

846
(defun coding-system-charset-list (coding-system)
Dave Love's avatar
Dave Love committed
847
  "Return list of charsets supported by CODING-SYSTEM.
848 849 850
If CODING-SYSTEM supports all ISO-2022 charsets, return `iso-2022'.
If CODING-SYSTEM supports all emacs-mule charsets, return `emacs-mule'."
  (plist-get (coding-system-plist coding-system) :charset-list))
851

852 853 854
(defun coding-system-category (coding-system)
  "Return a category symbol of CODING-SYSTEM."
  (plist-get (coding-system-plist coding-system) :category))
855 856

(defun coding-system-get (coding-system prop)
Dave Love's avatar
Dave Love committed
857 858 859 860 861
  "Extract a value from CODING-SYSTEM's property list for property PROP.
For compatibility with Emacs 20/21, this accepts old-style symbols
like `mime-charset' as well as the current style like `:mime-charset'."
  (or (plist-get (coding-system-plist coding-system) prop)
      (if (not (keywordp prop))
Kenichi Handa's avatar
Kenichi Handa committed
862 863 864 865 866 867
	  ;; For backward compatiblity.
	  (if (eq prop 'ascii-incompatible)
	      (not (plist-get (coding-system-plist coding-system)
			      :ascii-compatible-p))
	    (plist-get (coding-system-plist coding-system)
		       (intern (concat ":" (symbol-name prop))))))))
868

869 870 871
(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))
872
	 (val (cond ((eq eol-type 0) eol-mnemonic-unix)
873 874
		    ((eq eol-type 1) eol-mnemonic-dos)
		    ((eq eol-type 2) eol-mnemonic-mac)
875
		    (t eol-mnemonic-undecided))))
876 877 878 879
    (if (stringp val)
	val
      (char-to-string val))))

880 881 882 883 884 885 886 887 888 889 890 891 892
(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)))))))

893 894
(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.
895
Two coding systems are identical if both symbols are equal
896 897
or one is an alias of the other."
  (or (eq coding-system-1 coding-system-2)
898 899
      (and (equal (coding-system-plist coding-system-1)
		  (coding-system-plist coding-system-2))
900 901 902 903 904
	   (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)))))))

905
(defun add-to-coding-system-list (coding-system)
Dave Love's avatar
Dave Love committed
906
  "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
907 908 909 910 911 912 913 914 915 916 917 918 919
  (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))))))

920
(defun coding-system-list (&optional base-only)
921
  "Return a list of all existing non-subsidiary coding systems.
Dave Love's avatar
Dave Love committed
922 923
If optional arg BASE-ONLY is non-nil, only base coding systems are
listed.  The value doesn't include subsidiary coding systems which are
924 925
made from bases and aliases automatically for various end-of-line
formats (e.g. iso-latin-1-unix, koi8-r-dos)."
926 927 928 929 930 931 932
  (let ((codings nil))
    (dolist (coding coding-system-list)
      (if (eq (coding-system-base coding) coding)
	  (if base-only
	      (setq codings (cons coding codings))
	    (dolist (alias (coding-system-aliases coding))
	      (setq codings (cons alias codings))))))
933 934
    codings))

935
(defconst char-coding-system-table nil
936 937
  "It exists just for backward compatibility, and the value is always nil.")
(make-obsolete-variable 'char-coding-system-table nil "23.1")
938

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
(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
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
     ((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))
1012
     ((eq type 'ccl)
1013
      `(,name 4 ,mnemonic ,doc-string
1014
	      (,(plist-get props 'decode) . ,(plist-get props 'encode))
1015
	      ,properties ,eol-type))
1016
     (t
1017
      (error "unsupported XEmacs style make-coding-style arguments: %S"
1018 1019
	     `(,name ,type ,doc-string ,props))))))

1020
(defun make-coding-system (coding-system type mnemonic doc-string
1021 1022 1023 1024
					 &optional
					 flags
					 properties
					 eol-type)
1025
  "Define a new coding system CODING-SYSTEM (symbol).
1026
This function is provided for backward compatibility."
1027
  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
Eli Zaretskii's avatar
Eli Zaretskii committed
1028 1029
  ;; is a symbol, perhaps, this function is called with XEmacs-style
  ;; arguments.  Here, try to transform that kind of arguments to
1030 1031 1032 1033 1034
  ;; 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
1035
	      type (nth 1 args)
1036 1037 1038 1039 1040 1041
	      mnemonic (nth 2 args)
	      doc-string (nth 3 args)
	      flags (nth 4 args)
	      properties (nth 5 args)
	      eol-type (nth 6 args))))

Kenichi Handa's avatar
Kenichi Handa committed
1042 1043 1044 1045 1046 1047 1048
  (setq type
	(cond ((eq type 0) 'emacs-mule)
	      ((eq type 1) 'shift-jis)
	      ((eq type 2) 'iso2022)
	      ((eq type 3) 'big5)
	      ((eq type 4) 'ccl)
	      ((eq type 5) 'raw-text)
1049
	      (t
Kenichi Handa's avatar
Kenichi Handa committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	       (error "Invalid coding system type: %s" type))))

  (setq properties
	(let ((plist nil) key)
	  (dolist (elt properties)
	    (setq key (car elt))
	    (cond ((eq key 'post-read-conversion)
		   (setq key :post-read-conversion))
		  ((eq key 'pre-write-conversion)
		   (setq key :pre-write-conversion))
		  ((eq key 'translation-table-for-decode)
		   (setq key :decode-translation-table))
		  ((eq key 'translation-table-for-encode)
		   (setq key :encode-translation-table))
		  ((eq key 'safe-charsets)
		   (setq key :charset-list))
		  ((eq key 'mime-charset)
		   (setq key :mime-charset))
		  ((eq key 'valid-codes)
		   (setq key :valids)))
	    (setq plist (plist-put plist key (cdr elt))))
	  plist))
1072
  (setq properties (plist-put properties :mnemonic mnemonic))
Kenichi Handa's avatar
Kenichi Handa committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  (plist-put properties :coding-type type)
  (cond ((eq eol-type 0) (setq eol-type 'unix))
	((eq eol-type 1) (setq eol-type 'dos))
	((eq eol-type 2) (setq eol-type 'mac))
	((vectorp eol-type) (setq eol-type nil)))
  (plist-put properties :eol-type eol-type)

  (cond
   ((eq type 'iso2022)
    (plist-put properties :flags
	       (list (and (or (consp (nth 0 flags))
			      (consp (nth 1 flags))
			      (consp (nth 2 flags))
			      (consp (nth 3 flags))) 'designation)
		     (or (nth 4 flags) 'long-form)
		     (and (nth 5 flags) 'ascii-at-eol)
		     (and (nth 6 flags) 'ascii-at-cntl)
		     (and (nth 7 flags) '7-bit)
		     (and (nth 8 flags) 'locking-shift)
		     (and (nth 9 flags) 'single-shift)
		     (and (nth 10 flags) 'use-roman)
		     (and (nth 11 flags) 'use-oldjis)
		     (or (nth 12 flags) 'direction)
		     (and (nth 13 flags) 'init-at-bol)
		     (and (nth 14 flags) 'designate-at-bol)
		     (and (nth 15 flags) 'safe)
		     (and (nth 16 flags) 'latin-extra)))
    (plist-put properties :designation
	       (let ((vec (make-vector 4 nil)))
		 (dotimes (i 4)
		   (let ((spec (nth i flags)))
		     (if (eq spec t)
			 (aset vec i '(94 96))
		     (if (consp spec)
			 (progn
			   (if (memq t spec)
			       (setq spec (append (delq t spec) '(94 96))))
			   (aset vec i spec))))))
		 vec)))

   ((eq type 'ccl)
    (plist-put properties :ccl-decoder (car flags))
    (plist-put properties :ccl-encoder (cdr flags))))

  (apply 'define-coding-system coding-system doc-string properties))
Karl Heuer's avatar
Karl Heuer committed
1118

1119 1120
(make-obsolete 'make-coding-system 'define-coding-system "23.1")

1121