]> code.delx.au - gnu-emacs/blob - lisp/international/mule.el
(skkdic-convert): Insert (require 'ja-dic-cnv), not (require
[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 (SAKAKI)" "\
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' sets this value automatically
317 ;; unless its argument PROPERTIES specifies this property.
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' sets this
324 ;; value automatically and `define-coding-system-alias' updates it.
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-chars
355 ;;
356 ;; The value is a char table. If a character has non-nil value in it,
357 ;; the character is safely supported by the coding system. This
358 ;; overrides the specification of safe-charsets.
359
360 ;; o safe-charsets
361 ;;
362 ;; The value is a list of charsets safely supported by the coding
363 ;; system. The value t means that all charsets Emacs handles are
364 ;; supported. Even if some charset is not in this list, it doesn't
365 ;; mean that the charset can't be encoded in the coding system,
366 ;; instead, it just means that some other receiver of a text encoded
367 ;; in the coding system won't be able to handle that charset.
368 ;;
369 ;; o mime-charset
370 ;;
371 ;; The value is a symbol of which name is `MIME-charset' parameter of
372 ;; the coding system.
373 ;;
374 ;; o charset-origin-alist
375 ;;
376 ;; The value is a list of this form:
377 ;; (CHARSET EXTERNAL-CHARSET-NAME ENCODING-FUNCTION).
378 ;; ENCODING-FUNCTION is a function to encode a character in CHARSET
379 ;; to the code in EXTERNAL-CHARSET-NAME. The command what-cursor-position
380 ;; uses this information of the buffer-file-coding-system.
381 ;; ENCODING-FUNCTION may be a translation table or a symbol whose
382 ;; property `translation-table' is a translation table. In these case,
383 ;; the translation table is used to encode the character.
384 ;;
385 ;; o valid-codes (meaningful only for a coding system based on CCL)
386 ;;
387 ;; The value is a list to indicate valid byte ranges of the encoded
388 ;; file. Each element of the list is an integer or a cons of integer.
389 ;; In the former case, the integer value is a valid byte code. In the
390 ;; latter case, the integers specifies the range of valid byte codes.
391
392
393 ;; Return coding-spec of CODING-SYSTEM
394 (defsubst coding-system-spec (coding-system)
395 (get (check-coding-system coding-system) 'coding-system))
396
397 (defun coding-system-type (coding-system)
398 "Return the coding type of CODING-SYSTEM.
399 A coding type is an integer value indicating the encoding method
400 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
401 (aref (coding-system-spec coding-system) coding-spec-type-idx))
402
403 (defun coding-system-mnemonic (coding-system)
404 "Return the mnemonic character of CODING-SYSTEM.
405 The mnemonic character of a coding system is used in mode line
406 to indicate the coding system. If the arg is nil, return ?-."
407 (let ((spec (coding-system-spec coding-system)))
408 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
409
410 (defun coding-system-doc-string (coding-system)
411 "Return the documentation string for CODING-SYSTEM."
412 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
413
414 (defun coding-system-plist (coding-system)
415 "Return the property list of CODING-SYSTEM."
416 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
417
418 (defun coding-system-flags (coding-system)
419 "Return `flags' of CODING-SYSTEM.
420 A `flags' of a coding system is a vector of length 32 indicating detailed
421 information of a coding system. See the function `make-coding-system'
422 for more detail."
423 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
424
425 (defun coding-system-get (coding-system prop)
426 "Extract a value from CODING-SYSTEM's property list for property PROP."
427 (plist-get (coding-system-plist coding-system) prop))
428
429 (defun coding-system-put (coding-system prop val)
430 "Change value in CODING-SYSTEM's property list PROP to VAL."
431 (let ((plist (coding-system-plist coding-system)))
432 (if plist
433 (plist-put plist prop val)
434 (aset (coding-system-spec coding-system) coding-spec-plist-idx
435 (list prop val)))))
436
437 (defun coding-system-category (coding-system)
438 "Return the coding category of CODING-SYSTEM."
439 (coding-system-get coding-system 'coding-category))
440
441 (defun coding-system-base (coding-system)
442 "Return the base coding system of CODING-SYSTEM.
443 A base coding system is what made by `make-coding-system'.
444 Any alias nor subsidiary coding systems are not base coding system."
445 (car (coding-system-get coding-system 'alias-coding-systems)))
446
447 (defalias 'coding-system-parent 'coding-system-base)
448 (make-obsolete 'coding-system-parent 'coding-system-base "20.3")
449
450 ;; Coding system also has a property `eol-type'.
451 ;;
452 ;; This property indicates how the coding system handles end-of-line
453 ;; format. The value is integer 0, 1, 2, or a vector of three coding
454 ;; systems. Each integer value 0, 1, and 2 indicates the format of
455 ;; end-of-line LF, CRLF, and CR respectively. A vector value
456 ;; indicates that the format of end-of-line should be detected
457 ;; automatically. Nth element of the vector is the subsidiary coding
458 ;; system whose `eol-type' property is N.
459
460 (defun coding-system-eol-type (coding-system)
461 "Return eol-type of CODING-SYSTEM.
462 An eol-type is integer 0, 1, 2, or a vector of coding systems.
463
464 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
465 CRLF, and CR respectively.
466
467 A vector value indicates that a format of end-of-line should be
468 detected automatically. Nth element of the vector is the subsidiary
469 coding system whose eol-type is N."
470 (get coding-system 'eol-type))
471
472 (defun coding-system-lessp (x y)
473 (cond ((eq x 'no-conversion) t)
474 ((eq y 'no-conversion) nil)
475 ((eq x 'emacs-mule) t)
476 ((eq y 'emacs-mule) nil)
477 ((eq x 'undecided) t)
478 ((eq y 'undecided) nil)
479 (t (let ((c1 (coding-system-mnemonic x))
480 (c2 (coding-system-mnemonic y)))
481 (or (< (downcase c1) (downcase c2))
482 (and (not (> (downcase c1) (downcase c2)))
483 (< c1 c2)))))))
484
485 ;; Add CODING-SYSTEM to coding-system-list while keeping it sorted.
486 (defun add-to-coding-system-list (coding-system)
487 (if (or (null coding-system-list)
488 (coding-system-lessp coding-system (car coding-system-list)))
489 (setq coding-system-list (cons coding-system coding-system-list))
490 (let ((len (length coding-system-list))
491 mid (tem coding-system-list))
492 (while (> len 1)
493 (setq mid (nthcdr (/ len 2) tem))
494 (if (coding-system-lessp (car mid) coding-system)
495 (setq tem mid
496 len (- len (/ len 2)))
497 (setq len (/ len 2))))
498 (setcdr tem (cons coding-system (cdr tem))))))
499
500 (defun coding-system-list (&optional base-only)
501 "Return a list of all existing non-subsidiary coding systems.
502 If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
503 The value doesn't include subsidiary coding systems which are what
504 made from bases and aliases automatically for various end-of-line
505 formats (e.g. iso-latin-1-unix, koi8-r-dos)."
506 (let* ((codings (copy-sequence coding-system-list))
507 (tail (cons nil codings)))
508 ;; Remove subsidiary coding systems (eol variants) and alias
509 ;; coding systems (if necessary).
510 (while (cdr tail)
511 (let* ((coding (car (cdr tail)))
512 (aliases (coding-system-get coding 'alias-coding-systems)))
513 (if (or
514 ;; CODING is an eol variant if not in ALIASES.
515 (not (memq coding aliases))
516 ;; CODING is an alias if it is not car of ALIASES.
517 (and base-only (not (eq coding (car aliases)))))
518 (setcdr tail (cdr (cdr tail)))
519 (setq tail (cdr tail)))))
520 codings))
521
522 (defun register-char-codings (coding-system safe-chars)
523 (let ((general (char-table-extra-slot char-coding-system-table 0)))
524 (if (eq safe-chars t)
525 (or (memq coding-system general)
526 (set-char-table-extra-slot char-coding-system-table 0
527 (cons coding-system general)))
528 (map-char-table
529 (function
530 (lambda (key val)
531 (if (and (>= key 128) val)
532 (let ((codings (aref char-coding-system-table key)))
533 (or (memq coding-system codings)
534 (aset char-coding-system-table key
535 (cons coding-system codings)))))))
536 safe-chars))))
537
538
539 ;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
540 (defun make-subsidiary-coding-system (coding-system)
541 (let ((coding-spec (coding-system-spec coding-system))
542 (subsidiaries (vector (intern (format "%s-unix" coding-system))
543 (intern (format "%s-dos" coding-system))
544 (intern (format "%s-mac" coding-system))))
545 (i 0)
546 temp)
547 (while (< i 3)
548 (put (aref subsidiaries i) 'coding-system coding-spec)
549 (put (aref subsidiaries i) 'eol-type i)
550 (add-to-coding-system-list (aref subsidiaries i))
551 (setq coding-system-alist
552 (cons (list (symbol-name (aref subsidiaries i)))
553 coding-system-alist))
554 (setq i (1+ i)))
555 subsidiaries))
556
557 (defun make-coding-system (coding-system type mnemonic doc-string
558 &optional
559 flags
560 properties
561 eol-type)
562 "Define a new coding system CODING-SYSTEM (symbol).
563 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
564 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
565 in the following format:
566 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
567
568 TYPE is an integer value indicating the type of the coding system as follows:
569 0: Emacs internal format,
570 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
571 2: ISO-2022 including many variants,
572 3: Big5 used mainly on Chinese PC,
573 4: private, CCL programs provide encoding/decoding algorithm,
574 5: Raw-text, which means that text contains random 8-bit codes.
575
576 MNEMONIC is a character to be displayed on mode line for the coding system.
577
578 DOC-STRING is a documentation string for the coding system.
579
580 FLAGS specifies more detailed information of the coding system as follows:
581
582 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
583 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
584 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
585 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
586 SAFE, ACCEPT-LATIN-EXTRA-CODE.
587 CHARSETn are character sets initially designated to Gn graphic registers.
588 If CHARSETn is nil, Gn is never used.
589 If CHARSETn is t, Gn can be used but nothing designated initially.
590 If CHARSETn is a list of character sets, those character sets are
591 designated to Gn on output, but nothing designated to Gn initially.
592 But, character set `ascii' can be designated only to G0.
593 SHORT-FORM non-nil means use short designation sequence on output.
594 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
595 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
596 SPACE on output.
597 SEVEN non-nil means use 7-bit code only on output.
598 LOCKING-SHIFT non-nil means use locking-shift.
599 SINGLE-SHIFT non-nil means use single-shift.
600 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
601 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
602 NO-ISO6429 non-nil means not use ISO6429's direction specification.
603 INIT-BOL non-nil means any designation state is assumed to be reset
604 to initial at each beginning of line on output.
605 DESIGNATION-BOL non-nil means designation sequences should be placed
606 at beginning of line on output.
607 SAFE non-nil means convert unsafe characters to `?' on output.
608 Characters not specified in the property `safe-charsets' nor
609 `safe-chars' are unsafe.
610 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
611 a code specified in `latin-extra-code-table' (which see) as a valid
612 code of the coding system.
613
614 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
615 decoding and encoding. CCL programs should be specified by their
616 symbols.
617
618 PROPERTIES is an alist of properties vs the corresponding values.
619 These properties are set in PLIST, a property list. This function
620 also sets properties `coding-category' and `alias-coding-systems'
621 automatically.
622
623 EOL-TYPE specifies the EOL type of the coding-system in one of the
624 following formats:
625
626 o symbol (unix, dos, or mac)
627
628 The symbol `unix' means Unix-like EOL (LF), `dos' means
629 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
630
631 o number (0, 1, or 2)
632
633 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
634 respectively.
635
636 o vector of coding-systems of length 3
637
638 The EOL type is detected automatically for the coding system.
639 And, according to the detected EOL type, one of the coding
640 systems in the vector is selected. Elements of the vector
641 corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
642 in this order.
643
644 Kludgy features for backward compatibility:
645
646 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
647 treated as a compiled CCL code.
648
649 2. If PROPERTIES is just a list of character sets, the list is set as
650 a value of `safe-charsets' in PLIST."
651 ;; Set a value of `coding-system' property.
652 (let ((coding-spec (make-vector 5 nil))
653 (no-initial-designation t)
654 (no-alternative-designation t)
655 coding-category)
656 (if (or (not (integerp type)) (< type 0) (> type 5))
657 (error "TYPE argument must be 0..5"))
658 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
659 (error "MNEMONIC argument must be an ASCII printable character."))
660 (aset coding-spec coding-spec-type-idx type)
661 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
662 (aset coding-spec coding-spec-doc-string-idx
663 (purecopy (if (stringp doc-string) doc-string "")))
664 (cond ((= type 0)
665 (setq coding-category 'coding-category-emacs-mule))
666 ((= type 1)
667 (setq coding-category 'coding-category-sjis))
668 ((= type 2) ; ISO2022
669 (let ((i 0)
670 (vec (make-vector 32 nil))
671 (g1-designation nil)
672 (fl flags))
673 (while (< i 4)
674 (let ((charset (car fl)))
675 (if (and no-initial-designation
676 (> i 0)
677 (or (charsetp charset)
678 (and (consp charset)
679 (charsetp (car charset)))))
680 (setq no-initial-designation nil))
681 (if (charsetp charset)
682 (if (= i 1) (setq g1-designation charset))
683 (if (consp charset)
684 (let ((tail charset)
685 elt)
686 (while tail
687 (setq elt (car tail))
688 (if (eq elt t)
689 (setq no-alternative-designation nil)
690 (if (and elt (not (charsetp elt)))
691 (error "Invalid charset: %s" elt)))
692 (setq tail (cdr tail)))
693 (setq g1-designation (car charset)))
694 (if charset
695 (if (eq charset t)
696 (setq no-alternative-designation nil)
697 (error "Invalid charset: %s" charset)))))
698 (aset vec i charset))
699 (setq fl (cdr fl) i (1+ i)))
700 (while (and (< i 32) fl)
701 (aset vec i (car fl))
702 (setq fl (cdr fl) i (1+ i)))
703 (aset coding-spec 4 vec)
704 (setq coding-category
705 (if (aref vec 8) ; Use locking-shift.
706 (or (and (aref vec 7) 'coding-category-iso-7-else)
707 'coding-category-iso-8-else)
708 (if (aref vec 7) ; 7-bit only.
709 (if (aref vec 9) ; Use single-shift.
710 'coding-category-iso-7-else
711 (if no-alternative-designation
712 'coding-category-iso-7-tight
713 'coding-category-iso-7))
714 (if (or no-initial-designation
715 (not no-alternative-designation))
716 'coding-category-iso-8-else
717 (if (and (charsetp g1-designation)
718 (= (charset-dimension g1-designation) 2))
719 'coding-category-iso-8-2
720 'coding-category-iso-8-1)))))))
721 ((= type 3)
722 (setq coding-category 'coding-category-big5))
723 ((= type 4) ; private
724 (setq coding-category 'coding-category-ccl)
725 (if (not (consp flags))
726 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
727 (let ((decoder (check-ccl-program
728 (car flags)
729 (intern (format "%s-decoder" coding-system))))
730 (encoder (check-ccl-program
731 (cdr flags)
732 (intern (format "%s-encoder" coding-system)))))
733 (if (and decoder encoder)
734 (aset coding-spec 4 (cons decoder encoder))
735 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
736 (t ; i.e. (= type 5)
737 (setq coding-category 'coding-category-raw-text)))
738
739 (let ((plist (list 'coding-category coding-category
740 'alias-coding-systems (list coding-system))))
741 (if no-initial-designation
742 (plist-put plist 'no-initial-designation t))
743 (if (and properties
744 (or (eq properties t)
745 (not (consp (car properties)))))
746 ;; In the old version, the arg PROPERTIES is a list to be
747 ;; set in PLIST as a value of property `safe-charsets'.
748 (setq properties (list (cons 'safe-charsets properties))))
749 ;; In the current version PROPERTIES is a property list.
750 ;; Reflect it into PLIST one by one while handling safe-chars
751 ;; specially.
752 (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
753 (safe-chars (cdr (assq 'safe-chars properties)))
754 (l properties)
755 prop val)
756 ;; If only safe-charsets is specified, make a char-table from
757 ;; it, and store that char-table as the value of `safe-chars'.
758 (if (and (not safe-chars) safe-charsets)
759 (let (charset)
760 (if (eq safe-charsets t)
761 (setq safe-chars t)
762 (setq safe-chars (make-char-table 'safe-chars))
763 (while safe-charsets
764 (setq charset (car safe-charsets)
765 safe-charsets (cdr safe-charsets))
766 (cond ((eq charset 'ascii)) ; just ignore
767 ((eq charset 'eight-bit-control)
768 (let ((i 128))
769 (while (< i 160)
770 (aset safe-chars i t)
771 (setq i (1+ i)))))
772 ((eq charset 'eight-bit-graphic)
773 (let ((i 160))
774 (while (< i 256)
775 (aset safe-chars i t)
776 (setq i (1+ i)))))
777 (t
778 (aset safe-chars (make-char charset) t)))))
779 (setq l (cons (cons 'safe-chars safe-chars) l))))
780 (while l
781 (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
782 (if (eq prop 'safe-chars)
783 (progn
784 (setq val safe-chars)
785 (register-char-codings coding-system safe-chars)))
786 (plist-put plist prop val)))
787 ;; The property `coding-category' may have been set differently
788 ;; through PROPERTIES.
789 (setq coding-category (plist-get plist 'coding-category))
790 (aset coding-spec coding-spec-plist-idx plist))
791 (put coding-system 'coding-system coding-spec)
792 (put coding-category 'coding-systems
793 (cons coding-system (get coding-category 'coding-systems))))
794
795 ;; Next, set a value of `eol-type' property.
796 (if (not eol-type)
797 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
798 ;; systems, each corresponds to a coding system for the detected
799 ;; EOL format.
800 (setq eol-type (make-subsidiary-coding-system coding-system)))
801 (setq eol-type
802 (cond ((or (eq eol-type 'unix) (null eol-type))
803 0)
804 ((eq eol-type 'dos)
805 1)
806 ((eq eol-type 'mac)
807 2)
808 ((or (and (vectorp eol-type)
809 (= (length eol-type) 3))
810 (and (numberp eol-type)
811 (and (>= eol-type 0)
812 (<= eol-type 2))))
813 eol-type)
814 (t
815 (error "Invalid EOL-TYPE spec:%S" eol-type))))
816 (put coding-system 'eol-type eol-type)
817
818 ;; At last, register CODING-SYSTEM in `coding-system-list' and
819 ;; `coding-system-alist'.
820 (add-to-coding-system-list coding-system)
821 (setq coding-system-alist (cons (list (symbol-name coding-system))
822 coding-system-alist))
823
824 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
825 ;; XXX-with-esc variants.
826 (let ((coding-category (coding-system-category coding-system)))
827 (if (or (eq coding-category 'coding-category-iso-8-1)
828 (eq coding-category 'coding-category-iso-8-2))
829 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
830 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
831 (safe-charsets (assq 'safe-charsets properties))
832 (mime-charset (assq 'mime-charset properties)))
833 (if safe-charsets
834 (setcdr safe-charsets t)
835 (setq properties (cons (cons 'safe-charsets t) properties)))
836 (if mime-charset
837 (setcdr mime-charset nil))
838 (make-coding-system esc type mnemonic doc
839 (if (listp (car flags))
840 (cons (append (car flags) '(t)) (cdr flags))
841 (cons (list (car flags) t) (cdr flags)))
842 properties))))
843
844 coding-system)
845
846 (defun define-coding-system-alias (alias coding-system)
847 "Define ALIAS as an alias for coding system CODING-SYSTEM."
848 (put alias 'coding-system (coding-system-spec coding-system))
849 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
850 (add-to-coding-system-list alias)
851 (setq coding-system-alist (cons (list (symbol-name alias))
852 coding-system-alist))
853 (let ((eol-type (coding-system-eol-type coding-system)))
854 (if (vectorp eol-type)
855 (put alias 'eol-type (make-subsidiary-coding-system alias))
856 (put alias 'eol-type eol-type))))
857
858 (defun set-buffer-file-coding-system (coding-system &optional force)
859 "Set the file coding-system of the current buffer to CODING-SYSTEM.
860 This means that when you save the buffer, it will be converted
861 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
862 use \\[list-coding-systems].
863
864 If the buffer's previous file coding-system value specifies end-of-line
865 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
866 merged with the already-specified end-of-line conversion.
867
868 If the buffer's previous file coding-system value specifies text
869 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
870 merged with the already-specified text conversion.
871
872 However, if the optional prefix argument FORCE is non-nil, then
873 CODING-SYSTEM is used exactly as specified.
874
875 This marks the buffer modified so that the succeeding \\[save-buffer]
876 surely saves the buffer with CODING-SYSTEM. From a program, if you
877 don't want to mark the buffer modified, just set the variable
878 `buffer-file-coding-system' directly."
879 (interactive "zCoding system for visited file (default, nil): \nP")
880 (check-coding-system coding-system)
881 (if (and coding-system buffer-file-coding-system (null force))
882 (let ((base (coding-system-base buffer-file-coding-system))
883 (eol (coding-system-eol-type buffer-file-coding-system)))
884 ;; If CODING-SYSTEM doesn't specify text conversion, merge
885 ;; with that of buffer-file-coding-system.
886 (if (eq (coding-system-base coding-system) 'undecided)
887 (setq coding-system (coding-system-change-text-conversion
888 coding-system base)))
889 ;; If CODING-SYSTEM doesn't specify eol conversion, merge with
890 ;; that of buffer-file-coding-system.
891 (if (and (vectorp (coding-system-eol-type coding-system))
892 (numberp eol) (>= eol 0) (<= eol 2))
893 (setq coding-system (coding-system-change-eol-conversion
894 coding-system eol)))))
895 (setq buffer-file-coding-system coding-system)
896 (set-buffer-modified-p t)
897 (force-mode-line-update))
898
899 (defvar default-terminal-coding-system nil
900 "Default value for the terminal coding system.
901 This is normally set according to the selected language environment.
902 See also the command `set-terminal-coding-system'.")
903
904 (defun set-terminal-coding-system (coding-system)
905 "Set coding system of your terminal to CODING-SYSTEM.
906 All text output to the terminal will be encoded
907 with the specified coding system.
908 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
909 The default is determined by the selected language environment
910 or by the previous use of this command."
911 (interactive
912 (list (let ((default (if (and (not (terminal-coding-system))
913 default-terminal-coding-system)
914 default-terminal-coding-system)))
915 (read-coding-system
916 (format "Coding system for terminal display (default, %s): "
917 default)
918 default))))
919 (if (and (not coding-system)
920 (not (terminal-coding-system)))
921 (setq coding-system default-terminal-coding-system))
922 (if coding-system
923 (setq default-terminal-coding-system coding-system))
924 (set-terminal-coding-system-internal coding-system)
925 (redraw-frame (selected-frame)))
926
927 (defvar default-keyboard-coding-system nil
928 "Default value of the keyboard coding system.
929 This is normally set according to the selected language environment.
930 See also the command `set-keyboard-coding-system'.")
931
932 (defun set-keyboard-coding-system (coding-system)
933 "Set coding system for keyboard input to CODING-SYSTEM.
934 In addition, this command enables Encoded-kbd minor mode.
935 \(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
936 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
937 The default is determined by the selected language environment
938 or by the previous use of this command."
939 (interactive
940 (list (let ((default (if (and (not (keyboard-coding-system))
941 default-keyboard-coding-system)
942 default-keyboard-coding-system)))
943 (read-coding-system
944 (format "Coding system for keyboard input (default, %s): "
945 default)
946 default))))
947 (if (and (not coding-system)
948 (not (keyboard-coding-system)))
949 (setq coding-system default-keyboard-coding-system))
950 (if coding-system
951 (setq default-keyboard-coding-system coding-system))
952 (set-keyboard-coding-system-internal coding-system)
953 (encoded-kbd-mode (if coding-system 1 0)))
954
955 (defun set-buffer-process-coding-system (decoding encoding)
956 "Set coding systems for the process associated with the current buffer.
957 DECODING is the coding system to be used to decode input from the process,
958 ENCODING is the coding system to be used to encode output to the process.
959
960 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
961 (interactive
962 "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
963 (let ((proc (get-buffer-process (current-buffer))))
964 (if (null proc)
965 (error "no process")
966 (check-coding-system decoding)
967 (check-coding-system encoding)
968 (set-process-coding-system proc decoding encoding)))
969 (force-mode-line-update))
970
971 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
972
973 (defun set-selection-coding-system (coding-system)
974 "Make CODING-SYSTEM used for communicating with other X clients .
975 When sending or receiving text via cut_buffer, selection, and clipboard,
976 the text is encoded or decoded by CODING-SYSTEM."
977 (interactive "zCoding system for X selection: ")
978 (check-coding-system coding-system)
979 (setq selection-coding-system coding-system))
980
981 ;; Coding system lastly specified by the command
982 ;; set-next-selection-coding-system.
983 (defvar last-next-selection-coding-system nil)
984
985 (defun set-next-selection-coding-system (coding-system)
986 "Make CODING-SYSTEM used for the next communication with other X clients.
987 This setting is effective for the next communication only."
988 (interactive
989 (list (read-coding-system
990 (if last-next-selection-coding-system
991 (format "Coding system for the next X selection (default, %S): "
992 last-next-selection-coding-system)
993 "Coding system for the next X selection: ")
994 last-next-selection-coding-system)))
995 (if coding-system
996 (setq last-next-selection-coding-system coding-system)
997 (setq coding-system last-next-selection-coding-system))
998 (check-coding-system coding-system)
999
1000 (setq next-selection-coding-system coding-system))
1001
1002 (defun set-coding-priority (arg)
1003 "Set priority of coding categories according to LIST.
1004 LIST is a list of coding categories ordered by priority."
1005 (let ((l arg)
1006 (current-list (copy-sequence coding-category-list)))
1007 ;; Check the validity of ARG while deleting coding categories in
1008 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
1009 ;; contains all coding categories.
1010 (while l
1011 (if (or (null (get (car l) 'coding-category-index))
1012 (null (memq (car l) current-list)))
1013 (error "Invalid or duplicated element in argument: %s" arg))
1014 (setq current-list (delq (car l) current-list))
1015 (setq l (cdr l)))
1016 ;; Update `coding-category-list' and return it.
1017 (setq coding-category-list (append arg current-list))
1018 (set-coding-priority-internal)))
1019
1020 ;;; FILE I/O
1021
1022 (defvar auto-coding-alist
1023 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
1024 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
1025 "Alist of filename patterns vs corresponding coding systems.
1026 Each element looks like (REGEXP . CODING-SYSTEM).
1027 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
1028
1029 The settings in this alist take priority over `coding:' tags
1030 in the file (see the function `set-auto-coding')
1031 and the contents of `file-coding-system-alist'.")
1032
1033 (defvar set-auto-coding-for-load nil
1034 "Non-nil means look for `load-coding' property instead of `coding'.
1035 This is used for loading and byte-compiling Emacs Lisp files.")
1036
1037 (defun auto-coding-alist-lookup (filename)
1038 "Return the coding system specified by `auto-coding-alist' for FILENAME."
1039 (let ((alist auto-coding-alist)
1040 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
1041 coding-system)
1042 (while (and alist (not coding-system))
1043 (if (string-match (car (car alist)) filename)
1044 (setq coding-system (cdr (car alist)))
1045 (setq alist (cdr alist))))
1046 coding-system))
1047
1048 (defun set-auto-coding (filename size)
1049 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1050 These bytes should include at least the first 1k of the file
1051 and the last 3k of the file, but the middle may be omitted.
1052
1053 It checks FILENAME against the variable `auto-coding-alist'.
1054 If FILENAME doesn't match any entries in the variable,
1055 it checks for a `coding:' tag in the first one or two lines following
1056 point. If no `coding:' tag is found, it checks for local variables
1057 list in the last 3K bytes out of the SIZE bytes.
1058
1059 The return value is the specified coding system,
1060 or nil if nothing specified.
1061
1062 The variable `set-auto-coding-function' (which see) is set to this
1063 function by default."
1064 (let ((coding-system (auto-coding-alist-lookup filename)))
1065
1066 (or coding-system
1067 (let* ((case-fold-search t)
1068 (head-start (point))
1069 (head-end (+ head-start (min size 1024)))
1070 (tail-start (+ head-start (max (- size 3072) 0)))
1071 (tail-end (+ head-start size))
1072 coding-system head-found tail-found pos)
1073 ;; Try a short cut by searching for the string "coding:"
1074 ;; and for "unibyte:" at the head and tail of SIZE bytes.
1075 (setq head-found (or (search-forward "coding:" head-end t)
1076 (search-forward "unibyte:" head-end t)))
1077 (if (and head-found (> head-found tail-start))
1078 ;; Head and tail are overlapped.
1079 (setq tail-found head-found)
1080 (goto-char tail-start)
1081 (setq tail-found (or (search-forward "coding:" tail-end t)
1082 (search-forward "unibyte:" tail-end t))))
1083
1084 ;; At first check the head.
1085 (when head-found
1086 (goto-char head-start)
1087 (setq pos (re-search-forward "[\n\r]" head-end t))
1088 (if (and pos
1089 (= (char-after head-start) ?#)
1090 (= (char-after (1+ head-start)) ?!))
1091 ;; If the file begins with "#!" (exec interpreter magic),
1092 ;; look for coding frobs in the first two lines. You cannot
1093 ;; necessarily put them in the first line of such a file
1094 ;; without screwing up the interpreter invocation.
1095 (setq pos (search-forward "\n" head-end t)))
1096 (if pos (setq head-end pos))
1097 (when (< head-found head-end)
1098 (goto-char head-start)
1099 (when (and set-auto-coding-for-load
1100 (re-search-forward
1101 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
1102 head-end t))
1103 (setq coding-system 'raw-text))
1104 (when (and (not coding-system)
1105 (re-search-forward
1106 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
1107 head-end t))
1108 (setq coding-system (intern (match-string 2)))
1109 (or (coding-system-p coding-system)
1110 (setq coding-system nil)))))
1111
1112 ;; If no coding: tag in the head, check the tail.
1113 (when (and tail-found (not coding-system))
1114 (goto-char tail-start)
1115 (search-forward "\n\^L" nil t)
1116 (if (re-search-forward
1117 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
1118 ;; The prefix is what comes before "local variables:" in its
1119 ;; line. The suffix is what comes after "local variables:"
1120 ;; in its line.
1121 (let* ((prefix (regexp-quote (match-string 1)))
1122 (suffix (regexp-quote (match-string 2)))
1123 (re-coding
1124 (concat
1125 "^" prefix
1126 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1127 suffix "$"))
1128 (re-unibyte
1129 (concat
1130 "^" prefix
1131 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1132 suffix "$"))
1133 (re-end
1134 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
1135 (pos (point)))
1136 (re-search-forward re-end tail-end 'move)
1137 (setq tail-end (point))
1138 (goto-char pos)
1139 (when (and set-auto-coding-for-load
1140 (re-search-forward re-unibyte tail-end t))
1141 (setq coding-system 'raw-text))
1142 (when (and (not coding-system)
1143 (re-search-forward re-coding tail-end t))
1144 (setq coding-system (intern (match-string 1)))
1145 (or (coding-system-p coding-system)
1146 (setq coding-system nil))))))
1147 coding-system))))
1148
1149 (setq set-auto-coding-function 'set-auto-coding)
1150
1151 ;; Set buffer-file-coding-system of the current buffer after some text
1152 ;; is inserted.
1153 (defun after-insert-file-set-buffer-file-coding-system (inserted)
1154 (if last-coding-system-used
1155 (let ((coding-system
1156 (find-new-buffer-file-coding-system last-coding-system-used))
1157 (modified-p (buffer-modified-p)))
1158 (when coding-system
1159 (set-buffer-file-coding-system coding-system t)
1160 (if (and enable-multibyte-characters
1161 (or (eq coding-system 'no-conversion)
1162 (eq (coding-system-type coding-system) 5))
1163 ;; If buffer was unmodified and the size is the
1164 ;; same as INSERTED, we must be visiting it.
1165 (not modified-p)
1166 (= (buffer-size) inserted))
1167 ;; For coding systems no-conversion and raw-text...,
1168 ;; edit the buffer as unibyte.
1169 (let ((pos-byte (position-bytes (+ (point) inserted))))
1170 (set-buffer-multibyte nil)
1171 (setq inserted (- pos-byte (position-bytes (point))))))
1172 (set-buffer-modified-p modified-p))))
1173 inserted)
1174
1175 (add-hook 'after-insert-file-functions
1176 'after-insert-file-set-buffer-file-coding-system)
1177
1178 ;; The coding-spec and eol-type of coding-system returned is decided
1179 ;; independently in the following order.
1180 ;; 1. That of buffer-file-coding-system locally bound.
1181 ;; 2. That of CODING.
1182
1183 (defun find-new-buffer-file-coding-system (coding)
1184 "Return a coding system for a buffer when a file of CODING is inserted.
1185 The local variable `buffer-file-coding-system' of the current buffer
1186 is set to the returned value.
1187 Return nil if there's no need to set `buffer-file-coding-system'."
1188 (let (local-coding local-eol
1189 found-coding found-eol
1190 new-coding new-eol)
1191 (if (null coding)
1192 ;; Nothing found about coding.
1193 nil
1194
1195 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1196 ;; and LOCAL-CODING.
1197 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1198 (if (null (numberp local-eol))
1199 ;; But eol-type is not yet set.
1200 (setq local-eol nil))
1201 (if (and buffer-file-coding-system
1202 (not (eq (coding-system-type buffer-file-coding-system) t)))
1203 ;; This is not `undecided'.
1204 (setq local-coding (coding-system-base buffer-file-coding-system)))
1205
1206 (if (and (local-variable-p 'buffer-file-coding-system)
1207 local-eol local-coding)
1208 ;; The current buffer has already set full coding-system, we
1209 ;; had better not change it.
1210 nil
1211
1212 (setq found-eol (coding-system-eol-type coding))
1213 (if (null (numberp found-eol))
1214 ;; But eol-type is not found.
1215 ;; If EOL conversions are inhibited, force unix eol-type.
1216 (setq found-eol (if inhibit-eol-conversion 0)))
1217 (if (eq (coding-system-type coding) t)
1218 (setq found-coding 'undecided)
1219 (setq found-coding (coding-system-base coding)))
1220
1221 (if (and (not found-eol) (eq found-coding 'undecided))
1222 ;; No valid coding information found.
1223 nil
1224
1225 ;; Some coding information (eol or text) found.
1226
1227 ;; The local setting takes precedence over the found one.
1228 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1229 (or local-coding found-coding)
1230 (or found-coding local-coding)))
1231 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1232 (or local-eol found-eol)
1233 (or found-eol local-eol)))
1234
1235 (let ((eol-type (coding-system-eol-type new-coding)))
1236 (if (and (numberp new-eol) (vectorp eol-type))
1237 (aref eol-type new-eol)
1238 new-coding)))))))
1239
1240 (defun modify-coding-system-alist (target-type regexp coding-system)
1241 "Modify one of look up tables for finding a coding system on I/O operation.
1242 There are three of such tables, `file-coding-system-alist',
1243 `process-coding-system-alist', and `network-coding-system-alist'.
1244
1245 TARGET-TYPE specifies which of them to modify.
1246 If it is `file', it affects `file-coding-system-alist' (which see).
1247 If it is `process', it affects `process-coding-system-alist' (which see).
1248 If it is `network', it affects `network-coding-system-alist' (which see).
1249
1250 REGEXP is a regular expression matching a target of I/O operation.
1251 The target is a file name if TARGET-TYPE is `file', a program name if
1252 TARGET-TYPE is `process', or a network service name or a port number
1253 to connect to if TARGET-TYPE is `network'.
1254
1255 CODING-SYSTEM is a coding system to perform code conversion on the I/O
1256 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1257 for decoding and encoding respectively,
1258 or a function symbol which, when called, returns such a cons cell."
1259 (or (memq target-type '(file process network))
1260 (error "Invalid target type: %s" target-type))
1261 (or (stringp regexp)
1262 (and (eq target-type 'network) (integerp regexp))
1263 (error "Invalid regular expression: %s" regexp))
1264 (if (symbolp coding-system)
1265 (if (not (fboundp coding-system))
1266 (progn
1267 (check-coding-system coding-system)
1268 (setq coding-system (cons coding-system coding-system))))
1269 (check-coding-system (car coding-system))
1270 (check-coding-system (cdr coding-system)))
1271 (cond ((eq target-type 'file)
1272 (let ((slot (assoc regexp file-coding-system-alist)))
1273 (if slot
1274 (setcdr slot coding-system)
1275 (setq file-coding-system-alist
1276 (cons (cons regexp coding-system)
1277 file-coding-system-alist)))))
1278 ((eq target-type 'process)
1279 (let ((slot (assoc regexp process-coding-system-alist)))
1280 (if slot
1281 (setcdr slot coding-system)
1282 (setq process-coding-system-alist
1283 (cons (cons regexp coding-system)
1284 process-coding-system-alist)))))
1285 (t
1286 (let ((slot (assoc regexp network-coding-system-alist)))
1287 (if slot
1288 (setcdr slot coding-system)
1289 (setq network-coding-system-alist
1290 (cons (cons regexp coding-system)
1291 network-coding-system-alist)))))))
1292
1293 (defun make-translation-table (&rest args)
1294 "Make a translation table (char table) from arguments.
1295 Each argument is a list of the form (FROM . TO),
1296 where FROM is a character to be translated to TO.
1297
1298 FROM can be a generic character (see `make-char'). In this case, TO is
1299 a generic character containing the same number of characters, or a
1300 ordinary character. If FROM and TO are both generic characters, all
1301 characters belonging to FROM are translated to characters belonging to TO
1302 without changing their position code(s)."
1303 (let ((table (make-char-table 'translation-table))
1304 revlist)
1305 (while args
1306 (let ((elts (car args)))
1307 (while elts
1308 (let* ((from (car (car elts)))
1309 (from-i 0) ; degree of freedom of FROM
1310 (from-rev (nreverse (split-char from)))
1311 (to (cdr (car elts)))
1312 (to-i 0) ; degree of freedom of TO
1313 (to-rev (nreverse (split-char to))))
1314 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1315 (while (eq (car from-rev) 0)
1316 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1317 (while (eq (car to-rev) 0)
1318 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1319 (if (and (/= from-i to-i) (/= to-i 0))
1320 (error "Invalid character pair (%d . %d)" from to))
1321 ;; If we have already translated TO to TO-ALT, FROM should
1322 ;; also be translated to TO-ALT. But, this is only if TO
1323 ;; is a generic character or TO-ALT is not a generic
1324 ;; character.
1325 (let ((to-alt (aref table to)))
1326 (if (and to-alt
1327 (or (> to-i 0) (not (generic-char-p to-alt))))
1328 (setq to to-alt)))
1329 (if (> from-i 0)
1330 (set-char-table-default table from to)
1331 (aset table from to))
1332 ;; If we have already translated some chars to FROM, they
1333 ;; should also be translated to TO.
1334 (let ((l (assq from revlist)))
1335 (if l
1336 (let ((ch (car l)))
1337 (setcar l to)
1338 (setq l (cdr l))
1339 (while l
1340 (aset table ch to)
1341 (setq l (cdr l)) ))))
1342 ;; Now update REVLIST.
1343 (let ((l (assq to revlist)))
1344 (if l
1345 (setcdr l (cons from (cdr l)))
1346 (setq revlist (cons (list to from) revlist)))))
1347 (setq elts (cdr elts))))
1348 (setq args (cdr args)))
1349 ;; Return TABLE just created.
1350 table))
1351
1352 (defun make-translation-table-from-vector (vec)
1353 "Make translation table from decoding vector VEC.
1354 VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1355 See also the variable `nonascii-translation-table'."
1356 (let ((table (make-char-table 'translation-table))
1357 (rev-table (make-char-table 'translation-table))
1358 (i 0)
1359 ch)
1360 (while (< i 256)
1361 (setq ch (aref vec i))
1362 (aset table i ch)
1363 (if (>= ch 256)
1364 (aset rev-table ch i))
1365 (setq i (1+ i)))
1366 (set-char-table-extra-slot table 0 rev-table)
1367 table))
1368
1369 (defun define-translation-table (symbol &rest args)
1370 "Define SYMBOL as a name of translation table made by ARGS.
1371
1372 If the first element of ARGS is a char-table of which purpose is
1373 translation-table, just define SYMBOL as the name of it.
1374
1375 In the other case, ARGS are the same as arguments to the function
1376 `make-translation-table' (which see).
1377
1378 This function sets properties `translation-table' and
1379 `translation-table-id' of SYMBOL to the created table itself and
1380 identification number of the table respectively."
1381 (let ((table (if (and (char-table-p (car args))
1382 (eq (char-table-subtype (car args))
1383 'translation-table))
1384 (car args)
1385 (apply 'make-translation-table args)))
1386 (len (length translation-table-vector))
1387 (id 0)
1388 (done nil))
1389 (put symbol 'translation-table table)
1390 (while (not done)
1391 (if (>= id len)
1392 (setq translation-table-vector
1393 (vconcat translation-table-vector (make-vector len nil))))
1394 (let ((slot (aref translation-table-vector id)))
1395 (if (or (not slot)
1396 (eq (car slot) symbol))
1397 (progn
1398 (aset translation-table-vector id (cons symbol table))
1399 (setq done t))
1400 (setq id (1+ id)))))
1401 (put symbol 'translation-table-id id)
1402 id))
1403
1404 (put 'with-category-table 'lisp-indent-function 1)
1405
1406 (defmacro with-category-table (category-table &rest body)
1407 `(let ((current-category-table (category-table)))
1408 (set-category-table ,category-table)
1409 (unwind-protect
1410 (progn ,@body)
1411 (set-category-table current-category-table))))
1412
1413 ;;; Initialize some variables.
1414
1415 (put 'use-default-ascent 'char-table-extra-slots 0)
1416 (setq use-default-ascent (make-char-table 'use-default-ascent))
1417 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1418 (setq ignore-relative-composition
1419 (make-char-table 'ignore-relative-composition))
1420
1421 ;;;
1422 (provide 'mule)
1423
1424 ;;; mule.el ends here