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