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