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