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