(set-coding-priority): Call
[bpt/emacs.git] / lisp / international / mule.el
1 ;;; mule.el --- basic commands for mulitilingual environment
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual, character set, coding system
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (defconst mule-version "3.0 (MOMIJINOGA)" "\
28 Version number and name of this version of MULE (multilingual environment).")
29
30 (defconst mule-version-date "1998.1.1" "\
31 Distribution date of this version of MULE (multilingual environment).")
32
33 (defun load-with-code-conversion (fullname file &optional noerror nomessage)
34 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
35 The file contents are decoded before evaluation if necessary.
36 If optional second arg NOERROR is non-nil,
37 report no error if FILE doesn't exist.
38 Print messages at start and end of loading unless
39 optional third arg NOMESSAGE is non-nil.
40 Return t if file exists."
41 (if (null (file-readable-p fullname))
42 (and (null noerror)
43 (signal 'file-error (list "Cannot open load file" file)))
44 ;; Read file with code conversion, and then eval.
45 (let* ((buffer
46 ;; To avoid any autoloading, set default-major-mode to
47 ;; fundamental-mode.
48 ;; So that we don't get completely screwed if the
49 ;; file is encoded in some complicated character set,
50 ;; read it with real decoding, as a multibyte buffer,
51 ;; even if this is a --unibyte Emacs session.
52 (let ((default-major-mode 'fundamental-mode)
53 (default-enable-multibyte-characters t))
54 ;; We can't use `generate-new-buffer' because files.el
55 ;; is not yet loaded.
56 (get-buffer-create (generate-new-buffer-name " *load*"))))
57 (load-in-progress t)
58 (source (save-match-data (string-match "\\.el\\'" fullname))))
59 (unless nomessage
60 (if source
61 (message "Loading %s (source)..." file)
62 (message "Loading %s..." file)))
63 (when purify-flag
64 (setq preloaded-file-list (cons file preloaded-file-list)))
65 (unwind-protect
66 (let ((load-file-name fullname)
67 (inhibit-file-name-operation nil))
68 (save-excursion
69 (set-buffer buffer)
70 (insert-file-contents fullname)
71 ;; Make `kill-buffer' quiet.
72 (set-buffer-modified-p nil))
73 ;; Have the original buffer current while we eval.
74 (eval-buffer buffer nil file
75 ;; If this Emacs is running with --unibyte,
76 ;; convert multibyte strings to unibyte
77 ;; after reading them.
78 ;; (not default-enable-multibyte-characters)
79 ))
80 (let (kill-buffer-hook kill-buffer-query-functions)
81 (kill-buffer buffer)))
82 (let ((hook (assoc file after-load-alist)))
83 (when hook
84 (mapcar (function eval) (cdr hook))))
85 (unless (or nomessage noninteractive)
86 (if source
87 (message "Loading %s (source)...done" file)
88 (message "Loading %s...done" file)))
89 t)))
90
91 ;; API (Application Program Interface) for charsets.
92
93 ;; Return t if OBJ is a quoted symbol
94 ;; and the symbol is the name of a standard charset.
95 (defsubst charset-quoted-standard-p (obj)
96 (and (listp obj) (eq (car obj) 'quote)
97 (symbolp (car-safe (cdr obj)))
98 (let ((vector (get (car-safe (cdr obj)) 'charset)))
99 (and (vectorp vector)
100 (< (aref vector 0) 160)))))
101
102 (defsubst charsetp (object)
103 "T is OBJECT is a charset."
104 (and (symbolp object) (vectorp (get object 'charset))))
105
106 (defsubst charset-info (charset)
107 "Return a vector of information of CHARSET.
108 The elements of the vector are:
109 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
110 LEADING-CODE-BASE, LEADING-CODE-EXT,
111 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
112 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
113 PLIST,
114 where
115 CHARSET-ID (integer) is the identification number of the charset.
116 DIMENSION (integer) is the number of bytes to represent a character of
117 the charset: 1 or 2.
118 CHARS (integer) is the number of characters in a dimension: 94 or 96.
119 BYTE (integer) is the length of multi-byte form of a character in
120 the charset: one of 1, 2, 3, and 4.
121 WIDTH (integer) is the number of columns a character in the charset
122 occupies on the screen: one of 0, 1, and 2.
123 DIRECTION (integer) is the rendering direction of characters in the
124 charset when rendering. If 0, render from right to left, else
125 render from left to right.
126 LEADING-CODE-BASE (integer) is the base leading-code for the
127 charset.
128 LEADING-CODE-EXT (integer) is the extended leading-code for the
129 charset. All charsets of less than 0xA0 has the value 0.
130 ISO-FINAL-CHAR (character) is the final character of the
131 corresponding ISO 2022 charset.
132 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
133 while encoding to variants of ISO 2022 coding system, one of the
134 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
135 REVERSE-CHARSET (integer) is the charset which differs only in
136 LEFT-TO-RIGHT value from the charset. If there's no such a
137 charset, the value is -1.
138 SHORT-NAME (string) is the short name to refer to the charset.
139 LONG-NAME (string) is the long name to refer to the charset
140 DESCRIPTION (string) is the description string of the charset.
141 PLIST (property list) may contain any type of information a user
142 want to put and get by functions `put-charset-property' and
143 `get-charset-property' respectively."
144 (get charset 'charset))
145
146 (defmacro charset-id (charset)
147 "Return charset identification number of CHARSET."
148 (if (charset-quoted-standard-p charset)
149 (aref (charset-info (nth 1 charset)) 0)
150 `(aref (charset-info ,charset) 0)))
151
152 (defmacro charset-bytes (charset)
153 "Return bytes of CHARSET.
154 See the function `charset-info' for more detail."
155 (if (charset-quoted-standard-p charset)
156 (aref (charset-info (nth 1 charset)) 1)
157 `(aref (charset-info ,charset) 1)))
158
159 (defmacro charset-dimension (charset)
160 "Return dimension of CHARSET.
161 See the function `charset-info' for more detail."
162 (if (charset-quoted-standard-p charset)
163 (aref (charset-info (nth 1 charset)) 2)
164 `(aref (charset-info ,charset) 2)))
165
166 (defmacro charset-chars (charset)
167 "Return character numbers contained in a dimension of CHARSET.
168 See the function `charset-info' for more detail."
169 (if (charset-quoted-standard-p charset)
170 (aref (charset-info (nth 1 charset)) 3)
171 `(aref (charset-info ,charset) 3)))
172
173 (defmacro charset-width (charset)
174 "Return width (how many column occupied on a screen) of CHARSET.
175 See the function `charset-info' for more detail."
176 (if (charset-quoted-standard-p charset)
177 (aref (charset-info (nth 1 charset)) 4)
178 `(aref (charset-info ,charset) 4)))
179
180 (defmacro charset-direction (charset)
181 "Return direction of CHARSET.
182 See the function `charset-info' for more detail."
183 (if (charset-quoted-standard-p charset)
184 (aref (charset-info (nth 1 charset)) 5)
185 `(aref (charset-info ,charset) 5)))
186
187 (defmacro charset-iso-final-char (charset)
188 "Return final char of CHARSET.
189 See the function `charset-info' for more detail."
190 (if (charset-quoted-standard-p charset)
191 (aref (charset-info (nth 1 charset)) 8)
192 `(aref (charset-info ,charset) 8)))
193
194 (defmacro charset-iso-graphic-plane (charset)
195 "Return graphic plane of CHARSET.
196 See the function `charset-info' for more detail."
197 (if (charset-quoted-standard-p charset)
198 (aref (charset-info (nth 1 charset)) 9)
199 `(aref (charset-info ,charset) 9)))
200
201 (defmacro charset-reverse-charset (charset)
202 "Return reverse charset of CHARSET.
203 See the function `charset-info' for more detail."
204 (if (charset-quoted-standard-p charset)
205 (aref (charset-info (nth 1 charset)) 10)
206 `(aref (charset-info ,charset) 10)))
207
208 (defmacro charset-short-name (charset)
209 "Return short name of CHARSET.
210 See the function `charset-info' for more detail."
211 (if (charset-quoted-standard-p charset)
212 (aref (charset-info (nth 1 charset)) 11)
213 `(aref (charset-info ,charset) 11)))
214
215 (defmacro charset-long-name (charset)
216 "Return long name of CHARSET.
217 See the function `charset-info' for more detail."
218 (if (charset-quoted-standard-p charset)
219 (aref (charset-info (nth 1 charset)) 12)
220 `(aref (charset-info ,charset) 12)))
221
222 (defmacro charset-description (charset)
223 "Return descriptoin of CHARSET.
224 See the function `charset-info' for more detail."
225 (if (charset-quoted-standard-p charset)
226 (aref (charset-info (nth 1 charset)) 13)
227 `(aref (charset-info ,charset) 13)))
228
229 (defmacro charset-plist (charset)
230 "Return list charset property of CHARSET.
231 See the function `charset-info' for more detail."
232 (if (charset-quoted-standard-p charset)
233 `(aref ,(charset-info (nth 1 charset)) 14)
234 `(aref (charset-info ,charset) 14)))
235
236 (defun set-charset-plist (charset plist)
237 "Set CHARSET's property list to PLIST, and retrun PLIST."
238 (aset (charset-info charset) 14 plist))
239
240 (defun make-char (charset &optional c1 c2)
241 "Return a character of CHARSET and position-codes CODE1 and CODE2.
242 CODE1 and CODE2 are optional, but if you don't supply
243 sufficient position-codes, return a generic character which stands for
244 all characters or group of characters in the character sets.
245 A generic character can be used to index a char table (e.g. syntax-table)."
246 (make-char-internal (charset-id charset) c1 c2))
247
248 (put 'make-char 'byte-compile
249 (function
250 (lambda (form)
251 (let ((charset (nth 1 form)))
252 (if (charset-quoted-standard-p charset)
253 (byte-compile-normal-call
254 (cons 'make-char-internal
255 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
256 (byte-compile-normal-call
257 (cons 'make-char-internal
258 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
259
260 (defun charset-list ()
261 "Return list of charsets ever defined.
262
263 This function is provided for backward compatibility.
264 Now we have the variable `charset-list'."
265 charset-list)
266
267 (make-obsolete 'charset-list
268 "Use the variable charset-list instead.")
269
270 (defsubst generic-char-p (char)
271 "Return t if and only if CHAR is a generic character.
272 See also the documentation of make-char."
273 (and (>= char 0400)
274 (let ((l (split-char char)))
275 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
276 (not (eq (car l) 'composition))))))
277
278 \f
279 ;; Coding system staffs
280
281 ;; Coding system is a symbol that has the property `coding-system'.
282 ;;
283 ;; The value of the property `coding-system' is a vector of the
284 ;; following format:
285 ;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
286 ;; We call this vector as coding-spec. See comments in src/coding.c
287 ;; for more detail.
288
289 (defconst coding-spec-type-idx 0)
290 (defconst coding-spec-mnemonic-idx 1)
291 (defconst coding-spec-doc-string-idx 2)
292 (defconst coding-spec-plist-idx 3)
293 (defconst coding-spec-flags-idx 4)
294
295 ;; PLIST is a property list of a coding system. To share PLIST among
296 ;; alias coding systems, a coding system has PLIST in coding-spec
297 ;; instead of having it in normal property list of Lisp symbol.
298 ;; Here's a list of coding system properties currently being used.
299 ;;
300 ;; o coding-category
301 ;;
302 ;; The value is a coding category the coding system belongs to. The
303 ;; function `make-coding-system' and `define-coding-system-alias' sets
304 ;; this value automatically.
305 ;;
306 ;; o alias-coding-systems
307 ;;
308 ;; The value is a list of coding systems of the same alias group. The
309 ;; first element is the coding system made at first, which we call as
310 ;; `base coding system'. The function `make-coding-system' and
311 ;; `define-coding-system-alias' set this value automatically.
312 ;;
313 ;; o post-read-conversion
314 ;;
315 ;; The value is a function to call after some text is inserted and
316 ;; decoded by the coding system itself and before any functions in
317 ;; `after-insert-functions' are called. The arguments to this
318 ;; function is the same as those of a function in
319 ;; `after-insert-functions', i.e. LENGTH of a text while putting point
320 ;; at the head of the text to be decoded
321 ;;
322 ;; o pre-write-conversion
323 ;;
324 ;; The value is a function to call after all functions in
325 ;; `write-region-annotate-functions' and `buffer-file-format' are
326 ;; called, and before the text is encoded by the coding system itself.
327 ;; The arguments to this function is the same as those of a function
328 ;; in `write-region-annotate-functions', i.e. FROM and TO specifying
329 ;; region of a text.
330 ;;
331 ;; o translation-table-for-decode
332 ;;
333 ;; The value is a translation table to be applied on decoding. See
334 ;; the function `make-translation-table' for the format of translation
335 ;; table.
336 ;;
337 ;; o translation-table-for-encode
338 ;;
339 ;; The value is a translation table to be applied on encoding.
340 ;;
341 ;; o safe-charsets
342 ;;
343 ;; The value is a list of charsets safely supported by the coding
344 ;; system. The value t means that all charsets Emacs handles are
345 ;; supported. Even if some charset is not in this list, it doesn't
346 ;; mean that the charset can't be encoded in the coding system,
347 ;; instead, it just means that some other receiver of a text encoded
348 ;; in the coding system won't be able to handle that charset.
349 ;;
350 ;; o mime-charset
351 ;;
352 ;; The value is a symbol of which name is `MIME-charset' parameter of
353 ;; the coding system.
354
355 ;; Return coding-spec of CODING-SYSTEM
356 (defsubst coding-system-spec (coding-system)
357 (get (check-coding-system coding-system) 'coding-system))
358
359 (defun coding-system-type (coding-system)
360 "Return the coding type of CODING-SYSTEM.
361 A coding type is an integer value indicating the encoding method
362 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
363 (aref (coding-system-spec coding-system) coding-spec-type-idx))
364
365 (defun coding-system-mnemonic (coding-system)
366 "Return the mnemonic character of CODING-SYSTEM.
367 A mnemonic character of a coding system is used in mode line
368 to indicate the coding system."
369 (or (aref (coding-system-spec coding-system) coding-spec-mnemonic-idx)
370 ?-))
371
372 (defun coding-system-doc-string (coding-system)
373 "Return the documentation string for CODING-SYSTEM."
374 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
375
376 (defun coding-system-plist (coding-system)
377 "Return the property list of CODING-SYSTEM."
378 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
379
380 (defun coding-system-flags (coding-system)
381 "Return `flags' of CODING-SYSTEM.
382 A `flags' of a coding system is a vector of length 32 indicating detailed
383 information of a coding system. See the function `make-coding-system'
384 for more detail."
385 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
386
387 (defun coding-system-get (coding-system prop)
388 "Extract a value from CODING-SYSTEM's property list for property PROP."
389 (plist-get (coding-system-plist coding-system) prop))
390
391 (defun coding-system-put (coding-system prop val)
392 "Change value in CODING-SYSTEM's property list PROP to VAL."
393 (let ((plist (coding-system-plist coding-system)))
394 (if plist
395 (plist-put plist prop val)
396 (aset (coding-system-spec coding-system) coding-spec-plist-idx
397 (list prop val)))))
398
399 (defun coding-system-category (coding-system)
400 "Return the coding category of CODING-SYSTEM."
401 (coding-system-get coding-system 'coding-category))
402
403 (defun coding-system-base (coding-system)
404 "Return the base coding system of CODING-SYSTEM.
405 A base coding system is what made by `make-coding-system'.
406 Any alias nor subsidiary coding systems are not base coding system."
407 (car (coding-system-get coding-system 'alias-coding-systems)))
408
409 (defalias 'coding-system-parent 'coding-system-base)
410 (make-obsolete 'coding-system-parent 'coding-system-base)
411
412 ;; Coding system also has a property `eol-type'.
413 ;;
414 ;; This property indicates how the coding system handles end-of-line
415 ;; format. The value is integer 0, 1, 2, or a vector of three coding
416 ;; systems. Each integer value 0, 1, and 2 indicates the format of
417 ;; end-of-line LF, CRLF, and CR respectively. A vector value
418 ;; indicates that the format of end-of-line should be detected
419 ;; automatically. Nth element of the vector is the subsidiary coding
420 ;; system whose `eol-type' property is N.
421
422 (defun coding-system-eol-type (coding-system)
423 "Return eol-type of CODING-SYSTEM.
424 An eol-type is integer 0, 1, 2, or a vector of coding systems.
425
426 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
427 CRLF, and CR respectively.
428
429 A vector value indicates that a format of end-of-line should be
430 detected automatically. Nth element of the vector is the subsidiary
431 coding system whose eol-type is N."
432 (get coding-system 'eol-type))
433
434 ;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
435 (defun make-subsidiary-coding-system (coding-system)
436 (let ((coding-spec (coding-system-spec coding-system))
437 (subsidiaries (vector (intern (format "%s-unix" coding-system))
438 (intern (format "%s-dos" coding-system))
439 (intern (format "%s-mac" coding-system))))
440 (i 0)
441 temp)
442 (while (< i 3)
443 (put (aref subsidiaries i) 'coding-system coding-spec)
444 (put (aref subsidiaries i) 'eol-type i)
445 (setq coding-system-list
446 (cons (aref subsidiaries i) coding-system-list))
447 (setq coding-system-alist
448 (cons (list (symbol-name (aref subsidiaries i)))
449 coding-system-alist))
450 (setq i (1+ i)))
451 subsidiaries))
452
453 (defun make-coding-system (coding-system type mnemonic doc-string
454 &optional flags properties)
455 "Define a new coding system CODING-SYSTEM (symbol).
456 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
457 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
458 in the following format:
459 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
460
461 TYPE is an integer value indicating the type of the coding system as follows:
462 0: Emacs internal format,
463 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
464 2: ISO-2022 including many variants,
465 3: Big5 used mainly on Chinese PC,
466 4: private, CCL programs provide encoding/decoding algorithm,
467 5: Raw-text, which means that text contains random 8-bit codes.
468
469 MNEMONIC is a character to be displayed on mode line for the coding system.
470
471 DOC-STRING is a documentation string for the coding system.
472
473 FLAGS specifies more detailed information of the coding system as follows:
474
475 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
476 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
477 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
478 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
479 SAFE, ACCEPT-LATIN-EXTRA-CODE.
480 CHARSETn are character sets initially designated to Gn graphic registers.
481 If CHARSETn is nil, Gn is never used.
482 If CHARSETn is t, Gn can be used but nothing designated initially.
483 If CHARSETn is a list of character sets, those character sets are
484 designated to Gn on output, but nothing designated to Gn initially.
485 SHORT-FORM non-nil means use short designation sequence on output.
486 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
487 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
488 SPACE on output.
489 SEVEN non-nil means use 7-bit code only on output.
490 LOCKING-SHIFT non-nil means use locking-shift.
491 SINGLE-SHIFT non-nil means use single-shift.
492 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
493 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
494 NO-ISO6429 non-nil means not use ISO6429's direction specification.
495 INIT-BOL non-nil means any designation state is assumed to be reset
496 to initial at each beginning of line on output.
497 DESIGNATION-BOL non-nil means designation sequences should be placed
498 at beginning of line on output.
499 SAFE non-nil means convert unsafe characters to `?' on output.
500 Unsafe characters are what not specified in SAFE-CHARSET.
501 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
502 a code specified in `latin-extra-code-table' (which see) as a valid
503 code of the coding system.
504
505 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
506 decoding and encoding. CCL programs should be specified by their
507 symbols.
508
509 PROPERTIES is an alist of properties vs the corresponding values.
510 These properties are set in PLIST, a property list. This function
511 also sets properties `coding-category' and `alias-coding-systems'
512 automatically.
513
514 Kludgy features for backward compatibility:
515
516 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
517 treated as a compiled CCL code.
518
519 2. If PROPERTIES is just a list of character sets, the list is set as
520 a value of `safe-charsets' in PLIST."
521 (if (memq coding-system coding-system-list)
522 (error "Coding system %s already exists" coding-system))
523
524 ;; Set a value of `coding-system' property.
525 (let ((coding-spec (make-vector 5 nil))
526 (no-initial-designation t)
527 (no-alternative-designation t)
528 coding-category)
529 (if (or (not (integerp type)) (< type 0) (> type 5))
530 (error "TYPE argument must be 0..5"))
531 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
532 (error "MNEMONIC arguemnt must be an ASCII printable character."))
533 (aset coding-spec coding-spec-type-idx type)
534 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
535 (aset coding-spec coding-spec-doc-string-idx
536 (if (stringp doc-string) doc-string ""))
537 (cond ((= type 0)
538 (setq coding-category 'coding-category-emacs-mule))
539 ((= type 1)
540 (setq coding-category 'coding-category-sjis))
541 ((= type 2) ; ISO2022
542 (let ((i 0)
543 (vec (make-vector 32 nil))
544 (g1-designation nil))
545 (while (< i 4)
546 (let ((charset (car flags)))
547 (if (and no-initial-designation
548 (> i 0)
549 (or (charsetp charset)
550 (and (consp charset)
551 (charsetp (car charset)))))
552 (setq no-initial-designation nil))
553 (if (charsetp charset)
554 (if (= i 1) (setq g1-designation charset))
555 (if (consp charset)
556 (let ((tail charset)
557 elt)
558 (while tail
559 (setq elt (car tail))
560 (if (eq elt t)
561 (setq no-alternative-designation nil)
562 (if (and elt (not (charsetp elt)))
563 (error "Invalid charset: %s" elt)))
564 (setq tail (cdr tail)))
565 (setq g1-designation (car charset)))
566 (if charset
567 (if (eq charset t)
568 (setq no-alternative-designation nil)
569 (error "Invalid charset: %s" charset)))))
570 (aset vec i charset))
571 (setq flags (cdr flags) i (1+ i)))
572 (while (and (< i 32) flags)
573 (aset vec i (car flags))
574 (setq flags (cdr flags) i (1+ i)))
575 (aset coding-spec 4 vec)
576 (setq coding-category
577 (if (aref vec 8) ; Use locking-shift.
578 (or (and (aref vec 7) 'coding-category-iso-7-else)
579 'coding-category-iso-8-else)
580 (if (aref vec 7) ; 7-bit only.
581 (if (aref vec 9) ; Use single-shift.
582 'coding-category-iso-7-else
583 (if no-alternative-designation
584 'coding-category-iso-7-tight
585 'coding-category-iso-7))
586 (if (or no-initial-designation
587 (not no-alternative-designation))
588 'coding-category-iso-8-else
589 (if (and (charsetp g1-designation)
590 (= (charset-dimension g1-designation) 2))
591 'coding-category-iso-8-2
592 'coding-category-iso-8-1)))))))
593 ((= type 3)
594 (setq coding-category 'coding-category-big5))
595 ((= type 4) ; private
596 (setq coding-category 'coding-category-binary)
597 (if (not (consp flags))
598 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
599 (let ((decoder (check-ccl-program
600 (car flags)
601 (intern (format "%s-decoder" coding-system))))
602 (encoder (check-ccl-program
603 (cdr flags)
604 (intern (format "%s-encoder" coding-system)))))
605 (if (and decoder encoder)
606 (aset coding-spec 4 (cons decoder encoder))
607 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
608 (t ; i.e. (= type 5)
609 (setq coding-category 'coding-category-raw-text)))
610
611 (let ((plist (list 'coding-category coding-category
612 'alias-coding-systems (list coding-system))))
613 (if no-initial-designation
614 (plist-put plist 'no-initial-designation t))
615 (if (and properties
616 (or (eq properties t)
617 (not (consp (car properties)))))
618 ;; In the old version, the arg PROPERTIES is a list to be
619 ;; set in PLIST as a value of property `safe-charsets'.
620 (plist-put plist 'safe-charsets properties)
621 (while properties
622 (plist-put plist (car (car properties)) (cdr (car properties)))
623 (setq properties (cdr properties))))
624 (aset coding-spec coding-spec-plist-idx plist))
625 (put coding-system 'coding-system coding-spec)
626 (put coding-category 'coding-systems
627 (cons coding-system (get coding-category 'coding-systems))))
628
629 ;; Next, set a value of `eol-type' property. The value is a vector
630 ;; of subsidiary coding systems, each corresponds to a coding system
631 ;; for the detected end-of-line format.
632 (put coding-system 'eol-type
633 (if (or (<= type 3) (= type 5))
634 (make-subsidiary-coding-system coding-system)
635 0))
636
637 ;; At last, register CODING-SYSTEM in `coding-system-list' and
638 ;; `coding-system-alist'.
639 (setq coding-system-list (cons coding-system coding-system-list))
640 (setq coding-system-alist (cons (list (symbol-name coding-system))
641 coding-system-alist))
642 coding-system)
643
644 (defun define-coding-system-alias (alias coding-system)
645 "Define ALIAS as an alias for coding system CODING-SYSTEM."
646 (put alias 'coding-system (coding-system-spec coding-system))
647 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
648 (setq coding-system-list (cons alias coding-system-list))
649 (setq coding-system-alist (cons (list (symbol-name alias))
650 coding-system-alist))
651 (let ((eol-type (coding-system-eol-type coding-system)))
652 (if (vectorp eol-type)
653 (put alias 'eol-type (make-subsidiary-coding-system alias))
654 (put alias 'eol-type eol-type))))
655
656 (defun set-buffer-file-coding-system (coding-system &optional force)
657 "Set the file coding-system of the current buffer to CODING-SYSTEM.
658 This means that when you save the buffer, it will be converted
659 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
660 use \\[list-coding-systems].
661
662 If the buffer's previous file coding-system value specifies end-of-line
663 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
664 merged with the already-specified end-of-line conversion.
665 However, if the optional prefix argument FORCE is non-nil,
666 then CODING-SYSTEM is used exactly as specified."
667 (interactive "zCoding system for visited file (default, nil): \nP")
668 (check-coding-system coding-system)
669 (if (null force)
670 (let ((x (coding-system-eol-type buffer-file-coding-system))
671 (y (coding-system-eol-type coding-system)))
672 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
673 (setq coding-system (aref y x)))))
674 (setq buffer-file-coding-system coding-system)
675 (set-buffer-modified-p t)
676 (force-mode-line-update))
677
678 (defvar default-terminal-coding-system nil
679 "Default value for the terminal coding system.
680 This is normally set according to the selected language environment.
681 See also the command `set-terminal-coding-system'.")
682
683 (defun set-terminal-coding-system (coding-system)
684 "Set coding system of your terminal to CODING-SYSTEM.
685 All text output to the terminal will be encoded
686 with the specified coding system.
687 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
688 The default is determined by the selected language environment
689 or by the previous use of this command."
690 (interactive
691 (list (let ((default (if (and (not (terminal-coding-system))
692 default-terminal-coding-system)
693 default-terminal-coding-system)))
694 (read-coding-system
695 (format "Coding system for terminal display (default, %s): "
696 default)
697 default))))
698 (if (and (not coding-system)
699 (not (terminal-coding-system)))
700 (setq coding-system default-terminal-coding-system))
701 (if coding-system
702 (setq default-terminal-coding-system coding-system))
703 (set-terminal-coding-system-internal coding-system)
704 (redraw-frame (selected-frame)))
705
706 (defvar default-keyboard-coding-system nil
707 "Default value of the keyboard coding system.
708 This is normally set according to the selected language environment.
709 See also the command `set-keyboard-coding-system'.")
710
711 (defun set-keyboard-coding-system (coding-system)
712 "Set coding system for keyboard input to CODING-SYSTEM.
713 In addition, this command enables Encoded-kbd minor mode.
714 \(If CODING-SYSTEM is nil, Encoded-bkd mode is turned off.)
715 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
716 The default is determined by the selected language environment
717 or by the previous use of this command."
718 (interactive
719 (list (let ((default (if (and (not (keyboard-coding-system))
720 default-keyboard-coding-system)
721 default-keyboard-coding-system)))
722 (read-coding-system
723 (format "Coding system for keyboard input (default, %s): "
724 default)
725 default))))
726 (if (and (not coding-system)
727 (not (keyboard-coding-system)))
728 (setq coding-system default-keyboard-coding-system))
729 (if coding-system
730 (setq default-keyboard-coding-system coding-system))
731 (set-keyboard-coding-system-internal coding-system)
732 (encoded-kbd-mode (if coding-system 1 0)))
733
734 (defun set-buffer-process-coding-system (decoding encoding)
735 "Set coding systems for the process associated with the current buffer.
736 DECODING is the coding system to be used to decode input from the process,
737 ENCODING is the coding system to be used to encode output to the process.
738
739 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
740 (interactive
741 "zCoding-system for process input: \nzCoding-system for process output: ")
742 (let ((proc (get-buffer-process (current-buffer))))
743 (if (null proc)
744 (error "no process")
745 (check-coding-system decoding)
746 (check-coding-system encoding)
747 (set-process-coding-system proc decoding encoding)))
748 (force-mode-line-update))
749
750 (defun set-clipboard-coding-system (coding-system)
751 "Make CODING-SYSTEM used for communicating with other X clients .
752 When sending or receiving text via cut_buffer, selection, and clipboard,
753 the text is encoded or decoded by CODING-SYSTEM."
754 (check-coding-system coding-system)
755 (setq clipboard-coding-system coding-system))
756
757 (defun set-coding-priority (arg)
758 "Set priority of coding categories according to LIST.
759 LIST is a list of coding categories ordered by priority."
760 (let ((l arg)
761 (current-list (copy-sequence coding-category-list)))
762 ;; Check the varidity of ARG while deleting coding categories in
763 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
764 ;; contains all coding categories.
765 (while l
766 (if (or (null (get (car l) 'coding-category-index))
767 (null (memq (car l) current-list)))
768 (error "Invalid or duplicated element in argument: %s" arg))
769 (setq current-list (delq (car l) current-list))
770 (setq l (cdr l)))
771 ;; Update `coding-category-list' and return it.
772 (setq coding-category-list (append arg current-list))
773 (set-coding-priority-internal)))
774
775 ;;; FILE I/O
776
777 (defun set-auto-coding (string)
778 "Return coding system for a file which has STRING at the head and tail.
779 STRING is a concatination of the first 1K-byte and
780 the last 3K-byte of the file.
781
782 It checks for a -*- coding: tag in the first one or two lines of STRING.
783 If there's no coding: tag in the head, it checks local variables spec
784 in the tailing 3K-byte oof STRING.
785
786 The return value is the specified coding system,
787 or nil if nothing specified.
788
789 The variable `set-auto-coding-function' (which see) is set to this
790 function by default."
791 (condition-case nil
792 (let ((case-fold-search t)
793 (len (length string))
794 (limit (string-match "\n" string))
795 (coding-system nil))
796
797 ;; At first check the head.
798 (if limit
799 (when (string-match "^#!" string)
800 ;; If the file begins with "#!" (exec interpreter
801 ;; magic), look for coding frobs in the first two lines.
802 ;; You cannot necessarily put them in the first line of
803 ;; such a file without screwing up the interpreter
804 ;; invocation.
805 (setq limit (string-match "\n" string limit))
806 (or limit
807 (setq limit len)))
808 (setq limit len))
809 (when (and (string-match "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" string)
810 (< (match-beginning 2) limit))
811 (setq coding-system
812 (intern (substring string (match-beginning 2) (match-end 2))))
813 (if (not (coding-system-p coding-system))
814 (setq coding-system nil)))
815
816 ;; If no coding system is specified in the head, check the tail.
817 (when (and (not coding-system)
818 (let ((idx (if (> len 3000) (- len 3000) 0))
819 start)
820 (while (setq start (string-match "\n\^L" string idx))
821 (setq idx (+ start 2)))
822 (string-match
823 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$"
824 string idx)))
825 ;; The prefix is what comes before "local variables:" in its line.
826 ;; The suffix is what comes after "local variables:" in its line.
827 (let* ((idx (1+ (match-end 0)))
828 (prefix (regexp-quote
829 (substring string
830 (match-beginning 1) (match-end 1))))
831 (suffix (regexp-quote
832 (substring string
833 (match-beginning 2) (match-end 2))))
834 (re-coding (concat "^" prefix
835 "coding[ \t]*:[ \t]*\\([^ \t\n]+\\)[ \t]*"
836 suffix "$"))
837 (re-end (concat "^" prefix "end *:[ \t]*" suffix "$"))
838 (limit (or (string-match re-end string idx) len)))
839 (when (and (setq idx (string-match re-coding string idx))
840 (< idx limit))
841 (setq coding-system
842 (intern (substring string
843 (match-beginning 1) (match-end 1))))
844 (or (coding-system-p coding-system)
845 (setq coding-system nil)))))
846
847 coding-system)
848 (error nil)))
849
850 (setq set-auto-coding-function 'set-auto-coding)
851
852 ;; Set buffer-file-coding-system of the current buffer after some text
853 ;; is inserted.
854 (defun after-insert-file-set-buffer-file-coding-system (inserted)
855 (if last-coding-system-used
856 (let ((coding-system
857 (find-new-buffer-file-coding-system last-coding-system-used))
858 (modified-p (buffer-modified-p)))
859 (when coding-system
860 (set-buffer-file-coding-system coding-system)
861 (if (and (or (eq coding-system 'no-conversion)
862 (eq (coding-system-type coding-system) 5))
863 ;; If buffer was unmodified, we must be visiting it.
864 (not modified-p))
865 ;; For coding systems no-conversion and raw-text...,
866 ;; edit the buffer as unibyte.
867 (set-buffer-multibyte nil))
868 (set-buffer-modified-p modified-p))))
869 nil)
870
871 (add-hook 'after-insert-file-functions
872 'after-insert-file-set-buffer-file-coding-system)
873
874 ;; The coding-spec and eol-type of coding-system returned is decided
875 ;; independently in the following order.
876 ;; 1. That of buffer-file-coding-system locally bound.
877 ;; 2. That of CODING.
878
879 (defun find-new-buffer-file-coding-system (coding)
880 "Return a coding system for a buffer when a file of CODING is inserted.
881 The local variable `buffer-file-coding-system' of the current buffer
882 is set to the returned value.
883 Return nil if there's no need of setting new buffer-file-coding-system."
884 (let (local-coding local-eol
885 found-coding found-eol
886 new-coding new-eol)
887 (if (null coding)
888 ;; Nothing found about coding.
889 nil
890
891 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
892 ;; and LOCAL-CODING.
893 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
894 (if (null (numberp local-eol))
895 ;; But eol-type is not yet set.
896 (setq local-eol nil))
897 (if (and buffer-file-coding-system
898 (not (eq (coding-system-type buffer-file-coding-system) t)))
899 ;; This is not `undecided'.
900 (setq local-coding (coding-system-base buffer-file-coding-system)))
901
902 (if (and (local-variable-p 'buffer-file-coding-system)
903 local-eol local-coding)
904 ;; The current buffer has already set full coding-system, we
905 ;; had better not change it.
906 nil
907
908 (setq found-eol (coding-system-eol-type coding))
909 (if (null (numberp found-eol))
910 ;; But eol-type is not found.
911 (setq found-eol nil))
912 (if (not (eq (coding-system-type coding) t))
913 ;; This is not `undecided'.
914 (setq found-coding (coding-system-base coding)))
915
916 ;; The local setting takes precedence over the found one.
917 (setq new-coding (or (and (local-variable-p 'buffer-file-coding-system)
918 local-coding)
919 found-coding
920 local-coding))
921 (setq new-eol (or (and (local-variable-p 'buffer-file-coding-system)
922 local-eol)
923 found-eol
924 local-eol))
925 (when (numberp new-eol)
926 (or new-coding
927 (setq new-coding 'undecided))
928 (if (vectorp (coding-system-eol-type new-coding))
929 (setq new-coding
930 (aref (coding-system-eol-type new-coding) new-eol))))
931 ;; Return a new coding system only when it is different from
932 ;; the current one.
933 (if (not (eq buffer-file-coding-system new-coding))
934 new-coding)))))
935
936 (defun modify-coding-system-alist (target-type regexp coding-system)
937 "Modify one of look up tables for finding a coding system on I/O operation.
938 There are three of such tables, `file-coding-system-alist',
939 `process-coding-system-alist', and `network-coding-system-alist'.
940
941 TARGET-TYPE specifies which of them to modify.
942 If it is `file', it affects `file-coding-system-alist' (which see).
943 If it is `process', it affects `process-coding-system-alist' (which see).
944 If it is `network', it affects `network-codign-system-alist' (which see).
945
946 REGEXP is a regular expression matching a target of I/O operation.
947 The target is a file name if TARGET-TYPE is `file', a program name if
948 TARGET-TYPE is `process', or a network service name or a port number
949 to connect to if TARGET-TYPE is `network'.
950
951 CODING-SYSTEM is a coding system to perform code conversion on the I/O
952 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
953 for decoding and encoding respectively,
954 or a function symbol which, when called, returns such a cons cell."
955 (or (memq target-type '(file process network))
956 (error "Invalid target type: %s" target-type))
957 (or (stringp regexp)
958 (and (eq target-type 'network) (integerp regexp))
959 (error "Invalid regular expression: %s" regexp))
960 (if (symbolp coding-system)
961 (if (not (fboundp coding-system))
962 (progn
963 (check-coding-system coding-system)
964 (setq coding-system (cons coding-system coding-system))))
965 (check-coding-system (car coding-system))
966 (check-coding-system (cdr coding-system)))
967 (cond ((eq target-type 'file)
968 (let ((slot (assoc regexp file-coding-system-alist)))
969 (if slot
970 (setcdr slot coding-system)
971 (setq file-coding-system-alist
972 (cons (cons regexp coding-system)
973 file-coding-system-alist)))))
974 ((eq target-type 'process)
975 (let ((slot (assoc regexp process-coding-system-alist)))
976 (if slot
977 (setcdr slot coding-system)
978 (setq process-coding-system-alist
979 (cons (cons regexp coding-system)
980 process-coding-system-alist)))))
981 (t
982 (let ((slot (assoc regexp network-coding-system-alist)))
983 (if slot
984 (setcdr slot coding-system)
985 (setq network-coding-system-alist
986 (cons (cons regexp coding-system)
987 network-coding-system-alist)))))))
988
989 (defun make-translation-table (&rest args)
990 "Make a translation table (char table) from arguments.
991 Each argument is a list of the form (FROM . TO),
992 where FROM is a character to be translated to TO.
993
994 FROM can be a generic character (see `make-char'). In this case, TO is
995 a generic character containing the same number of characters, or a
996 ordinary character. If FROM and TO are both generic characters, all
997 characters belonging to FROM are translated to characters belonging to TO
998 without changing their position code(s)."
999 (let ((table (make-char-table 'translation-table))
1000 revlist)
1001 (while args
1002 (let ((elts (car args)))
1003 (while elts
1004 (let* ((from (car (car elts)))
1005 (from-i 0) ; degree of freedom of FROM
1006 (from-rev (nreverse (split-char from)))
1007 (to (cdr (car elts)))
1008 (to-i 0) ; degree of freedom of TO
1009 (to-rev (nreverse (split-char to))))
1010 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1011 (while (eq (car from-rev) 0)
1012 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1013 (while (eq (car to-rev) 0)
1014 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1015 (if (and (/= from-i to-i) (/= to-i 0))
1016 (error "Invalid character pair (%d . %d)" from to))
1017 ;; If we have already translated TO to TO-ALT, FROM should
1018 ;; also be translated to TO-ALT. But, this is only if TO
1019 ;; is a generic character or TO-ALT is not a generic
1020 ;; character.
1021 (let ((to-alt (aref table to)))
1022 (if (and to-alt
1023 (or (> to-i 0) (not (generic-char-p to-alt))))
1024 (setq to to-alt)))
1025 (if (> from-i 0)
1026 (set-char-table-default table from to)
1027 (aset table from to))
1028 ;; If we have already translated some chars to FROM, they
1029 ;; should also be translated to TO.
1030 (let ((l (assq from revlist)))
1031 (if l
1032 (let ((ch (car l)))
1033 (setcar l to)
1034 (setq l (cdr l))
1035 (while l
1036 (aset table ch to)
1037 (setq l (cdr l)) ))))
1038 ;; Now update REVLIST.
1039 (let ((l (assq to revlist)))
1040 (if l
1041 (setcdr l (cons from (cdr l)))
1042 (setq revlist (cons (list to from) revlist)))))
1043 (setq elts (cdr elts))))
1044 (setq args (cdr args)))
1045 ;; Return TABLE just created.
1046 table))
1047
1048 (defun define-translation-table (symbol &rest args)
1049 "Define SYMBOL as a name of translation table makde by ARGS.
1050
1051 See the documentation of the function `make-translation-table' for the
1052 meaning of ARGS.
1053
1054 This function sets properties `translation-table' and
1055 `translation-table-id' of SYMBOL to the created table itself and
1056 identification number of the table respectively."
1057 (let ((table (apply 'make-translation-table args))
1058 (len (length translation-table-vector))
1059 (id 0)
1060 (done nil))
1061 (put symbol 'translation-table table)
1062 (while (not done)
1063 (if (>= id len)
1064 (setq translation-table-vector
1065 (vconcat translation-table-vector (make-vector len nil))))
1066 (let ((slot (aref translation-table-vector id)))
1067 (if (or (not slot)
1068 (eq (car slot) symbol))
1069 (progn
1070 (aset translation-table-vector id (cons symbol table))
1071 (setq done t))))
1072 (setq id (1+ id)))
1073 (put symbol 'translation-table-id id)
1074 id))
1075
1076 ;;; Initialize some variables.
1077
1078 (put 'use-default-ascent 'char-table-extra-slots 0)
1079 (setq use-default-ascent (make-char-table 'use-default-ascent))
1080 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1081 (setq ignore-relative-composition
1082 (make-char-table 'ignore-relative-composition))
1083
1084 ;;;
1085 (provide 'mule)
1086
1087 ;;; mule.el ends here