]> code.delx.au - gnu-emacs/blob - lisp/international/mule.el
(quail-install-map): New optional arg
[gnu-emacs] / 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 "5.0 (AOI)" "\
28 Version number and name of this version of MULE (multilingual environment).")
29
30 (defconst mule-version-date "1999.12.7" "\
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 (set-auto-coding-for-load t)
68 (inhibit-file-name-operation nil))
69 (save-excursion
70 (set-buffer buffer)
71 (insert-file-contents fullname)
72 ;; If the loaded file was inserted with no-conversion or
73 ;; raw-text coding system, make the buffer unibyte.
74 ;; Otherwise, eval-buffer might try to interpret random
75 ;; binary junk as multibyte characters.
76 (if (and enable-multibyte-characters
77 (or (eq (coding-system-type last-coding-system-used) 5)
78 (eq last-coding-system-used 'no-conversion)))
79 (set-buffer-multibyte nil))
80 ;; Make `kill-buffer' quiet.
81 (set-buffer-modified-p nil))
82 ;; Have the original buffer current while we eval.
83 (eval-buffer buffer nil file
84 ;; If this Emacs is running with --unibyte,
85 ;; convert multibyte strings to unibyte
86 ;; after reading them.
87 ;; (not default-enable-multibyte-characters)
88 nil t
89 ))
90 (let (kill-buffer-hook kill-buffer-query-functions)
91 (kill-buffer buffer)))
92 (let ((hook (assoc file after-load-alist)))
93 (when hook
94 (mapcar (function eval) (cdr hook))))
95 (unless (or nomessage noninteractive)
96 (if source
97 (message "Loading %s (source)...done" file)
98 (message "Loading %s...done" file)))
99 t)))
100
101 ;; API (Application Program Interface) for charsets.
102
103 ;; Return t if OBJ is a quoted symbol
104 ;; and the symbol is the name of a standard charset.
105 (defsubst charset-quoted-standard-p (obj)
106 (and (listp obj) (eq (car obj) 'quote)
107 (symbolp (car-safe (cdr obj)))
108 (let ((vector (get (car-safe (cdr obj)) 'charset)))
109 (and (vectorp vector)
110 (< (aref vector 0) 160)))))
111
112 (defsubst charsetp (object)
113 "T if OBJECT is a charset."
114 (and (symbolp object) (vectorp (get object 'charset))))
115
116 (defsubst charset-info (charset)
117 "Return a vector of information of CHARSET.
118 The elements of the vector are:
119 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
120 LEADING-CODE-BASE, LEADING-CODE-EXT,
121 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
122 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
123 PLIST,
124 where
125 CHARSET-ID (integer) is the identification number of the charset.
126 BYTES (integer) is the length of multi-byte form of a character in
127 the charset: one of 1, 2, 3, and 4.
128 DIMENSION (integer) is the number of bytes to represent a character of
129 the charset: 1 or 2.
130 CHARS (integer) is the number of characters in a dimension: 94 or 96.
131 WIDTH (integer) is the number of columns a character in the charset
132 occupies on the screen: one of 0, 1, and 2.
133 DIRECTION (integer) is the rendering direction of characters in the
134 charset when rendering. If 0, render from left to right, else
135 render from right to left.
136 LEADING-CODE-BASE (integer) is the base leading-code for the
137 charset.
138 LEADING-CODE-EXT (integer) is the extended leading-code for the
139 charset. All charsets of less than 0xA0 has the value 0.
140 ISO-FINAL-CHAR (character) is the final character of the
141 corresponding ISO 2022 charset.
142 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
143 while encoding to variants of ISO 2022 coding system, one of the
144 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
145 REVERSE-CHARSET (integer) is the charset which differs only in
146 LEFT-TO-RIGHT value from the charset. If there's no such a
147 charset, the value is -1.
148 SHORT-NAME (string) is the short name to refer to the charset.
149 LONG-NAME (string) is the long name to refer to the charset
150 DESCRIPTION (string) is the description string of the charset.
151 PLIST (property list) may contain any type of information a user
152 want to put and get by functions `put-charset-property' and
153 `get-charset-property' respectively."
154 (get charset 'charset))
155
156 ;; It is better not to use backquote in this file,
157 ;; because that makes a bootstrapping problem
158 ;; if you need to recompile all the Lisp files using interpreted code.
159
160 (defmacro charset-id (charset)
161 "Return charset identification number of CHARSET."
162 (if (charset-quoted-standard-p charset)
163 (aref (charset-info (nth 1 charset)) 0)
164 (list 'aref (list 'charset-info charset) 0)))
165
166 (defmacro charset-bytes (charset)
167 "Return bytes 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)) 1)
171 (list 'aref (list 'charset-info charset) 1)))
172
173 (defmacro charset-dimension (charset)
174 "Return dimension 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)) 2)
178 (list 'aref (list 'charset-info charset) 2)))
179
180 (defmacro charset-chars (charset)
181 "Return character numbers contained in a dimension 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)) 3)
185 (list 'aref (list 'charset-info charset) 3)))
186
187 (defmacro charset-width (charset)
188 "Return width (how many column occupied on a screen) 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)) 4)
192 (list 'aref (list 'charset-info charset) 4)))
193
194 (defmacro charset-direction (charset)
195 "Return direction 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)) 5)
199 (list 'aref (list 'charset-info charset) 5)))
200
201 (defmacro charset-iso-final-char (charset)
202 "Return final char 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)) 8)
206 (list 'aref (list 'charset-info charset) 8)))
207
208 (defmacro charset-iso-graphic-plane (charset)
209 "Return graphic plane 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)) 9)
213 (list 'aref (list 'charset-info charset) 9)))
214
215 (defmacro charset-reverse-charset (charset)
216 "Return reverse charset 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)) 10)
220 (list 'aref (list 'charset-info charset) 10)))
221
222 (defmacro charset-short-name (charset)
223 "Return short name 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)) 11)
227 (list 'aref (list 'charset-info charset) 11)))
228
229 (defmacro charset-long-name (charset)
230 "Return long name 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)) 12)
234 (list 'aref (list 'charset-info charset) 12)))
235
236 (defmacro charset-description (charset)
237 "Return description of CHARSET.
238 See the function `charset-info' for more detail."
239 (if (charset-quoted-standard-p charset)
240 (aref (charset-info (nth 1 charset)) 13)
241 (list 'aref (list 'charset-info charset) 13)))
242
243 (defmacro charset-plist (charset)
244 "Return list charset property of CHARSET.
245 See the function `charset-info' for more detail."
246 (list 'aref
247 (if (charset-quoted-standard-p charset)
248 (charset-info (nth 1 charset))
249 (list 'charset-info charset))
250 14))
251
252 (defun set-charset-plist (charset plist)
253 "Set CHARSET's property list to PLIST, and return PLIST."
254 (aset (charset-info charset) 14 plist))
255
256 (defun make-char (charset &optional c1 c2)
257 "Return a character of CHARSET and position codes CODE1 and CODE2.
258 CODE1 and CODE2 are optional, but if you don't supply
259 sufficient position codes, return a generic character which stands for
260 all characters or group of characters in the character set.
261 A generic character can be used to index a char table (e.g. syntax-table)."
262 (make-char-internal (charset-id charset) c1 c2))
263
264 (put 'make-char 'byte-compile
265 (function
266 (lambda (form)
267 (let ((charset (nth 1 form)))
268 (if (charset-quoted-standard-p charset)
269 (byte-compile-normal-call
270 (cons 'make-char-internal
271 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
272 (byte-compile-normal-call
273 (cons 'make-char-internal
274 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
275
276 (defun charset-list ()
277 "Return list of charsets ever defined.
278
279 This function is provided for backward compatibility.
280 Now we have the variable `charset-list'."
281 charset-list)
282
283 (defsubst generic-char-p (char)
284 "Return t if and only if CHAR is a generic character.
285 See also the documentation of make-char."
286 (and (>= char 0400)
287 (let ((l (split-char char)))
288 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
289 (not (eq (car l) 'composition))))))
290
291 \f
292 ;; Coding system staffs
293
294 ;; Coding system is a symbol that has the property `coding-system'.
295 ;;
296 ;; The value of the property `coding-system' is a vector of the
297 ;; following format:
298 ;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
299 ;; We call this vector as coding-spec. See comments in src/coding.c
300 ;; for more detail.
301
302 (defconst coding-spec-type-idx 0)
303 (defconst coding-spec-mnemonic-idx 1)
304 (defconst coding-spec-doc-string-idx 2)
305 (defconst coding-spec-plist-idx 3)
306 (defconst coding-spec-flags-idx 4)
307
308 ;; PLIST is a property list of a coding system. To share PLIST among
309 ;; alias coding systems, a coding system has PLIST in coding-spec
310 ;; instead of having it in normal property list of Lisp symbol.
311 ;; Here's a list of coding system properties currently being used.
312 ;;
313 ;; o coding-category
314 ;;
315 ;; The value is a coding category the coding system belongs to. The
316 ;; function `make-coding-system' and `define-coding-system-alias' sets
317 ;; this value automatically.
318 ;;
319 ;; o alias-coding-systems
320 ;;
321 ;; The value is a list of coding systems of the same alias group. The
322 ;; first element is the coding system made at first, which we call as
323 ;; `base coding system'. The function `make-coding-system' and
324 ;; `define-coding-system-alias' set this value automatically.
325 ;;
326 ;; o post-read-conversion
327 ;;
328 ;; The value is a function to call after some text is inserted and
329 ;; decoded by the coding system itself and before any functions in
330 ;; `after-insert-functions' are called. The arguments to this
331 ;; function is the same as those of a function in
332 ;; `after-insert-functions', i.e. LENGTH of a text while putting point
333 ;; at the head of the text to be decoded
334 ;;
335 ;; o pre-write-conversion
336 ;;
337 ;; The value is a function to call after all functions in
338 ;; `write-region-annotate-functions' and `buffer-file-format' are
339 ;; called, and before the text is encoded by the coding system itself.
340 ;; The arguments to this function is the same as those of a function
341 ;; in `write-region-annotate-functions', i.e. FROM and TO specifying
342 ;; region of a text.
343 ;;
344 ;; o translation-table-for-decode
345 ;;
346 ;; The value is a translation table to be applied on decoding. See
347 ;; the function `make-translation-table' for the format of translation
348 ;; table.
349 ;;
350 ;; o translation-table-for-encode
351 ;;
352 ;; The value is a translation table to be applied on encoding.
353 ;;
354 ;; o safe-charsets
355 ;;
356 ;; The value is a list of charsets safely supported by the coding
357 ;; system. The value t means that all charsets Emacs handles are
358 ;; supported. Even if some charset is not in this list, it doesn't
359 ;; mean that the charset can't be encoded in the coding system,
360 ;; instead, it just means that some other receiver of a text encoded
361 ;; in the coding system won't be able to handle that charset.
362 ;;
363 ;; o mime-charset
364 ;;
365 ;; The value is a symbol of which name is `MIME-charset' parameter of
366 ;; the coding system.
367 ;;
368 ;; o charset-origin-alist
369 ;;
370 ;; The value is a list of this form:
371 ;; (CHARSET EXTERNAL-CHARSET-NAME ENCODING-FUNCTION).
372 ;; ENCODING-FUNCTION is a function to encode a character in CHARSET
373 ;; to the code in EXTERNAL-CHARSET-NAME. The command what-cursor-position
374 ;; uses this information of the buffer-file-coding-system.
375 ;; ENCODING-FUNCTION may be a translation table or a symbol whose
376 ;; property `translation-table' is a translation table. In these case,
377 ;; the translation table is used to encode the character.
378 ;;
379 ;; o valid-codes (meaningful only for a coding system based on CCL)
380 ;;
381 ;; The value is a list to indicate valid byte ranges of the encoded
382 ;; file. Each element of the list is an integer or a cons of integer.
383 ;; In the former case, the integer value is a valid byte code. In the
384 ;; latter case, the integers specifies the range of valid byte codes.
385
386
387 ;; Return coding-spec of CODING-SYSTEM
388 (defsubst coding-system-spec (coding-system)
389 (get (check-coding-system coding-system) 'coding-system))
390
391 (defun coding-system-type (coding-system)
392 "Return the coding type of CODING-SYSTEM.
393 A coding type is an integer value indicating the encoding method
394 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
395 (aref (coding-system-spec coding-system) coding-spec-type-idx))
396
397 (defun coding-system-mnemonic (coding-system)
398 "Return the mnemonic character of CODING-SYSTEM.
399 The mnemonic character of a coding system is used in mode line
400 to indicate the coding system. If the arg is nil, return ?-."
401 (let ((spec (coding-system-spec coding-system)))
402 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
403
404 (defun coding-system-doc-string (coding-system)
405 "Return the documentation string for CODING-SYSTEM."
406 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
407
408 (defun coding-system-plist (coding-system)
409 "Return the property list of CODING-SYSTEM."
410 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
411
412 (defun coding-system-flags (coding-system)
413 "Return `flags' of CODING-SYSTEM.
414 A `flags' of a coding system is a vector of length 32 indicating detailed
415 information of a coding system. See the function `make-coding-system'
416 for more detail."
417 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
418
419 (defun coding-system-get (coding-system prop)
420 "Extract a value from CODING-SYSTEM's property list for property PROP."
421 (plist-get (coding-system-plist coding-system) prop))
422
423 (defun coding-system-put (coding-system prop val)
424 "Change value in CODING-SYSTEM's property list PROP to VAL."
425 (let ((plist (coding-system-plist coding-system)))
426 (if plist
427 (plist-put plist prop val)
428 (aset (coding-system-spec coding-system) coding-spec-plist-idx
429 (list prop val)))))
430
431 (defun coding-system-category (coding-system)
432 "Return the coding category of CODING-SYSTEM."
433 (coding-system-get coding-system 'coding-category))
434
435 (defun coding-system-base (coding-system)
436 "Return the base coding system of CODING-SYSTEM.
437 A base coding system is what made by `make-coding-system'.
438 Any alias nor subsidiary coding systems are not base coding system."
439 (car (coding-system-get coding-system 'alias-coding-systems)))
440
441 (defalias 'coding-system-parent 'coding-system-base)
442 (make-obsolete 'coding-system-parent 'coding-system-base)
443
444 ;; Coding system also has a property `eol-type'.
445 ;;
446 ;; This property indicates how the coding system handles end-of-line
447 ;; format. The value is integer 0, 1, 2, or a vector of three coding
448 ;; systems. Each integer value 0, 1, and 2 indicates the format of
449 ;; end-of-line LF, CRLF, and CR respectively. A vector value
450 ;; indicates that the format of end-of-line should be detected
451 ;; automatically. Nth element of the vector is the subsidiary coding
452 ;; system whose `eol-type' property is N.
453
454 (defun coding-system-eol-type (coding-system)
455 "Return eol-type of CODING-SYSTEM.
456 An eol-type is integer 0, 1, 2, or a vector of coding systems.
457
458 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
459 CRLF, and CR respectively.
460
461 A vector value indicates that a format of end-of-line should be
462 detected automatically. Nth element of the vector is the subsidiary
463 coding system whose eol-type is N."
464 (get coding-system 'eol-type))
465
466 (defun coding-system-lessp (x y)
467 (cond ((eq x 'no-conversion) t)
468 ((eq y 'no-conversion) nil)
469 ((eq x 'emacs-mule) t)
470 ((eq y 'emacs-mule) nil)
471 ((eq x 'undecided) t)
472 ((eq y 'undecided) nil)
473 (t (let ((c1 (coding-system-mnemonic x))
474 (c2 (coding-system-mnemonic y)))
475 (or (< (downcase c1) (downcase c2))
476 (and (not (> (downcase c1) (downcase c2)))
477 (< c1 c2)))))))
478
479 ;; Add CODING-SYSTEM to coding-system-list while keeping it sorted.
480 (defun add-to-coding-system-list (coding-system)
481 (if (or (null coding-system-list)
482 (coding-system-lessp coding-system (car coding-system-list)))
483 (setq coding-system-list (cons coding-system coding-system-list))
484 (let ((len (length coding-system-list))
485 mid (tem coding-system-list))
486 (while (> len 1)
487 (setq mid (nthcdr (/ len 2) tem))
488 (if (coding-system-lessp (car mid) coding-system)
489 (setq tem mid
490 len (- len (/ len 2)))
491 (setq len (/ len 2))))
492 (setcdr tem (cons coding-system (cdr tem))))))
493
494 (defun coding-system-list (&optional base-only)
495 "Return a list of all existing coding systems.
496 If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
497 (let* ((codings (copy-sequence coding-system-list))
498 (tail (cons nil codings)))
499 ;; Remove subsidiary coding systems (eol variants) and alias
500 ;; coding systems (if necessary).
501 (while (cdr tail)
502 (let* ((coding (car (cdr tail)))
503 (aliases (coding-system-get coding 'alias-coding-systems)))
504 (if (or
505 ;; CODING is an eol variant if not in ALIASES.
506 (not (memq coding aliases))
507 ;; CODING is an alias if it is not car of ALIASES.
508 (and base-only (not (eq coding (car aliases)))))
509 (setcdr tail (cdr (cdr tail)))
510 (setq tail (cdr tail)))))
511 codings))
512
513 ;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
514 (defun make-subsidiary-coding-system (coding-system)
515 (let ((coding-spec (coding-system-spec coding-system))
516 (subsidiaries (vector (intern (format "%s-unix" coding-system))
517 (intern (format "%s-dos" coding-system))
518 (intern (format "%s-mac" coding-system))))
519 (i 0)
520 temp)
521 (while (< i 3)
522 (put (aref subsidiaries i) 'coding-system coding-spec)
523 (put (aref subsidiaries i) 'eol-type i)
524 (add-to-coding-system-list (aref subsidiaries i))
525 (setq coding-system-alist
526 (cons (list (symbol-name (aref subsidiaries i)))
527 coding-system-alist))
528 (setq i (1+ i)))
529 subsidiaries))
530
531 (defun make-coding-system (coding-system type mnemonic doc-string
532 &optional flags properties)
533 "Define a new coding system CODING-SYSTEM (symbol).
534 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
535 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
536 in the following format:
537 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
538
539 TYPE is an integer value indicating the type of the coding system as follows:
540 0: Emacs internal format,
541 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
542 2: ISO-2022 including many variants,
543 3: Big5 used mainly on Chinese PC,
544 4: private, CCL programs provide encoding/decoding algorithm,
545 5: Raw-text, which means that text contains random 8-bit codes.
546
547 MNEMONIC is a character to be displayed on mode line for the coding system.
548
549 DOC-STRING is a documentation string for the coding system.
550
551 FLAGS specifies more detailed information of the coding system as follows:
552
553 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
554 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
555 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
556 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
557 SAFE, ACCEPT-LATIN-EXTRA-CODE.
558 CHARSETn are character sets initially designated to Gn graphic registers.
559 If CHARSETn is nil, Gn is never used.
560 If CHARSETn is t, Gn can be used but nothing designated initially.
561 If CHARSETn is a list of character sets, those character sets are
562 designated to Gn on output, but nothing designated to Gn initially.
563 But, character set `ascii' can be designated only to G0.
564 SHORT-FORM non-nil means use short designation sequence on output.
565 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
566 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
567 SPACE on output.
568 SEVEN non-nil means use 7-bit code only on output.
569 LOCKING-SHIFT non-nil means use locking-shift.
570 SINGLE-SHIFT non-nil means use single-shift.
571 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
572 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
573 NO-ISO6429 non-nil means not use ISO6429's direction specification.
574 INIT-BOL non-nil means any designation state is assumed to be reset
575 to initial at each beginning of line on output.
576 DESIGNATION-BOL non-nil means designation sequences should be placed
577 at beginning of line on output.
578 SAFE non-nil means convert unsafe characters to `?' on output.
579 Unsafe characters are what not specified in SAFE-CHARSET.
580 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
581 a code specified in `latin-extra-code-table' (which see) as a valid
582 code of the coding system.
583
584 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
585 decoding and encoding. CCL programs should be specified by their
586 symbols.
587
588 PROPERTIES is an alist of properties vs the corresponding values.
589 These properties are set in PLIST, a property list. This function
590 also sets properties `coding-category' and `alias-coding-systems'
591 automatically.
592
593 Kludgy features for backward compatibility:
594
595 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
596 treated as a compiled CCL code.
597
598 2. If PROPERTIES is just a list of character sets, the list is set as
599 a value of `safe-charsets' in PLIST."
600 (if (memq coding-system coding-system-list)
601 (error "Coding system %s already exists" coding-system))
602
603 ;; Set a value of `coding-system' property.
604 (let ((coding-spec (make-vector 5 nil))
605 (no-initial-designation t)
606 (no-alternative-designation t)
607 coding-category)
608 (if (or (not (integerp type)) (< type 0) (> type 5))
609 (error "TYPE argument must be 0..5"))
610 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
611 (error "MNEMONIC argument must be an ASCII printable character."))
612 (aset coding-spec coding-spec-type-idx type)
613 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
614 (aset coding-spec coding-spec-doc-string-idx
615 (if (stringp doc-string) doc-string ""))
616 (cond ((= type 0)
617 (setq coding-category 'coding-category-emacs-mule))
618 ((= type 1)
619 (setq coding-category 'coding-category-sjis))
620 ((= type 2) ; ISO2022
621 (let ((i 0)
622 (vec (make-vector 32 nil))
623 (g1-designation nil)
624 (fl flags))
625 (while (< i 4)
626 (let ((charset (car fl)))
627 (if (and no-initial-designation
628 (> i 0)
629 (or (charsetp charset)
630 (and (consp charset)
631 (charsetp (car charset)))))
632 (setq no-initial-designation nil))
633 (if (charsetp charset)
634 (if (= i 1) (setq g1-designation charset))
635 (if (consp charset)
636 (let ((tail charset)
637 elt)
638 (while tail
639 (setq elt (car tail))
640 (if (eq elt t)
641 (setq no-alternative-designation nil)
642 (if (and elt (not (charsetp elt)))
643 (error "Invalid charset: %s" elt)))
644 (setq tail (cdr tail)))
645 (setq g1-designation (car charset)))
646 (if charset
647 (if (eq charset t)
648 (setq no-alternative-designation nil)
649 (error "Invalid charset: %s" charset)))))
650 (aset vec i charset))
651 (setq fl (cdr fl) i (1+ i)))
652 (while (and (< i 32) fl)
653 (aset vec i (car fl))
654 (setq fl (cdr fl) i (1+ i)))
655 (aset coding-spec 4 vec)
656 (setq coding-category
657 (if (aref vec 8) ; Use locking-shift.
658 (or (and (aref vec 7) 'coding-category-iso-7-else)
659 'coding-category-iso-8-else)
660 (if (aref vec 7) ; 7-bit only.
661 (if (aref vec 9) ; Use single-shift.
662 'coding-category-iso-7-else
663 (if no-alternative-designation
664 'coding-category-iso-7-tight
665 'coding-category-iso-7))
666 (if (or no-initial-designation
667 (not no-alternative-designation))
668 'coding-category-iso-8-else
669 (if (and (charsetp g1-designation)
670 (= (charset-dimension g1-designation) 2))
671 'coding-category-iso-8-2
672 'coding-category-iso-8-1)))))))
673 ((= type 3)
674 (setq coding-category 'coding-category-big5))
675 ((= type 4) ; private
676 (setq coding-category 'coding-category-ccl)
677 (if (not (consp flags))
678 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
679 (let ((decoder (check-ccl-program
680 (car flags)
681 (intern (format "%s-decoder" coding-system))))
682 (encoder (check-ccl-program
683 (cdr flags)
684 (intern (format "%s-encoder" coding-system)))))
685 (if (and decoder encoder)
686 (aset coding-spec 4 (cons decoder encoder))
687 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
688 (t ; i.e. (= type 5)
689 (setq coding-category 'coding-category-raw-text)))
690
691 (let ((plist (list 'coding-category coding-category
692 'alias-coding-systems (list coding-system))))
693 (if no-initial-designation
694 (plist-put plist 'no-initial-designation t))
695 (if (and properties
696 (or (eq properties t)
697 (not (consp (car properties)))))
698 ;; In the old version, the arg PROPERTIES is a list to be
699 ;; set in PLIST as a value of property `safe-charsets'.
700 (plist-put plist 'safe-charsets properties)
701 (let ((l properties))
702 (while l
703 (plist-put plist (car (car l)) (cdr (car l)))
704 (setq l (cdr l)))))
705 (aset coding-spec coding-spec-plist-idx plist))
706 (put coding-system 'coding-system coding-spec)
707 (put coding-category 'coding-systems
708 (cons coding-system (get coding-category 'coding-systems))))
709
710 ;; Next, set a value of `eol-type' property. The value is a vector
711 ;; of subsidiary coding systems, each corresponds to a coding system
712 ;; for the detected end-of-line format.
713 (put coding-system 'eol-type
714 (if (or (<= type 3) (= type 5))
715 (make-subsidiary-coding-system coding-system)
716 0))
717
718 ;; At last, register CODING-SYSTEM in `coding-system-list' and
719 ;; `coding-system-alist'.
720 (add-to-coding-system-list coding-system)
721 (setq coding-system-alist (cons (list (symbol-name coding-system))
722 coding-system-alist))
723
724 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
725 ;; XXX-with-esc variants.
726 (let ((coding-category (coding-system-category coding-system)))
727 (if (or (eq coding-category 'coding-category-iso-8-1)
728 (eq coding-category 'coding-category-iso-8-2))
729 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
730 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system)))
731 (make-coding-system esc type mnemonic doc
732 (if (listp (car flags))
733 (cons (append (car flags) '(t)) (cdr flags))
734 (cons (list (car flags) t) (cdr flags)))
735 properties)
736 (coding-system-put esc 'mime-charset nil)
737 (coding-system-put esc 'safe-charsets t))))
738
739 coding-system)
740
741 (defun define-coding-system-alias (alias coding-system)
742 "Define ALIAS as an alias for coding system CODING-SYSTEM."
743 (put alias 'coding-system (coding-system-spec coding-system))
744 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
745 (add-to-coding-system-list alias)
746 (setq coding-system-alist (cons (list (symbol-name alias))
747 coding-system-alist))
748 (let ((eol-type (coding-system-eol-type coding-system)))
749 (if (vectorp eol-type)
750 (put alias 'eol-type (make-subsidiary-coding-system alias))
751 (put alias 'eol-type eol-type))))
752
753 (defun set-buffer-file-coding-system (coding-system &optional force)
754 "Set the file coding-system of the current buffer to CODING-SYSTEM.
755 This means that when you save the buffer, it will be converted
756 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
757 use \\[list-coding-systems].
758
759 If the buffer's previous file coding-system value specifies end-of-line
760 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
761 merged with the already-specified end-of-line conversion.
762 However, if the optional prefix argument FORCE is non-nil,
763 then CODING-SYSTEM is used exactly as specified.
764
765 This marks the buffer modified so that the succeeding \\[save-buffer]
766 surely saves the buffer with CODING-SYSTEM. From a program, if you
767 don't want to mark the buffer modified, just set the variable
768 `buffer-file-coding-system' directly."
769 (interactive "zCoding system for visited file (default, nil): \nP")
770 (check-coding-system coding-system)
771 (if (null force)
772 (let ((x (coding-system-eol-type buffer-file-coding-system))
773 (y (coding-system-eol-type coding-system)))
774 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
775 (setq coding-system (aref y x)))))
776 (setq buffer-file-coding-system coding-system)
777 (set-buffer-modified-p t)
778 (force-mode-line-update))
779
780 (defvar default-terminal-coding-system nil
781 "Default value for the terminal coding system.
782 This is normally set according to the selected language environment.
783 See also the command `set-terminal-coding-system'.")
784
785 (defun set-terminal-coding-system (coding-system)
786 "Set coding system of your terminal to CODING-SYSTEM.
787 All text output to the terminal will be encoded
788 with the specified coding system.
789 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
790 The default is determined by the selected language environment
791 or by the previous use of this command."
792 (interactive
793 (list (let ((default (if (and (not (terminal-coding-system))
794 default-terminal-coding-system)
795 default-terminal-coding-system)))
796 (read-coding-system
797 (format "Coding system for terminal display (default, %s): "
798 default)
799 default))))
800 (if (and (not coding-system)
801 (not (terminal-coding-system)))
802 (setq coding-system default-terminal-coding-system))
803 (if coding-system
804 (setq default-terminal-coding-system coding-system))
805 (set-terminal-coding-system-internal coding-system)
806 (redraw-frame (selected-frame)))
807
808 (defvar default-keyboard-coding-system nil
809 "Default value of the keyboard coding system.
810 This is normally set according to the selected language environment.
811 See also the command `set-keyboard-coding-system'.")
812
813 (defun set-keyboard-coding-system (coding-system)
814 "Set coding system for keyboard input to CODING-SYSTEM.
815 In addition, this command enables Encoded-kbd minor mode.
816 \(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
817 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
818 The default is determined by the selected language environment
819 or by the previous use of this command."
820 (interactive
821 (list (let ((default (if (and (not (keyboard-coding-system))
822 default-keyboard-coding-system)
823 default-keyboard-coding-system)))
824 (read-coding-system
825 (format "Coding system for keyboard input (default, %s): "
826 default)
827 default))))
828 (if (and (not coding-system)
829 (not (keyboard-coding-system)))
830 (setq coding-system default-keyboard-coding-system))
831 (if coding-system
832 (setq default-keyboard-coding-system coding-system))
833 (set-keyboard-coding-system-internal coding-system)
834 (encoded-kbd-mode (if coding-system 1 0)))
835
836 (defun set-buffer-process-coding-system (decoding encoding)
837 "Set coding systems for the process associated with the current buffer.
838 DECODING is the coding system to be used to decode input from the process,
839 ENCODING is the coding system to be used to encode output to the process.
840
841 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
842 (interactive
843 "zCoding-system for process input: \nzCoding-system for process output: ")
844 (let ((proc (get-buffer-process (current-buffer))))
845 (if (null proc)
846 (error "no process")
847 (check-coding-system decoding)
848 (check-coding-system encoding)
849 (set-process-coding-system proc decoding encoding)))
850 (force-mode-line-update))
851
852 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
853
854 (defun set-selection-coding-system (coding-system)
855 "Make CODING-SYSTEM used for communicating with other X clients .
856 When sending or receiving text via cut_buffer, selection, and clipboard,
857 the text is encoded or decoded by CODING-SYSTEM."
858 (interactive "zCoding system for X selection: ")
859 (check-coding-system coding-system)
860 (setq selection-coding-system coding-system))
861
862 ;; Coding system lastly specified by the command
863 ;; set-next-selection-coding-system.
864 (defvar last-next-selection-coding-system nil)
865
866 (defun set-next-selection-coding-system (coding-system)
867 "Make CODING-SYSTEM used for the next communication with other X clients.
868 This setting is effective for the next communication only."
869 (interactive
870 (list (read-coding-system
871 (if last-next-selection-coding-system
872 (format "Coding system for the next X selection (default, %S): "
873 last-next-selection-coding-system)
874 "Coding system for the next X selection: ")
875 last-next-selection-coding-system)))
876 (if coding-system
877 (setq last-next-selection-coding-system coding-system)
878 (setq coding-system last-next-selection-coding-system))
879 (check-coding-system coding-system)
880
881 (setq next-selection-coding-system coding-system))
882
883 (defun set-coding-priority (arg)
884 "Set priority of coding categories according to LIST.
885 LIST is a list of coding categories ordered by priority."
886 (let ((l arg)
887 (current-list (copy-sequence coding-category-list)))
888 ;; Check the validity of ARG while deleting coding categories in
889 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
890 ;; contains all coding categories.
891 (while l
892 (if (or (null (get (car l) 'coding-category-index))
893 (null (memq (car l) current-list)))
894 (error "Invalid or duplicated element in argument: %s" arg))
895 (setq current-list (delq (car l) current-list))
896 (setq l (cdr l)))
897 ;; Update `coding-category-list' and return it.
898 (setq coding-category-list (append arg current-list))
899 (set-coding-priority-internal)))
900
901 ;;; FILE I/O
902
903 (defvar auto-coding-alist
904 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
905 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
906 "Alist of filename patterns vs corresponding coding systems.
907 Each element looks like (REGEXP . CODING-SYSTEM).
908 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
909
910 The settings in this alist take priority over `coding:' tags
911 in the file (see the function `set-auto-coding')
912 and the contents of `file-coding-system-alist'.")
913
914 (defvar set-auto-coding-for-load nil
915 "Non-nil means look for `load-coding' property instead of `coding'.
916 This is used for loading and byte-compiling Emacs Lisp files.")
917
918 (defun auto-coding-alist-lookup (filename)
919 "Return the coding system specified by `auto-coding-alist' for FILENAME."
920 (let ((alist auto-coding-alist)
921 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
922 coding-system)
923 (while (and alist (not coding-system))
924 (if (string-match (car (car alist)) filename)
925 (setq coding-system (cdr (car alist)))
926 (setq alist (cdr alist))))
927 coding-system))
928
929 (defun set-auto-coding (filename size)
930 "Return coding system for a file FILENAME of which SIZE bytes follow point.
931 These bytes should include at least the first 1k of the file
932 and the last 3k of the file, but the middle may be omitted.
933
934 It checks FILENAME against the variable `auto-coding-alist'.
935 If FILENAME doesn't match any entries in the variable,
936 it checks for a `coding:' tag in the first one or two lines following
937 point. If no `coding:' tag is found, it checks for local variables
938 list in the last 3K bytes out of the SIZE bytes.
939
940 The return value is the specified coding system,
941 or nil if nothing specified.
942
943 The variable `set-auto-coding-function' (which see) is set to this
944 function by default."
945 (let ((coding-system (auto-coding-alist-lookup filename)))
946
947 (or coding-system
948 (let* ((case-fold-search t)
949 (head-start (point))
950 (head-end (+ head-start (min size 1024)))
951 (tail-start (+ head-start (max (- size 3072) 0)))
952 (tail-end (+ head-start size))
953 coding-system head-found tail-found pos)
954 ;; Try a short cut by searching for the string "coding:"
955 ;; and for "unibyte:" at the head and tail of SIZE bytes.
956 (setq head-found (or (search-forward "coding:" head-end t)
957 (search-forward "unibyte:" head-end t)))
958 (if (and head-found (> head-found tail-start))
959 ;; Head and tail are overlapped.
960 (setq tail-found head-found)
961 (goto-char tail-start)
962 (setq tail-found (or (search-forward "coding:" tail-end t)
963 (search-forward "unibyte:" tail-end t))))
964
965 ;; At first check the head.
966 (when head-found
967 (goto-char head-start)
968 (setq pos (re-search-forward "[\n\r]" head-end t))
969 (if (and pos
970 (= (char-after head-start) ?#)
971 (= (char-after (1+ head-start)) ?!))
972 ;; If the file begins with "#!" (exec interpreter magic),
973 ;; look for coding frobs in the first two lines. You cannot
974 ;; necessarily put them in the first line of such a file
975 ;; without screwing up the interpreter invocation.
976 (setq pos (search-forward "\n" head-end t)))
977 (if pos (setq head-end pos))
978 (when (< head-found head-end)
979 (goto-char head-start)
980 (when (and set-auto-coding-for-load
981 (re-search-forward
982 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
983 head-end t))
984 (setq coding-system 'raw-text))
985 (when (and (not coding-system)
986 (re-search-forward
987 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
988 head-end t))
989 (setq coding-system (intern (match-string 2)))
990 (or (coding-system-p coding-system)
991 (setq coding-system nil)))))
992
993 ;; If no coding: tag in the head, check the tail.
994 (when (and tail-found (not coding-system))
995 (goto-char tail-start)
996 (search-forward "\n\^L" nil t)
997 (if (re-search-forward
998 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
999 ;; The prefix is what comes before "local variables:" in its
1000 ;; line. The suffix is what comes after "local variables:"
1001 ;; in its line.
1002 (let* ((prefix (regexp-quote (match-string 1)))
1003 (suffix (regexp-quote (match-string 2)))
1004 (re-coding
1005 (concat
1006 "^" prefix
1007 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1008 suffix "$"))
1009 (re-unibyte
1010 (concat
1011 "^" prefix
1012 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1013 suffix "$"))
1014 (re-end
1015 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
1016 (pos (point)))
1017 (re-search-forward re-end tail-end 'move)
1018 (setq tail-end (point))
1019 (goto-char pos)
1020 (when (and set-auto-coding-for-load
1021 (re-search-forward re-unibyte tail-end t))
1022 (setq coding-system 'raw-text))
1023 (when (and (not coding-system)
1024 (re-search-forward re-coding tail-end t))
1025 (setq coding-system (intern (match-string 1)))
1026 (or (coding-system-p coding-system)
1027 (setq coding-system nil))))))
1028 coding-system))))
1029
1030 (setq set-auto-coding-function 'set-auto-coding)
1031
1032 ;; Set buffer-file-coding-system of the current buffer after some text
1033 ;; is inserted.
1034 (defun after-insert-file-set-buffer-file-coding-system (inserted)
1035 (if last-coding-system-used
1036 (let ((coding-system
1037 (find-new-buffer-file-coding-system last-coding-system-used))
1038 (modified-p (buffer-modified-p)))
1039 (when coding-system
1040 (set-buffer-file-coding-system coding-system)
1041 (if (and enable-multibyte-characters
1042 (or (eq coding-system 'no-conversion)
1043 (eq (coding-system-type coding-system) 5))
1044 ;; If buffer was unmodified, we must be visiting it.
1045 (not modified-p))
1046 ;; For coding systems no-conversion and raw-text...,
1047 ;; edit the buffer as unibyte.
1048 (let ((pos-byte (position-bytes (+ (point) inserted))))
1049 (set-buffer-multibyte nil)
1050 (setq inserted (- pos-byte (position-bytes (point))))))
1051 (set-buffer-modified-p modified-p))))
1052 inserted)
1053
1054 (add-hook 'after-insert-file-functions
1055 'after-insert-file-set-buffer-file-coding-system)
1056
1057 ;; The coding-spec and eol-type of coding-system returned is decided
1058 ;; independently in the following order.
1059 ;; 1. That of buffer-file-coding-system locally bound.
1060 ;; 2. That of CODING.
1061
1062 (defun find-new-buffer-file-coding-system (coding)
1063 "Return a coding system for a buffer when a file of CODING is inserted.
1064 The local variable `buffer-file-coding-system' of the current buffer
1065 is set to the returned value.
1066 Return nil if there's no need to set `buffer-file-coding-system'."
1067 (let (local-coding local-eol
1068 found-coding found-eol
1069 new-coding new-eol)
1070 (if (null coding)
1071 ;; Nothing found about coding.
1072 nil
1073
1074 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1075 ;; and LOCAL-CODING.
1076 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1077 (if (null (numberp local-eol))
1078 ;; But eol-type is not yet set.
1079 (setq local-eol nil))
1080 (if (and buffer-file-coding-system
1081 (not (eq (coding-system-type buffer-file-coding-system) t)))
1082 ;; This is not `undecided'.
1083 (setq local-coding (coding-system-base buffer-file-coding-system)))
1084
1085 (if (and (local-variable-p 'buffer-file-coding-system)
1086 local-eol local-coding)
1087 ;; The current buffer has already set full coding-system, we
1088 ;; had better not change it.
1089 nil
1090
1091 (setq found-eol (coding-system-eol-type coding))
1092 (if (null (numberp found-eol))
1093 ;; But eol-type is not found.
1094 ;; If EOL conversions are inhibited, force unix eol-type.
1095 (setq found-eol (if inhibit-eol-conversion 0)))
1096 (if (eq (coding-system-type coding) t)
1097 (setq found-coding 'undecided)
1098 (setq found-coding (coding-system-base coding)))
1099
1100 (if (and (not found-eol) (eq found-coding 'undecided))
1101 ;; No valid coding information found.
1102 nil
1103
1104 ;; Some coding information (eol or text) found.
1105
1106 ;; The local setting takes precedence over the found one.
1107 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1108 (or local-coding found-coding)
1109 (or found-coding local-coding)))
1110 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1111 (or local-eol found-eol)
1112 (or found-eol local-eol)))
1113
1114 (let ((eol-type (coding-system-eol-type new-coding)))
1115 (if (and (numberp new-eol) (vectorp eol-type))
1116 (aref eol-type new-eol)
1117 new-coding)))))))
1118
1119 (defun modify-coding-system-alist (target-type regexp coding-system)
1120 "Modify one of look up tables for finding a coding system on I/O operation.
1121 There are three of such tables, `file-coding-system-alist',
1122 `process-coding-system-alist', and `network-coding-system-alist'.
1123
1124 TARGET-TYPE specifies which of them to modify.
1125 If it is `file', it affects `file-coding-system-alist' (which see).
1126 If it is `process', it affects `process-coding-system-alist' (which see).
1127 If it is `network', it affects `network-coding-system-alist' (which see).
1128
1129 REGEXP is a regular expression matching a target of I/O operation.
1130 The target is a file name if TARGET-TYPE is `file', a program name if
1131 TARGET-TYPE is `process', or a network service name or a port number
1132 to connect to if TARGET-TYPE is `network'.
1133
1134 CODING-SYSTEM is a coding system to perform code conversion on the I/O
1135 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1136 for decoding and encoding respectively,
1137 or a function symbol which, when called, returns such a cons cell."
1138 (or (memq target-type '(file process network))
1139 (error "Invalid target type: %s" target-type))
1140 (or (stringp regexp)
1141 (and (eq target-type 'network) (integerp regexp))
1142 (error "Invalid regular expression: %s" regexp))
1143 (if (symbolp coding-system)
1144 (if (not (fboundp coding-system))
1145 (progn
1146 (check-coding-system coding-system)
1147 (setq coding-system (cons coding-system coding-system))))
1148 (check-coding-system (car coding-system))
1149 (check-coding-system (cdr coding-system)))
1150 (cond ((eq target-type 'file)
1151 (let ((slot (assoc regexp file-coding-system-alist)))
1152 (if slot
1153 (setcdr slot coding-system)
1154 (setq file-coding-system-alist
1155 (cons (cons regexp coding-system)
1156 file-coding-system-alist)))))
1157 ((eq target-type 'process)
1158 (let ((slot (assoc regexp process-coding-system-alist)))
1159 (if slot
1160 (setcdr slot coding-system)
1161 (setq process-coding-system-alist
1162 (cons (cons regexp coding-system)
1163 process-coding-system-alist)))))
1164 (t
1165 (let ((slot (assoc regexp network-coding-system-alist)))
1166 (if slot
1167 (setcdr slot coding-system)
1168 (setq network-coding-system-alist
1169 (cons (cons regexp coding-system)
1170 network-coding-system-alist)))))))
1171
1172 (defun make-translation-table (&rest args)
1173 "Make a translation table (char table) from arguments.
1174 Each argument is a list of the form (FROM . TO),
1175 where FROM is a character to be translated to TO.
1176
1177 FROM can be a generic character (see `make-char'). In this case, TO is
1178 a generic character containing the same number of characters, or a
1179 ordinary character. If FROM and TO are both generic characters, all
1180 characters belonging to FROM are translated to characters belonging to TO
1181 without changing their position code(s)."
1182 (let ((table (make-char-table 'translation-table))
1183 revlist)
1184 (while args
1185 (let ((elts (car args)))
1186 (while elts
1187 (let* ((from (car (car elts)))
1188 (from-i 0) ; degree of freedom of FROM
1189 (from-rev (nreverse (split-char from)))
1190 (to (cdr (car elts)))
1191 (to-i 0) ; degree of freedom of TO
1192 (to-rev (nreverse (split-char to))))
1193 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1194 (while (eq (car from-rev) 0)
1195 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1196 (while (eq (car to-rev) 0)
1197 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1198 (if (and (/= from-i to-i) (/= to-i 0))
1199 (error "Invalid character pair (%d . %d)" from to))
1200 ;; If we have already translated TO to TO-ALT, FROM should
1201 ;; also be translated to TO-ALT. But, this is only if TO
1202 ;; is a generic character or TO-ALT is not a generic
1203 ;; character.
1204 (let ((to-alt (aref table to)))
1205 (if (and to-alt
1206 (or (> to-i 0) (not (generic-char-p to-alt))))
1207 (setq to to-alt)))
1208 (if (> from-i 0)
1209 (set-char-table-default table from to)
1210 (aset table from to))
1211 ;; If we have already translated some chars to FROM, they
1212 ;; should also be translated to TO.
1213 (let ((l (assq from revlist)))
1214 (if l
1215 (let ((ch (car l)))
1216 (setcar l to)
1217 (setq l (cdr l))
1218 (while l
1219 (aset table ch to)
1220 (setq l (cdr l)) ))))
1221 ;; Now update REVLIST.
1222 (let ((l (assq to revlist)))
1223 (if l
1224 (setcdr l (cons from (cdr l)))
1225 (setq revlist (cons (list to from) revlist)))))
1226 (setq elts (cdr elts))))
1227 (setq args (cdr args)))
1228 ;; Return TABLE just created.
1229 table))
1230
1231 (defun make-translation-table-from-vector (vec)
1232 "Make translation table from decoding vector VEC.
1233 VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1234 See also the variable `nonascii-translation-table'."
1235 (let ((table (make-char-table 'translation-table))
1236 (rev-table (make-char-table 'translation-table))
1237 (i 0)
1238 ch)
1239 (while (< i 256)
1240 (setq ch (aref vec i))
1241 (aset table i ch)
1242 (if (>= ch 256)
1243 (aset rev-table ch i))
1244 (setq i (1+ i)))
1245 (set-char-table-extra-slot table 0 rev-table)
1246 table))
1247
1248 (defun define-translation-table (symbol &rest args)
1249 "Define SYMBOL as a name of translation table made by ARGS.
1250
1251 If the first element of ARGS is a char-table of which purpose is
1252 translation-table, just define SYMBOL as the name of it.
1253
1254 In the other case, ARGS are the same as arguments to the function
1255 `make-translation-table' (which see).
1256
1257 This function sets properties `translation-table' and
1258 `translation-table-id' of SYMBOL to the created table itself and
1259 identification number of the table respectively."
1260 (let ((table (if (and (char-table-p (car args))
1261 (eq (char-table-subtype (car args))
1262 'translation-table))
1263 (car args)
1264 (apply 'make-translation-table args)))
1265 (len (length translation-table-vector))
1266 (id 0)
1267 (done nil))
1268 (put symbol 'translation-table table)
1269 (while (not done)
1270 (if (>= id len)
1271 (setq translation-table-vector
1272 (vconcat translation-table-vector (make-vector len nil))))
1273 (let ((slot (aref translation-table-vector id)))
1274 (if (or (not slot)
1275 (eq (car slot) symbol))
1276 (progn
1277 (aset translation-table-vector id (cons symbol table))
1278 (setq done t))
1279 (setq id (1+ id)))))
1280 (put symbol 'translation-table-id id)
1281 id))
1282
1283 (put 'with-category-table 'lisp-indent-function 1)
1284
1285 (defmacro with-category-table (category-table &rest body)
1286 `(let ((current-category-table (category-table)))
1287 (set-category-table ,category-table)
1288 (unwind-protect
1289 (progn ,@body)
1290 (set-category-table current-category-table))))
1291
1292 ;;; Initialize some variables.
1293
1294 (put 'use-default-ascent 'char-table-extra-slots 0)
1295 (setq use-default-ascent (make-char-table 'use-default-ascent))
1296 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1297 (setq ignore-relative-composition
1298 (make-char-table 'ignore-relative-composition))
1299
1300 ;;;
1301 (provide 'mule)
1302
1303 ;;; mule.el ends here