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