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