(load-with-code-conversion): Don't pass extra arg to eval-buffer.
[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 character-unification-table-for-decode
332 ;;
333 ;; The value is a unification table to be applied on decoding. See
334 ;; the function `make-unification-table' for the format of unification
335 ;; table.
336 ;;
337 ;; o character-unification-table-for-encode
338 ;;
339 ;; The value is a unification 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
351 ;; Return coding-spec of CODING-SYSTEM
352 (defsubst coding-system-spec (coding-system)
353 (get (check-coding-system coding-system) 'coding-system))
354
355 (defun coding-system-type (coding-system)
356 "Return the coding type of CODING-SYSTEM.
357 A coding type is an integer value indicating the encoding method
358 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
359 (aref (coding-system-spec coding-system) coding-spec-type-idx))
360
361 (defun coding-system-mnemonic (coding-system)
362 "Return the mnemonic character of CODING-SYSTEM.
363 A mnemonic character of a coding system is used in mode line
364 to indicate the coding system."
365 (or (aref (coding-system-spec coding-system) coding-spec-mnemonic-idx)
366 ?-))
367
368 (defun coding-system-doc-string (coding-system)
369 "Return the documentation string for CODING-SYSTEM."
370 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
371
372 (defun coding-system-plist (coding-system)
373 "Return the property list of CODING-SYSTEM."
374 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
375
376 (defun coding-system-flags (coding-system)
377 "Return `flags' of CODING-SYSTEM.
378 A `flags' of a coding system is a vector of length 32 indicating detailed
379 information of a coding system. See the function `make-coding-system'
380 for more detail."
381 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
382
383 (defun coding-system-get (coding-system prop)
384 "Extract a value from CODING-SYSTEM's property list for property PROP."
385 (plist-get (coding-system-plist coding-system) prop))
386
387 (defun coding-system-put (coding-system prop val)
388 "Change value in CODING-SYSTEM's property list PROP to VAL."
389 (let ((plist (coding-system-plist coding-system)))
390 (if plist
391 (plist-put plist prop val)
392 (aset (coding-system-spec coding-system) coding-spec-plist-idx
393 (list prop val)))))
394
395 (defun coding-system-category (coding-system)
396 "Return the coding category of CODING-SYSTEM."
397 (coding-system-get coding-system 'coding-category))
398
399 (defun coding-system-base (coding-system)
400 "Return the base coding system of CODING-SYSTEM.
401 A base coding system is what made by `make-coding-system'.
402 Any alias nor subsidiary coding systems are not base coding system."
403 (car (coding-system-get coding-system 'alias-coding-systems)))
404
405 (defalias 'coding-system-parent 'coding-system-base)
406 (make-obsolete 'coding-system-parent 'coding-system-base)
407
408 ;; Coding system also has a property `eol-type'.
409 ;;
410 ;; This property indicates how the coding system handles end-of-line
411 ;; format. The value is integer 0, 1, 2, or a vector of three coding
412 ;; systems. Each integer value 0, 1, and 2 indicates the format of
413 ;; end-of-line LF, CRLF, and CR respectively. A vector value
414 ;; indicates that the format of end-of-line should be detected
415 ;; automatically. Nth element of the vector is the subsidiary coding
416 ;; system whose `eol-type' property is N.
417
418 (defun coding-system-eol-type (coding-system)
419 "Return eol-type of CODING-SYSTEM.
420 An eol-type is integer 0, 1, 2, or a vector of coding systems.
421
422 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
423 CRLF, and CR respectively.
424
425 A vector value indicates that a format of end-of-line should be
426 detected automatically. Nth element of the vector is the subsidiary
427 coding system whose eol-type is N."
428 (get coding-system 'eol-type))
429
430 ;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
431 (defun make-subsidiary-coding-system (coding-system)
432 (let ((coding-spec (coding-system-spec coding-system))
433 (subsidiaries (vector (intern (format "%s-unix" coding-system))
434 (intern (format "%s-dos" coding-system))
435 (intern (format "%s-mac" coding-system))))
436 (i 0)
437 temp)
438 (while (< i 3)
439 (put (aref subsidiaries i) 'coding-system coding-spec)
440 (put (aref subsidiaries i) 'eol-type i)
441 (setq coding-system-list
442 (cons (aref subsidiaries i) coding-system-list))
443 (setq coding-system-alist
444 (cons (list (symbol-name (aref subsidiaries i)))
445 coding-system-alist))
446 (setq i (1+ i)))
447 subsidiaries))
448
449 (defun make-coding-system (coding-system type mnemonic doc-string
450 &optional flags properties)
451 "Define a new coding system CODING-SYSTEM (symbol).
452 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
453 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
454 in the following format:
455 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
456
457 TYPE is an integer value indicating the type of the coding system as follows:
458 0: Emacs internal format,
459 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
460 2: ISO-2022 including many variants,
461 3: Big5 used mainly on Chinese PC,
462 4: private, CCL programs provide encoding/decoding algorithm,
463 5: Raw-text, which means that text contains random 8-bit codes.
464
465 MNEMONIC is a character to be displayed on mode line for the coding system.
466
467 DOC-STRING is a documentation string for the coding system.
468
469 FLAGS specifies more detailed information of the coding system as follows:
470
471 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
472 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
473 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
474 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
475 SAFE, ACCEPT-LATIN-EXTRA-CODE.
476 CHARSETn are character sets initially designated to Gn graphic registers.
477 If CHARSETn is nil, Gn is never used.
478 If CHARSETn is t, Gn can be used but nothing designated initially.
479 If CHARSETn is a list of character sets, those character sets are
480 designated to Gn on output, but nothing designated to Gn initially.
481 SHORT-FORM non-nil means use short designation sequence on output.
482 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
483 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
484 SPACE on output.
485 SEVEN non-nil means use 7-bit code only on output.
486 LOCKING-SHIFT non-nil means use locking-shift.
487 SINGLE-SHIFT non-nil means use single-shift.
488 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
489 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
490 NO-ISO6429 non-nil means not use ISO6429's direction specification.
491 INIT-BOL non-nil means any designation state is assumed to be reset
492 to initial at each beginning of line on output.
493 DESIGNATION-BOL non-nil means designation sequences should be placed
494 at beginning of line on output.
495 SAFE non-nil means convert unsafe characters to `?' on output.
496 Unsafe characters are what not specified in SAFE-CHARSET.
497 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
498 a code specified in `latin-extra-code-table' (which see) as a valid
499 code of the coding system.
500
501 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
502 decoding and encoding. CCL programs should be specified by their
503 symbols.
504
505 PROPERTIES is an alist of properties vs the corresponding values.
506 These properties are set in PLIST, a property list. This function
507 also sets properties `coding-category' and `alias-coding-systems'
508 automatically.
509
510 Kludgy features for backward compatibility:
511
512 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
513 treated as a compiled CCL code.
514
515 2. If PROPERTIES is just a list of character sets, the list is set as
516 a value of `safe-charsets' in PLIST."
517 (if (memq coding-system coding-system-list)
518 (error "Coding system %s already exists" coding-system))
519
520 ;; Set a value of `coding-system' property.
521 (let ((coding-spec (make-vector 5 nil))
522 (no-initial-designation t)
523 (no-alternative-designation t)
524 coding-category)
525 (if (or (not (integerp type)) (< type 0) (> type 5))
526 (error "TYPE argument must be 0..5"))
527 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
528 (error "MNEMONIC arguemnt must be an ASCII printable character."))
529 (aset coding-spec coding-spec-type-idx type)
530 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
531 (aset coding-spec coding-spec-doc-string-idx
532 (if (stringp doc-string) doc-string ""))
533 (cond ((= type 0)
534 (setq coding-category 'coding-category-emacs-mule))
535 ((= type 1)
536 (setq coding-category 'coding-category-sjis))
537 ((= type 2) ; ISO2022
538 (let ((i 0)
539 (vec (make-vector 32 nil))
540 (g1-designation nil))
541 (while (< i 4)
542 (let ((charset (car flags)))
543 (if (and no-initial-designation
544 (> i 0)
545 (or (charsetp charset)
546 (and (consp charset)
547 (charsetp (car charset)))))
548 (setq no-initial-designation nil))
549 (if (charsetp charset)
550 (if (= i 1) (setq g1-designation charset))
551 (if (consp charset)
552 (let ((tail charset)
553 elt)
554 (while tail
555 (setq elt (car tail))
556 (if (eq elt t)
557 (setq no-alternative-designation nil)
558 (if (and elt (not (charsetp elt)))
559 (error "Invalid charset: %s" elt)))
560 (setq tail (cdr tail)))
561 (setq g1-designation (car charset)))
562 (if charset
563 (if (eq charset t)
564 (setq no-alternative-designation nil)
565 (error "Invalid charset: %s" charset)))))
566 (aset vec i charset))
567 (setq flags (cdr flags) i (1+ i)))
568 (while (and (< i 32) flags)
569 (aset vec i (car flags))
570 (setq flags (cdr flags) i (1+ i)))
571 (aset coding-spec 4 vec)
572 (setq coding-category
573 (if (aref vec 8) ; Use locking-shift.
574 (or (and (aref vec 7) 'coding-category-iso-7-else)
575 'coding-category-iso-8-else)
576 (if (aref vec 7) ; 7-bit only.
577 (if (aref vec 9) ; Use single-shift.
578 'coding-category-iso-7-else
579 (if no-alternative-designation
580 'coding-category-iso-7-tight
581 'coding-category-iso-7))
582 (if no-initial-designation
583 'coding-category-iso-8-else
584 (if (and (charsetp g1-designation)
585 (= (charset-dimension g1-designation) 2))
586 'coding-category-iso-8-2
587 'coding-category-iso-8-1)))))))
588 ((= type 3)
589 (setq coding-category 'coding-category-big5))
590 ((= type 4) ; private
591 (setq coding-category 'coding-category-binary)
592 (if (not (consp flags))
593 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
594 (let ((decoder (check-ccl-program
595 (car flags)
596 (intern (format "%s-decoder" coding-system))))
597 (encoder (check-ccl-program
598 (cdr flags)
599 (intern (format "%s-encoder" coding-system)))))
600 (if (and decoder encoder)
601 (aset coding-spec 4 (cons decoder encoder))
602 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
603 (t ; i.e. (= type 5)
604 (setq coding-category 'coding-category-raw-text)))
605
606 (let ((plist (list 'coding-category coding-category
607 'alias-coding-systems (list coding-system))))
608 (if no-initial-designation
609 (plist-put plist 'no-initial-designation t))
610 (if (and properties
611 (or (eq properties t)
612 (not (consp (car properties)))))
613 ;; In the old version, the arg PROPERTIES is a list to be
614 ;; set in PLIST as a value of property `safe-charsets'.
615 (plist-put plist 'safe-charsets properties)
616 (while properties
617 (plist-put plist (car (car properties)) (cdr (car properties)))
618 (setq properties (cdr properties))))
619 (aset coding-spec coding-spec-plist-idx plist))
620 (put coding-system 'coding-system coding-spec)
621 (put coding-category 'coding-systems
622 (cons coding-system (get coding-category 'coding-systems))))
623
624 ;; Next, set a value of `eol-type' property. The value is a vector
625 ;; of subsidiary coding systems, each corresponds to a coding system
626 ;; for the detected end-of-line format.
627 (put coding-system 'eol-type
628 (if (or (<= type 3) (= type 5))
629 (make-subsidiary-coding-system coding-system)
630 0))
631
632 ;; At last, register CODING-SYSTEM in `coding-system-list' and
633 ;; `coding-system-alist'.
634 (setq coding-system-list (cons coding-system coding-system-list))
635 (setq coding-system-alist (cons (list (symbol-name coding-system))
636 coding-system-alist))
637 coding-system)
638
639 (defun define-coding-system-alias (alias coding-system)
640 "Define ALIAS as an alias for coding system CODING-SYSTEM."
641 (put alias 'coding-system (coding-system-spec coding-system))
642 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
643 (setq coding-system-list (cons alias coding-system-list))
644 (setq coding-system-alist (cons (list (symbol-name alias))
645 coding-system-alist))
646 (let ((eol-type (coding-system-eol-type coding-system)))
647 (if (vectorp eol-type)
648 (put alias 'eol-type (make-subsidiary-coding-system alias))
649 (put alias 'eol-type eol-type))))
650
651 (defun set-buffer-file-coding-system (coding-system &optional force)
652 "Set the file coding-system of the current buffer to CODING-SYSTEM.
653 This means that when you save the buffer, it will be converted
654 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
655 use \\[list-coding-systems].
656
657 If the buffer's previous file coding-system value specifies end-of-line
658 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
659 merged with the already-specified end-of-line conversion.
660 However, if the optional prefix argument FORCE is non-nil,
661 then CODING-SYSTEM is used exactly as specified."
662 (interactive "zCoding system for visited file (default, nil): \nP")
663 (check-coding-system coding-system)
664 (if (null force)
665 (let ((x (coding-system-eol-type buffer-file-coding-system))
666 (y (coding-system-eol-type coding-system)))
667 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
668 (setq coding-system (aref y x)))))
669 (setq buffer-file-coding-system coding-system)
670 (set-buffer-modified-p t)
671 (force-mode-line-update))
672
673 (defvar default-terminal-coding-system nil
674 "Default value for the terminal coding system.
675 This is normally set according to the selected language environment.
676 See also the command `set-terminal-coding-system'.")
677
678 (defun set-terminal-coding-system (coding-system)
679 "Set coding system of your terminal to CODING-SYSTEM.
680 All text output to the terminal will be encoded
681 with the specified coding system.
682 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
683 The default is determined by the selected language environment
684 or by the previous use of this command."
685 (interactive
686 (list (let ((default (if (and (not (terminal-coding-system))
687 default-terminal-coding-system)
688 default-terminal-coding-system)))
689 (read-coding-system
690 (format "Coding system for terminal display (default, %s): "
691 default)
692 default))))
693 (if (and (not coding-system)
694 (not (terminal-coding-system)))
695 (setq coding-system default-terminal-coding-system))
696 (if coding-system
697 (setq default-terminal-coding-system coding-system))
698 (set-terminal-coding-system-internal coding-system)
699 (redraw-frame (selected-frame)))
700
701 (defvar default-keyboard-coding-system nil
702 "Default value of the keyboard coding system.
703 This is normally set according to the selected language environment.
704 See also the command `set-keyboard-coding-system'.")
705
706 (defun set-keyboard-coding-system (coding-system)
707 "Set coding system for keyboard input to CODING-SYSTEM.
708 In addition, this command enables Encoded-kbd minor mode.
709 \(If CODING-SYSTEM is nil, Encoded-bkd mode is turned off.)
710 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
711 The default is determined by the selected language environment
712 or by the previous use of this command."
713 (interactive
714 (list (let ((default (if (and (not (keyboard-coding-system))
715 default-keyboard-coding-system)
716 default-keyboard-coding-system)))
717 (read-coding-system
718 (format "Coding system for keyboard input (default, %s): "
719 default)
720 default))))
721 (if (and (not coding-system)
722 (not (keyboard-coding-system)))
723 (setq coding-system default-keyboard-coding-system))
724 (if coding-system
725 (setq default-keyboard-coding-system coding-system))
726 (set-keyboard-coding-system-internal coding-system)
727 (encoded-kbd-mode (if coding-system 1 0)))
728
729 (defun set-buffer-process-coding-system (decoding encoding)
730 "Set coding systems for the process associated with the current buffer.
731 DECODING is the coding system to be used to decode input from the process,
732 ENCODING is the coding system to be used to encode output to the process.
733
734 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
735 (interactive
736 "zCoding-system for process input: \nzCoding-system for process output: ")
737 (let ((proc (get-buffer-process (current-buffer))))
738 (if (null proc)
739 (error "no process")
740 (check-coding-system decoding)
741 (check-coding-system encoding)
742 (set-process-coding-system proc decoding encoding)))
743 (force-mode-line-update))
744
745 (defun set-coding-priority (arg)
746 "Set priority of coding categories according to LIST.
747 LIST is a list of coding categories ordered by priority."
748 (let ((l arg)
749 (current-list (copy-sequence coding-category-list)))
750 ;; Check the varidity of ARG while deleting coding categories in
751 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
752 ;; contains all coding categories.
753 (while l
754 (if (or (null (get (car l) 'coding-category-index))
755 (null (memq (car l) current-list)))
756 (error "Invalid or duplicated element in argument: %s" arg))
757 (setq current-list (delq (car l) current-list))
758 (setq l (cdr l)))
759 ;; Update `coding-category-list' and return it.
760 (setq coding-category-list (append arg current-list))))
761
762 ;;; FILE I/O
763
764 (defun set-auto-coding (string)
765 "Return coding system for a file which has STRING at the head and tail.
766 STRING is a concatination of the first 1K-byte and
767 the last 3K-byte of the file.
768
769 It checks for a -*- coding: tag in the first one or two lines of STRING.
770 If there's no coding: tag in the head, it checks local variables spec
771 in the tailing 3K-byte oof STRING.
772
773 The return value is the specified coding system,
774 or nil if nothing specified.
775
776 The variable `set-auto-coding-function' (which see) is set to this
777 function by default."
778 (condition-case nil
779 (let ((case-fold-search t)
780 (len (length string))
781 (limit (string-match "\n" string))
782 (coding-system nil))
783
784 ;; At first check the head.
785 (if limit
786 (when (string-match "^#!" string)
787 ;; If the file begins with "#!" (exec interpreter
788 ;; magic), look for coding frobs in the first two lines.
789 ;; You cannot necessarily put them in the first line of
790 ;; such a file without screwing up the interpreter
791 ;; invocation.
792 (setq limit (string-match "\n" string limit))
793 (or limit
794 (setq limit len)))
795 (setq limit len))
796 (when (and (string-match "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" string)
797 (< (match-beginning 2) limit))
798 (setq coding-system
799 (intern (substring string (match-beginning 2) (match-end 2))))
800 (if (not (coding-system-p coding-system))
801 (setq coding-system nil)))
802
803 ;; If no coding system is specified in the head, check the tail.
804 (when (and (not coding-system)
805 (let ((idx (if (> len 3000) (- len 3000) 0))
806 start)
807 (while (setq start (string-match "\n\^L" string idx))
808 (setq idx (+ start 2)))
809 (string-match
810 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$"
811 string idx)))
812 ;; The prefix is what comes before "local variables:" in its line.
813 ;; The suffix is what comes after "local variables:" in its line.
814 (let* ((idx (1+ (match-end 0)))
815 (prefix (regexp-quote
816 (substring string
817 (match-beginning 1) (match-end 1))))
818 (suffix (regexp-quote
819 (substring string
820 (match-beginning 2) (match-end 2))))
821 (re-coding (concat "^" prefix
822 "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
823 suffix "$"))
824 (re-end (concat "^" prefix "end *:[ \t]*" suffix "$"))
825 (limit (or (string-match re-end string idx) len)))
826 (when (and (setq idx (string-match re-coding string idx))
827 (< idx limit))
828 (setq coding-system
829 (intern (substring string
830 (match-beginning 1) (match-end 1))))
831 (or (coding-system-p coding-system)
832 (setq coding-system nil)))))
833
834 coding-system)
835 (error nil)))
836
837 (setq set-auto-coding-function 'set-auto-coding)
838
839 ;; Set buffer-file-coding-system of the current buffer after some text
840 ;; is inserted.
841 (defun after-insert-file-set-buffer-file-coding-system (inserted)
842 (if last-coding-system-used
843 (let ((coding-system
844 (find-new-buffer-file-coding-system last-coding-system-used))
845 (modified-p (buffer-modified-p)))
846 (when coding-system
847 (set-buffer-file-coding-system coding-system)
848 (if (or (eq coding-system 'no-conversion)
849 (eq (coding-system-type coding-system) 5))
850 ;; It seems that random 8-bit codes are read. We had
851 ;; better edit this buffer without multibyte characters.
852 (set-buffer-multibyte nil))
853 (set-buffer-modified-p modified-p))))
854 nil)
855
856 (add-hook 'after-insert-file-functions
857 'after-insert-file-set-buffer-file-coding-system)
858
859 ;; The coding-spec and eol-type of coding-system returned is decided
860 ;; independently in the following order.
861 ;; 1. That of buffer-file-coding-system locally bound.
862 ;; 2. That of CODING.
863
864 (defun find-new-buffer-file-coding-system (coding)
865 "Return a coding system for a buffer when a file of CODING is inserted.
866 The local variable `buffer-file-coding-system' of the current buffer
867 is set to the returned value.
868 Return nil if there's no need of setting new buffer-file-coding-system."
869 (let (local-coding local-eol
870 found-coding found-eol
871 new-coding new-eol)
872 (if (null coding)
873 ;; Nothing found about coding.
874 nil
875
876 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
877 ;; and LOCAL-CODING.
878 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
879 (if (null (numberp local-eol))
880 ;; But eol-type is not yet set.
881 (setq local-eol nil))
882 (if (and buffer-file-coding-system
883 (not (eq (coding-system-type buffer-file-coding-system) t)))
884 ;; This is not `undecided'.
885 (setq local-coding (coding-system-base buffer-file-coding-system)))
886
887 (if (and (local-variable-p 'buffer-file-coding-system)
888 local-eol local-coding)
889 ;; The current buffer has already set full coding-system, we
890 ;; had better not change it.
891 nil
892
893 (setq found-eol (coding-system-eol-type coding))
894 (if (null (numberp found-eol))
895 ;; But eol-type is not found.
896 (setq found-eol nil))
897 (if (not (eq (coding-system-type coding) t))
898 ;; This is not `undecided'.
899 (setq found-coding (coding-system-base coding)))
900
901 ;; The local setting takes precedence over the found one.
902 (setq new-coding (or (and (local-variable-p 'buffer-file-coding-system)
903 local-coding)
904 found-coding
905 local-coding))
906 (setq new-eol (or (and (local-variable-p 'buffer-file-coding-system)
907 local-eol)
908 found-eol
909 local-eol))
910 (when (numberp new-eol)
911 (or new-coding
912 (setq new-coding 'undecided))
913 (if (vectorp (coding-system-eol-type new-coding))
914 (setq new-coding
915 (aref (coding-system-eol-type new-coding) new-eol))))
916 ;; Return a new coding system only when it is different from
917 ;; the current one.
918 (if (not (eq buffer-file-coding-system new-coding))
919 new-coding)))))
920
921 (defun modify-coding-system-alist (target-type regexp coding-system)
922 "Modify one of look up tables for finding a coding system on I/O operation.
923 There are three of such tables, `file-coding-system-alist',
924 `process-coding-system-alist', and `network-coding-system-alist'.
925
926 TARGET-TYPE specifies which of them to modify.
927 If it is `file', it affects `file-coding-system-alist' (which see).
928 If it is `process', it affects `process-coding-system-alist' (which see).
929 If it is `network', it affects `network-codign-system-alist' (which see).
930
931 REGEXP is a regular expression matching a target of I/O operation.
932 The target is a file name if TARGET-TYPE is `file', a program name if
933 TARGET-TYPE is `process', or a network service name or a port number
934 to connect to if TARGET-TYPE is `network'.
935
936 CODING-SYSTEM is a coding system to perform code conversion on the I/O
937 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
938 for decoding and encoding respectively,
939 or a function symbol which, when called, returns such a cons cell."
940 (or (memq target-type '(file process network))
941 (error "Invalid target type: %s" target-type))
942 (or (stringp regexp)
943 (and (eq target-type 'network) (integerp regexp))
944 (error "Invalid regular expression: %s" regexp))
945 (if (symbolp coding-system)
946 (if (not (fboundp coding-system))
947 (progn
948 (check-coding-system coding-system)
949 (setq coding-system (cons coding-system coding-system))))
950 (check-coding-system (car coding-system))
951 (check-coding-system (cdr coding-system)))
952 (cond ((eq target-type 'file)
953 (let ((slot (assoc regexp file-coding-system-alist)))
954 (if slot
955 (setcdr slot coding-system)
956 (setq file-coding-system-alist
957 (cons (cons regexp coding-system)
958 file-coding-system-alist)))))
959 ((eq target-type 'process)
960 (let ((slot (assoc regexp process-coding-system-alist)))
961 (if slot
962 (setcdr slot coding-system)
963 (setq process-coding-system-alist
964 (cons (cons regexp coding-system)
965 process-coding-system-alist)))))
966 (t
967 (let ((slot (assoc regexp network-coding-system-alist)))
968 (if slot
969 (setcdr slot coding-system)
970 (setq network-coding-system-alist
971 (cons (cons regexp coding-system)
972 network-coding-system-alist)))))))
973
974 (defun make-unification-table (&rest args)
975 "Make a unification table (char table) from arguments.
976 Each argument is a list of the form (FROM . TO),
977 where FROM is a character to be unified to TO.
978
979 FROM can be a generic character (see make-char). In this case, TO is
980 a generic character containing the same number of charcters or a
981 oridinal character. If FROM and TO are both generic characters, all
982 characters belonging to FROM are unified to characters belonging to TO
983 without changing their position code(s)."
984 (let ((table (make-char-table 'character-unification-table))
985 revlist)
986 (while args
987 (let ((elts (car args)))
988 (while elts
989 (let* ((from (car (car elts)))
990 (from-i 0) ; degree of freedom of FROM
991 (from-rev (nreverse (split-char from)))
992 (to (cdr (car elts)))
993 (to-i 0) ; degree of freedom of TO
994 (to-rev (nreverse (split-char to))))
995 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
996 (while (eq (car from-rev) 0)
997 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
998 (while (eq (car to-rev) 0)
999 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1000 (if (and (/= from-i to-i) (/= to-i 0))
1001 (error "Invalid character pair (%d . %d)" from to))
1002 ;; If we have already unified TO to TO-ALT, FROM should
1003 ;; also be unified to TO-ALT. But, this is only if TO is
1004 ;; a generic character or TO-ALT is not a generic
1005 ;; character.
1006 (let ((to-alt (aref table to)))
1007 (if (and to-alt
1008 (or (> to-i 0) (not (generic-char-p to-alt))))
1009 (setq to to-alt)))
1010 (if (> from-i 0)
1011 (set-char-table-default table from to)
1012 (aset table from to))
1013 ;; If we have already unified some chars to FROM, they
1014 ;; should also be unified to TO.
1015 (let ((l (assq from revlist)))
1016 (if l
1017 (let ((ch (car l)))
1018 (setcar l to)
1019 (setq l (cdr l))
1020 (while l
1021 (aset table ch to)
1022 (setq l (cdr l)) ))))
1023 ;; Now update REVLIST.
1024 (let ((l (assq to revlist)))
1025 (if l
1026 (setcdr l (cons from (cdr l)))
1027 (setq revlist (cons (list to from) revlist)))))
1028 (setq elts (cdr elts))))
1029 (setq args (cdr args)))
1030 ;; Return TABLE just created.
1031 table))
1032
1033 (defun define-character-unification-table (symbol &rest args)
1034 "define character unification table. This function call make-unification-table,
1035 store a returned table to character-unification-table-vector.
1036 And then set the table as SYMBOL's unification-table property,
1037 the index of the vector as SYMBOL's unification-table-id."
1038 (let ((table (apply 'make-unification-table args))
1039 (len (length character-unification-table-vector))
1040 (id 0)
1041 slot)
1042 (or (symbolp symbol)
1043 (signal 'wrong-type-argument symbol))
1044 (put symbol 'unification-table table)
1045 (while (and (< id len)
1046 (if (consp (setq slot (aref character-unification-table-vector id)))
1047 (if (eq (car slot) symbol) nil t)
1048 (aset character-unification-table-vector id (cons symbol table))
1049 nil))
1050 (setq id (1+ id)))
1051 (if (= id len)
1052 (progn
1053 (setq character-unification-table-vector
1054 (vconcat character-unification-table-vector (make-vector len nil)))
1055 (aset character-unification-table-vector id (cons symbol table))))
1056 (put symbol 'unification-table-id id)
1057 id))
1058
1059
1060 ;;; Initialize some variables.
1061
1062 (put 'use-default-ascent 'char-table-extra-slots 0)
1063 (setq use-default-ascent (make-char-table 'use-default-ascent))
1064 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1065 (setq ignore-relative-composition
1066 (make-char-table 'ignore-relative-composition))
1067
1068 ;;;
1069 (provide 'mule)
1070
1071 ;;; mule.el ends here