Commit | Line | Data |
---|---|---|
07513d64 | 1 | ;;; mule.el --- basic commands for multilingual environment |
4ed46869 | 2 | |
4ed46869 | 3 | ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. |
58cfed09 | 4 | ;; Licensed to the Free Software Foundation. |
4e4a5bca | 5 | ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
c1841772 KH |
6 | ;; Copyright (C) 2001, 2002 |
7 | ;; National Institute of Advanced Industrial Science and Technology (AIST) | |
8 | ;; Registration Number H13PRO009 | |
4ed46869 KH |
9 | |
10 | ;; Keywords: mule, multilingual, character set, coding system | |
11 | ||
12 | ;; This file is part of GNU Emacs. | |
13 | ||
14 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
16 | ;; the Free Software Foundation; either version 2, or (at your option) | |
17 | ;; any later version. | |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
369314dc KH |
25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
26 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 | ;; Boston, MA 02111-1307, USA. | |
4ed46869 | 28 | |
60370d40 PJ |
29 | ;;; Commentary: |
30 | ||
4ed46869 KH |
31 | ;;; Code: |
32 | ||
c1841772 | 33 | (defconst mule-version "7.0 (SAKAKI)" "\ |
4ed46869 KH |
34 | Version number and name of this version of MULE (multilingual environment).") |
35 | ||
c1841772 | 36 | (defconst mule-version-date "2002.2.28" "\ |
4ed46869 KH |
37 | Distribution date of this version of MULE (multilingual environment).") |
38 | ||
c1841772 KH |
39 | |
40 | \f | |
41 | ;;; CHARACTER | |
42 | (defalias 'char-valid-p 'characterp) | |
43 | (make-obsolete 'char-valid-p 'characterp "22.1") | |
44 | ||
45 | \f | |
46 | ;;; CHARSET | |
47 | ||
48 | (defun define-charset (name docstring &rest props) | |
49 | "Define NAME (symbol) as a charset with DOCSTRING. | |
50 | The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE | |
07513d64 | 51 | may be any symbol. The following have special meanings, and one of |
bec25acc | 52 | `:code-offset', `:map', `:subset', `:superset' must be specified. |
c1841772 KH |
53 | |
54 | `:short-name' | |
55 | ||
56 | VALUE must be a short string to identify the charset. If omitted, | |
57 | NAME is used. | |
58 | ||
59 | `:long-name' | |
60 | ||
61 | VALUE must be a string longer than `:short-name' to identify the | |
07513d64 | 62 | charset. If omitted, the value of the `:short-name' attribute is used. |
c1841772 KH |
63 | |
64 | `:dimension' | |
65 | ||
66 | VALUE must be an integer 0, 1, 2, or 3, specifying the dimension of | |
07513d64 DL |
67 | code-points of the charsets. If omitted, it is calculated from the |
68 | value of the `:code-space' attribute. | |
c1841772 KH |
69 | |
70 | `:code-space' | |
71 | ||
72 | VALUE must be a vector of length at most 8 specifying the byte code | |
73 | range of each dimension in this format: | |
74 | [ MIN-1 MAX-1 MIN-2 MAX-2 ... ] | |
07513d64 | 75 | where MIN-N is the minimum byte value of Nth dimension of code-point, |
c1841772 KH |
76 | MAX-N is the maximum byte value of that. |
77 | ||
b1a79461 KH |
78 | `:min-code' |
79 | ||
80 | VALUE must be an integer specifying the mininum code point of the | |
81 | charset. If omitted, it is calculated from `:code-space'. VALUE may | |
82 | be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of | |
83 | the code point and LOW is the least significant 16 bits. | |
84 | ||
85 | `max-code' | |
86 | ||
87 | VALUE must be an integer specifying the maxinum code point of the | |
88 | charset. If omitted, it is calculated from `:code-space'. VALUE may | |
89 | be a cons (HIGH . LOW), where HIGH is the most significant 16 bits of | |
90 | the code point and LOW is the least significant 16 bits. | |
91 | ||
c1841772 KH |
92 | `:iso-final-char' |
93 | ||
94 | VALUE must be a character in the range 32 to 127 (inclusive) | |
95 | specifying the final char of the charset for ISO-2022 encoding. If | |
96 | omitted, the charset can't be encoded by ISO-2022 based | |
97 | coding-systems. | |
98 | ||
99 | `:iso-revision-number' | |
100 | ||
101 | VALUE must be an integer in the range 0..63, specifying the revision | |
102 | number of the charset for ISO-2022 encoding. | |
103 | ||
104 | `:emacs-mule-id' | |
105 | ||
106 | VALUE must be an integer of 0, 128..255. If omitted, the charset | |
107 | can't be encoded by coding-systems of type `emacs-mule'. | |
108 | ||
109 | `:ascii-compatible-p' | |
110 | ||
07513d64 DL |
111 | VALUE must be nil or t (default nil). If VALUE is t, the charset is |
112 | compatible with ASCII, i.e. the first 128 code points map to ASCII. | |
c1841772 KH |
113 | |
114 | `:supplementary-p' | |
115 | ||
116 | VALUE must be nil or t. If the VALUE is t, the charset is | |
07513d64 DL |
117 | supplementary, which means it is used only as a parent of some other |
118 | charset. | |
c1841772 KH |
119 | |
120 | `:invalid-code' | |
121 | ||
122 | VALUE must be a nonnegative integer that can be used as an invalid | |
123 | code point of the charset. If the minimum code is 0 and the maximum | |
124 | code is greater than Emacs' maximum integer value, `:invalid-code' | |
125 | should not be omitted. | |
126 | ||
127 | `:code-offset' | |
128 | ||
07513d64 DL |
129 | VALUE must be an integer added to the index number of a character to |
130 | get the corresponding character code. | |
c1841772 KH |
131 | |
132 | `:map' | |
133 | ||
134 | VALUE must be vector or string. | |
135 | ||
136 | If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ], | |
137 | where CODE-n is a code-point of the charset, and CHAR-n is the | |
07513d64 | 138 | corresponding character code. |
c1841772 KH |
139 | |
140 | If it is a string, it is a name of file that contains the above | |
3e4abc9e KH |
141 | information. Each line of the file must be this format: |
142 | 0xXXX 0xYYY | |
143 | where XXX is a hexadecimal representation of CODE-n and YYY is a | |
144 | hexadecimal representation of CHAR-n. A line starting with `#' is a | |
145 | comment line. | |
c1841772 | 146 | |
2c2a254f KH |
147 | `:subset' |
148 | ||
149 | VALUE must be a list: | |
150 | ( PARENT MIN-CODE MAX-CODE OFFSET ) | |
151 | PARENT is a parent charset. MIN-CODE and MAX-CODE specify the range | |
152 | of characters inherited from the parent. OFFSET is an integer value | |
153 | to add to a code point of the parent charset to get the corresponding | |
154 | code point of this charset. | |
155 | ||
156 | `:superset' | |
c1841772 KH |
157 | |
158 | VALUE must be a list of parent charsets. The charset inherits | |
159 | characters from them. Each element of the list may be a cons (PARENT | |
160 | . OFFSET), where PARENT is a parent charset, and OFFSET is an offset | |
2c2a254f KH |
161 | value to add to a code point of PARENT to get the corresponding code |
162 | point of this charset. | |
c1841772 KH |
163 | |
164 | `:unify-map' | |
165 | ||
166 | VALUE must be vector or string. | |
167 | ||
168 | If it is a vector, the format is [ CODE-1 CHAR-1 CODE-2 CHAR-2 ... ], | |
169 | where CODE-n is a code-point of the charset, and CHAR-n is the | |
07513d64 | 170 | corresponding Unicode character code. |
c1841772 KH |
171 | |
172 | If it is a string, it is a name of file that contains the above | |
3e4abc9e KH |
173 | information. The file format is the same as what described for `:map' |
174 | attribute." | |
c1841772 KH |
175 | (let ((attrs (mapcar 'list '(:dimension |
176 | :code-space | |
b1a79461 KH |
177 | :min-code |
178 | :max-code | |
c1841772 KH |
179 | :iso-final-char |
180 | :iso-revision-number | |
181 | :emacs-mule-id | |
182 | :ascii-compatible-p | |
183 | :supplementary-p | |
184 | :invalid-code | |
185 | :code-offset | |
186 | :map | |
2c2a254f KH |
187 | :subset |
188 | :superset | |
c1841772 KH |
189 | :unify-map |
190 | :plist)))) | |
191 | ||
192 | ;; If :dimension is omitted, get the dimension from :code-space. | |
193 | (let ((dimension (plist-get props :dimension))) | |
194 | (or dimension | |
195 | (progn | |
196 | (setq dimension (/ (length (plist-get props :code-space)) 2)) | |
197 | (setq props (plist-put props :dimension dimension))))) | |
198 | ||
199 | (dolist (slot attrs) | |
200 | (setcdr slot (plist-get props (car slot)))) | |
201 | ||
202 | ;; Make sure that the value of :code-space is a vector of 8 | |
203 | ;; elements. | |
204 | (let* ((slot (assq :code-space attrs)) | |
205 | (val (cdr slot)) | |
206 | (len (length val))) | |
207 | (if (< len 8) | |
208 | (setcdr slot | |
209 | (vconcat val (make-vector (- 8 len) 0))))) | |
210 | ||
211 | ;; Add :name and :docstring properties to PROPS. | |
212 | (setq props | |
213 | (cons :name (cons name (cons :docstring (cons docstring props))))) | |
214 | (or (plist-get props :short-name) | |
215 | (plist-put props :short-name (symbol-name name))) | |
216 | (or (plist-get props :long-name) | |
217 | (plist-put props :long-name (plist-get props :short-name))) | |
e1e529fa DL |
218 | ;; We can probably get a worthwhile amount in purespace. |
219 | (setq props | |
220 | (mapcar (lambda (elt) | |
221 | (if (stringp elt) | |
222 | (purecopy elt) | |
223 | elt)) | |
224 | props)) | |
c1841772 KH |
225 | (setcdr (assq :plist attrs) props) |
226 | ||
227 | (apply 'define-charset-internal name (mapcar 'cdr attrs)))) | |
228 | ||
229 | ||
4ed46869 | 230 | (defun load-with-code-conversion (fullname file &optional noerror nomessage) |
0f69cb38 KH |
231 | "Execute a file of Lisp code named FILE whose absolute name is FULLNAME. |
232 | The file contents are decoded before evaluation if necessary. | |
4ed46869 KH |
233 | If optional second arg NOERROR is non-nil, |
234 | report no error if FILE doesn't exist. | |
235 | Print messages at start and end of loading unless | |
236 | optional third arg NOMESSAGE is non-nil. | |
237 | Return t if file exists." | |
238 | (if (null (file-readable-p fullname)) | |
239 | (and (null noerror) | |
240 | (signal 'file-error (list "Cannot open load file" file))) | |
241 | ;; Read file with code conversion, and then eval. | |
242 | (let* ((buffer | |
243 | ;; To avoid any autoloading, set default-major-mode to | |
244 | ;; fundamental-mode. | |
88162676 RS |
245 | ;; So that we don't get completely screwed if the |
246 | ;; file is encoded in some complicated character set, | |
247 | ;; read it with real decoding, as a multibyte buffer, | |
248 | ;; even if this is a --unibyte Emacs session. | |
249 | (let ((default-major-mode 'fundamental-mode) | |
250 | (default-enable-multibyte-characters t)) | |
4ed46869 KH |
251 | ;; We can't use `generate-new-buffer' because files.el |
252 | ;; is not yet loaded. | |
253 | (get-buffer-create (generate-new-buffer-name " *load*")))) | |
db5cae4b SM |
254 | (load-in-progress t) |
255 | (source (save-match-data (string-match "\\.el\\'" fullname)))) | |
256 | (unless nomessage | |
257 | (if source | |
258 | (message "Loading %s (source)..." file) | |
259 | (message "Loading %s..." file))) | |
260 | (when purify-flag | |
261 | (setq preloaded-file-list (cons file preloaded-file-list))) | |
4ed46869 | 262 | (unwind-protect |
a6acd8a2 | 263 | (let ((load-file-name fullname) |
1c4cc63a | 264 | (set-auto-coding-for-load t) |
a6acd8a2 | 265 | (inhibit-file-name-operation nil)) |
4ed46869 KH |
266 | (save-excursion |
267 | (set-buffer buffer) | |
268 | (insert-file-contents fullname) | |
7d276780 EZ |
269 | ;; If the loaded file was inserted with no-conversion or |
270 | ;; raw-text coding system, make the buffer unibyte. | |
271 | ;; Otherwise, eval-buffer might try to interpret random | |
272 | ;; binary junk as multibyte characters. | |
273 | (if (and enable-multibyte-characters | |
513dcb0a DL |
274 | (eq (coding-system-type last-coding-system-used) |
275 | 'raw-text)) | |
7d276780 | 276 | (set-buffer-multibyte nil)) |
4ed46869 KH |
277 | ;; Make `kill-buffer' quiet. |
278 | (set-buffer-modified-p nil)) | |
0f69cb38 | 279 | ;; Have the original buffer current while we eval. |
88162676 RS |
280 | (eval-buffer buffer nil file |
281 | ;; If this Emacs is running with --unibyte, | |
282 | ;; convert multibyte strings to unibyte | |
283 | ;; after reading them. | |
ba74e833 | 284 | ;; (not default-enable-multibyte-characters) |
8dd08b5b | 285 | nil t |
ba74e833 | 286 | )) |
cfc70cdf RS |
287 | (let (kill-buffer-hook kill-buffer-query-functions) |
288 | (kill-buffer buffer))) | |
4ed46869 | 289 | (let ((hook (assoc file after-load-alist))) |
db5cae4b SM |
290 | (when hook |
291 | (mapcar (function eval) (cdr hook)))) | |
292 | (unless (or nomessage noninteractive) | |
293 | (if source | |
294 | (message "Loading %s (source)...done" file) | |
295 | (message "Loading %s...done" file))) | |
4ed46869 KH |
296 | t))) |
297 | ||
298 | ;; API (Application Program Interface) for charsets. | |
299 | ||
c1841772 KH |
300 | ;;; Charset property |
301 | ||
302 | (defun get-charset-property (charset propname) | |
303 | "Return the value of CHARSET's PROPNAME property. | |
304 | This is the last value stored with | |
305 | (put-charset-property CHARSET PROPNAME VALUE)." | |
306 | (plist-get (charset-plist charset) propname)) | |
307 | ||
308 | (defun put-charset-property (charset propname value) | |
309 | "Store CHARSETS's PROPNAME property with value VALUE. | |
310 | It can be retrieved with `(get-charset-property CHARSET PROPNAME)'." | |
311 | (set-charset-plist charset | |
312 | (plist-put (charset-plist charset) propname value))) | |
313 | ||
314 | ||
315 | (defun charset-description (charset) | |
316 | "Return description string of CHARSET." | |
317 | (plist-get (charset-plist charset) :docstring)) | |
318 | ||
319 | (defun charset-dimension (charset) | |
12504f57 | 320 | "Return dimension of CHARSET." |
c1841772 KH |
321 | (plist-get (charset-plist charset) :dimension)) |
322 | ||
346a8d64 | 323 | (defun charset-chars (charset &optional dimension) |
12504f57 | 324 | "Return number of characters contained in DIMENSION of CHARSET. |
346a8d64 DL |
325 | DIMENSION defaults to the first dimension." |
326 | (unless dimension (setq dimension 1)) | |
103cc921 | 327 | (let ((code-space (plist-get (charset-plist charset) :code-space))) |
346a8d64 DL |
328 | (1+ (- (aref code-space (1- (* 2 dimension))) |
329 | (aref code-space (- (* 2 dimension) 2)))))) | |
c1841772 KH |
330 | |
331 | (defun charset-iso-final-char (charset) | |
12504f57 | 332 | "Return ISO-2022 final character of CHARSET." |
c1841772 KH |
333 | (or (plist-get (charset-plist charset) :iso-final-char) |
334 | -1)) | |
4ed46869 KH |
335 | |
336 | (defmacro charset-short-name (charset) | |
c1841772 KH |
337 | "Return short name of CHARSET." |
338 | (plist-get (charset-plist charset) :short-name)) | |
4ed46869 KH |
339 | |
340 | (defmacro charset-long-name (charset) | |
c1841772 KH |
341 | "Return long name of CHARSET." |
342 | (plist-get (charset-plist charset) :long-name)) | |
4ed46869 | 343 | |
d3675a42 | 344 | (defun charset-list () |
12504f57 | 345 | "Return list of all charsets ever defined. |
900dc6e3 | 346 | |
d3675a42 | 347 | This function is provided for backward compatibility. |
900dc6e3 | 348 | Now we have the variable `charset-list'." |
d3675a42 | 349 | charset-list) |
07513d64 | 350 | (make-obsolete 'charset-list "Use variable `charset-list'" "22.1") |
d3675a42 | 351 | |
c1841772 KH |
352 | (defun generic-char-p (char) |
353 | "Always return nil. This exists only for backward compatibility." | |
354 | nil) | |
07513d64 | 355 | (make-obsolete 'generic-char-p "Generic characters no longer exist" "22.1") |
0269ddfb | 356 | \f |
e76938e7 | 357 | ;; Coding system stuff |
4ed46869 | 358 | |
c1841772 KH |
359 | ;; Coding system is a symbol that has been defined by the function |
360 | ;; `define-coding-system'. | |
c76b5c99 | 361 | |
c1841772 KH |
362 | (defconst coding-system-iso-2022-flags |
363 | '(long-form | |
364 | ascii-at-eol | |
365 | ascii-at-cntl | |
366 | 7-bit | |
367 | locking-shift | |
368 | single-shift | |
369 | designation | |
370 | revision | |
371 | direction | |
372 | init-at-bol | |
373 | designate-at-bol | |
374 | safe | |
375 | latin-extra | |
376 | composition | |
3ed58a15 KH |
377 | euc-tw-shift |
378 | use-roman | |
379 | use-oldjis) | |
c1841772 | 380 | "List of symbols that control ISO-2022 encoder/decoder. |
0269ddfb | 381 | |
12504f57 | 382 | The value of the `:flags' attribute in the argument of the function |
caa7db3a | 383 | `define-coding-system' must be one of them. |
4ed46869 | 384 | |
c1841772 KH |
385 | If `long-form' is specified, use a long designation sequence on |
386 | encoding for the charsets `japanese-jisx0208-1978', `chinese-gb2312', | |
387 | and `japanese-jisx0208'. The long designation sequence doesn't | |
12504f57 | 388 | conform to ISO 2022, but is used by such coding systems as |
c1841772 KH |
389 | `compound-text'. |
390 | ||
391 | If `ascii-at-eol' is specified, designate ASCII to g0 at end of line | |
392 | on encoding. | |
393 | ||
394 | If `ascii-at-cntl' is specified, designate ASCII to g0 before control | |
395 | codes and SPC on encoding. | |
396 | ||
397 | If `7-bit' is specified, use 7-bit code only on encoding. | |
398 | ||
399 | If `locking-shift' is specified, decode locking-shift code correctly | |
400 | on decoding, and use locking-shift to invoke a graphic element on | |
401 | encoding. | |
402 | ||
403 | If `single-shift' is specified, decode single-shift code correctly on | |
404 | decoding, and use single-shift to invoke a graphic element on encoding. | |
405 | ||
406 | If `designation' is specified, decode designation code correctly on | |
407 | decoding, and use designation to designate a charset to a graphic | |
408 | element on encoding. | |
409 | ||
410 | If `revision' is specified, produce an escape sequence to specify | |
411 | revision number of a charset on encoding. Such an escape sequence is | |
412 | always correctly decoded on decoding. | |
413 | ||
414 | If `direction' is specified, decode ISO6429's code for specifying | |
12504f57 | 415 | direction correctly, and produce the code on encoding. |
c1841772 KH |
416 | |
417 | If `init-at-bol' is specified, on encoding, it is assumed that | |
418 | invocation and designation statuses are reset at each beginning of | |
12504f57 | 419 | line even if `ascii-at-eol' is not specified; thus no codes for |
c1841772 KH |
420 | resetting them are produced. |
421 | ||
422 | If `safe' is specified, on encoding, characters not supported by a | |
423 | coding are replaced with `?'. | |
424 | ||
12504f57 | 425 | If `latin-extra' is specified, the code-detection routine assumes that a |
c1841772 KH |
426 | code specified in `latin-extra-code-table' (which see) is valid. |
427 | ||
428 | If `composition' is specified, an escape sequence to specify | |
12504f57 | 429 | composition sequence is correctly decoded on decoding, and is produced |
c1841772 KH |
430 | on encoding. |
431 | ||
432 | If `euc-tw-shift' is specified, the EUC-TW specific shifting code is | |
12504f57 | 433 | correctly decoded on decoding, and is produced on encoding. |
c1841772 | 434 | |
12504f57 DL |
435 | If `use-roman' is specified, JIS0201-1976-Roman is designated instead |
436 | of ASCII. | |
437 | ||
438 | If `use-oldjis' is specified, JIS0208-1976 is designated instead of | |
439 | JIS0208-1983.") | |
440 | ||
441 | ;; Fixme: Are translation tables still relevant? (Not currently | |
442 | ;; implemented, anyway.) | |
443 | ;; Fixme: What does cons :bom mean? Explanation of :endian. | |
c1841772 | 444 | (defun define-coding-system (name docstring &rest props) |
12504f57 | 445 | "Define NAME (a symbol) as a coding system with DOCSTRING and attributes. |
c1841772 KH |
446 | The remaining arguments must come in pairs ATTRIBUTE VALUE. ATTRIBUTE |
447 | may be any symbol. | |
448 | ||
12504f57 DL |
449 | The following attributes have special meanings. Those labeled as |
450 | \"(required)\", should not be omitted. | |
c1841772 KH |
451 | |
452 | `:mnemonic' (required) | |
453 | ||
454 | VALUE is a character to display on mode line for the coding system. | |
455 | ||
456 | `:coding-type' (required) | |
457 | ||
458 | VALUE must be one of `charset', `utf-8', `utf-16', `iso-2022', | |
5170b329 | 459 | `emacs-mule', `shift-jis', `big5', `ccl', `raw-text', `undecided'. |
c1841772 | 460 | |
12504f57 | 461 | `:eol-type' |
c1841772 | 462 | |
12504f57 | 463 | VALUE is the EOL (end-of-line) format of the coding system. It must be |
c1841772 KH |
464 | one of `unix', `dos', `mac'. The symbol `unix' means Unix-like EOL |
465 | \(i.e. single LF), `dos' means DOS-like EOL \(i.e. sequence of CR LF), | |
466 | and `mac' means MAC-like EOL \(i.e. single CR). If omitted, on | |
12504f57 | 467 | decoding by the coding system, Emacs automatically detects the EOL |
c1841772 KH |
468 | format of the source text. |
469 | ||
12504f57 | 470 | `:charset-list' (required for `charset' coding systems) |
c1841772 KH |
471 | |
472 | VALUE must be a list of charsets supported by the coding system. On | |
473 | encoding by the coding system, if a character belongs to multiple | |
474 | charsets in the list, a charset that comes earlier in the list is | |
475 | selected. | |
476 | ||
12504f57 | 477 | `:ascii-compatible-p' |
c1841772 KH |
478 | |
479 | If VALUE is non-nil, the coding system decodes all 7-bit bytes into | |
07513d64 | 480 | the corresponding ASCII characters, and encodes all ASCII characters |
12504f57 | 481 | back to the corresponding 7-bit bytes. VALUE defaults to nil. |
c1841772 | 482 | |
12504f57 | 483 | `:decode-translation-table' |
c1841772 KH |
484 | |
485 | VALUE must be a translation table to use on decoding. | |
486 | ||
12504f57 | 487 | `:encode-translation-table' |
c1841772 KH |
488 | |
489 | VALUE must be a translation table to use on encoding. | |
490 | ||
12504f57 | 491 | `:post-read-conversion' |
c1841772 KH |
492 | |
493 | VALUE must be a function to call after some text is inserted and | |
494 | decoded by the coding system itself and before any functions in | |
495 | `after-insert-functions' are called. The arguments to this function | |
12504f57 DL |
496 | are the same as those of a function in `after-insert-file-functions', |
497 | i.e. LENGTH of the text to be decoded with point at the head of it, | |
498 | and the function should leave point unchanged. | |
c1841772 KH |
499 | |
500 | `:pre-write-conversion' | |
501 | ||
502 | VALUE must be a function to call after all functions in | |
503 | `write-region-annotate-functions' and `buffer-file-format' are called, | |
504 | and before the text is encoded by the coding system itself. The | |
12504f57 DL |
505 | arguments to this function are the same as those of a function in |
506 | `write-region-annotate-functions'. | |
c1841772 KH |
507 | |
508 | `:default-char' | |
509 | ||
510 | VALUE must be a character. On encoding, a character not supported by | |
511 | the coding system is replaced with VALUE. | |
512 | ||
513 | `:eol-type' | |
514 | ||
515 | VALUE must be `unix', `dos', `mac'. The symbol `unix' means Unix-like | |
516 | EOL (LF), `dos' means DOS-like EOL (CRLF), and `mac' means MAC-like | |
12504f57 DL |
517 | EOL (CR). If omitted, on decoding, the coding system detects EOL |
518 | format automatically, and on encoding, uses Unix-like EOL. | |
c1841772 KH |
519 | |
520 | `:mime-charset' | |
521 | ||
12504f57 DL |
522 | VALUE must be a symbol whose name is that of a MIME charset converted |
523 | to lower case. | |
c1841772 KH |
524 | |
525 | `:flags' | |
526 | ||
12504f57 DL |
527 | VALUE must be a list of symbols that control the ISO-2022 converter. |
528 | Each must be a member of the list `coding-system-iso-2022-flags' | |
c1841772 KH |
529 | \(which see). This attribute has a meaning only when `:coding-type' |
530 | is `iso-2022'. | |
531 | ||
532 | `:designation' | |
533 | ||
12504f57 | 534 | VALUE must be a vector [G0-USAGE G1-USAGE G2-USAGE G3-USAGE]. |
c1841772 KH |
535 | GN-USAGE specifies the usage of graphic register GN as follows. |
536 | ||
537 | If it is nil, no charset can be designated to GN. | |
538 | ||
07513d64 | 539 | If it is a charset, the charset is initially designated to GN, and |
c1841772 KH |
540 | never used by the other charsets. |
541 | ||
542 | If it is a list, the elements must be charsets, nil, 94, or 96. GN | |
12504f57 DL |
543 | can be used by all the listed charsets. If the list contains 94, any |
544 | iso-2022 charset whose code-space ranges are 94 long can be designated | |
545 | to GN. If the list contains 96, any charsets whose whose ranges are | |
546 | 96 long can be designated to GN. If the first element is a charset, | |
547 | that charset is initially designated to GN. | |
c1841772 KH |
548 | |
549 | This attribute has a meaning only when `:coding-type' is `iso-2022'. | |
550 | ||
551 | `:bom' | |
552 | ||
12504f57 DL |
553 | This attributes specifies whether the coding system uses a `byte order |
554 | mark'. VALUE must nil, t, or cons of coding systems whose | |
555 | `:coding-type' is `utf-16'. | |
c1841772 KH |
556 | |
557 | This attribute has a meaning only when `:coding-type' is `utf-16'. | |
558 | ||
559 | `:endian' | |
560 | ||
561 | VALUE must be t or nil. See the above description for the detail. | |
562 | ||
563 | This attribute has a meaning only when `:coding-type' is `utf-16'. | |
564 | ||
565 | `:ccl-decoder' | |
566 | ||
12504f57 DL |
567 | VALUE is a symbol representing the registered CCL program used for |
568 | decoding. This attribute has a meaning only when `:coding-type' is | |
569 | `ccl'. | |
c1841772 KH |
570 | |
571 | `:ccl-encoder' | |
572 | ||
12504f57 DL |
573 | VALUE is a symbol representing the registered CCL program used for |
574 | encoding. This attribute has a meaning only when `:coding-type' is | |
575 | `ccl'." | |
c1841772 KH |
576 | (let* ((common-attrs (mapcar 'list |
577 | '(:mnemonic | |
578 | :coding-type | |
579 | :charset-list | |
580 | :ascii-compatible-p | |
581 | :docode-translation-table | |
582 | :encode-translation-table | |
583 | :post-read-conversion | |
584 | :pre-write-conversion | |
585 | :default-char | |
586 | :plist | |
587 | :eol-type))) | |
588 | (coding-type (plist-get props :coding-type)) | |
589 | (spec-attrs (mapcar 'list | |
590 | (cond ((eq coding-type 'iso-2022) | |
591 | '(:initial | |
592 | :reg-usage | |
593 | :request | |
594 | :flags)) | |
595 | ((eq coding-type 'utf-16) | |
596 | '(:bom | |
597 | :endian)) | |
687441de | 598 | ;; Fixme: CCL definition is broken. |
c1841772 KH |
599 | ((eq coding-type 'ccl) |
600 | '(:ccl-decoder | |
601 | :ccl-encoder | |
602 | :valids)))))) | |
603 | ||
604 | (dolist (slot common-attrs) | |
605 | (setcdr slot (plist-get props (car slot)))) | |
606 | ||
607 | (dolist (slot spec-attrs) | |
608 | (setcdr slot (plist-get props (car slot)))) | |
609 | ||
610 | (if (eq coding-type 'iso-2022) | |
611 | (let ((designation (plist-get props :designation)) | |
612 | (flags (plist-get props :flags)) | |
613 | (initial (make-vector 4 nil)) | |
614 | (reg-usage (cons 4 4)) | |
615 | request elt) | |
616 | (dotimes (i 4) | |
617 | (setq elt (aref designation i)) | |
618 | (cond ((charsetp elt) | |
619 | (aset initial i elt) | |
620 | (setq request (cons (cons elt i) request))) | |
621 | ((consp elt) | |
622 | (aset initial i (car elt)) | |
623 | (if (charsetp (car elt)) | |
624 | (setq request (cons (cons (car elt) i) request))) | |
625 | (dolist (e (cdr elt)) | |
626 | (cond ((charsetp e) | |
627 | (setq request (cons (cons e i) request))) | |
628 | ((eq e 94) | |
629 | (setcar reg-usage i)) | |
630 | ((eq e 96) | |
631 | (setcdr reg-usage i)) | |
632 | ((eq e t) | |
633 | (setcar reg-usage i) | |
634 | (setcdr reg-usage i))))))) | |
635 | (setcdr (assq :initial spec-attrs) initial) | |
636 | (setcdr (assq :reg-usage spec-attrs) reg-usage) | |
637 | (setcdr (assq :request spec-attrs) request) | |
638 | ||
639 | ;; Change :flags value from a list to a bit-mask. | |
640 | (let ((bits 0) | |
641 | (i 0)) | |
642 | (dolist (elt coding-system-iso-2022-flags) | |
643 | (if (memq elt flags) | |
644 | (setq bits (logior bits (lsh 1 i)))) | |
645 | (setq i (1+ i))) | |
646 | (setcdr (assq :flags spec-attrs) bits)))) | |
647 | ||
648 | ;; Add :name and :docstring properties to PROPS. | |
649 | (setq props | |
e1e529fa DL |
650 | (cons :name (cons name (cons :docstring (cons (purecopy docstring) |
651 | props))))) | |
c1841772 KH |
652 | (setcdr (assq :plist common-attrs) props) |
653 | ||
654 | (apply 'define-coding-system-internal | |
655 | name (mapcar 'cdr (append common-attrs spec-attrs))))) | |
656 | ||
657 | (defun coding-system-doc-string (coding-system) | |
658 | "Return the documentation string for CODING-SYSTEM." | |
659 | (plist-get (coding-system-plist coding-system) :docstring)) | |
4ed46869 | 660 | |
4ed46869 | 661 | (defun coding-system-mnemonic (coding-system) |
0269ddfb | 662 | "Return the mnemonic character of CODING-SYSTEM. |
12504f57 DL |
663 | The mnemonic character of a coding system is used in mode line to |
664 | indicate the coding system. If CODING-SYSTEM. is nil, return ?=." | |
c1841772 | 665 | (plist-get (coding-system-plist coding-system) :mnemonic)) |
4ed46869 | 666 | |
c1841772 KH |
667 | (defun coding-system-type (coding-system) |
668 | "Return the coding type of CODING-SYSTEM. | |
669 | A coding type is a symbol indicating the encoding method of CODING-SYSTEM. | |
670 | See the function `define-coding-system' for more detail." | |
671 | (plist-get (coding-system-plist coding-system) :coding-type)) | |
d3675a42 | 672 | |
c1841772 | 673 | (defun coding-system-charset-list (coding-system) |
07513d64 | 674 | "Return list of charsets supported by CODING-SYSTEM. |
c1841772 KH |
675 | If CODING-SYSTEM supports all ISO-2022 charsets, return `iso-2022'. |
676 | If CODING-SYSTEM supports all emacs-mule charsets, return `emacs-mule'." | |
677 | (plist-get (coding-system-plist coding-system) :charset-list)) | |
0269ddfb KH |
678 | |
679 | (defun coding-system-get (coding-system prop) | |
07513d64 DL |
680 | "Extract a value from CODING-SYSTEM's property list for property PROP. |
681 | For compatibility with Emacs 20/21, this accepts old-style symbols | |
682 | like `mime-charset' as well as the current style like `:mime-charset'." | |
683 | (or (plist-get (coding-system-plist coding-system) prop) | |
684 | (if (not (keywordp prop)) | |
685 | (plist-get (coding-system-plist coding-system) | |
686 | (intern (concat ":" (symbol-name prop))))))) | |
0269ddfb KH |
687 | |
688 | (defun coding-system-put (coding-system prop val) | |
689 | "Change value in CODING-SYSTEM's property list PROP to VAL." | |
c1841772 | 690 | (plist-put (coding-system-plist coding-system) prop val)) |
0269ddfb KH |
691 | |
692 | (defalias 'coding-system-parent 'coding-system-base) | |
2598a293 | 693 | (make-obsolete 'coding-system-parent 'coding-system-base "20.3") |
0269ddfb | 694 | |
857ea15c AS |
695 | (defun coding-system-lessp (x y) |
696 | (cond ((eq x 'no-conversion) t) | |
697 | ((eq y 'no-conversion) nil) | |
698 | ((eq x 'emacs-mule) t) | |
699 | ((eq y 'emacs-mule) nil) | |
700 | ((eq x 'undecided) t) | |
701 | ((eq y 'undecided) nil) | |
702 | (t (let ((c1 (coding-system-mnemonic x)) | |
703 | (c2 (coding-system-mnemonic y))) | |
704 | (or (< (downcase c1) (downcase c2)) | |
705 | (and (not (> (downcase c1) (downcase c2))) | |
706 | (< c1 c2))))))) | |
707 | ||
857ea15c | 708 | (defun add-to-coding-system-list (coding-system) |
521d4010 | 709 | "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted." |
857ea15c AS |
710 | (if (or (null coding-system-list) |
711 | (coding-system-lessp coding-system (car coding-system-list))) | |
712 | (setq coding-system-list (cons coding-system coding-system-list)) | |
713 | (let ((len (length coding-system-list)) | |
714 | mid (tem coding-system-list)) | |
715 | (while (> len 1) | |
716 | (setq mid (nthcdr (/ len 2) tem)) | |
717 | (if (coding-system-lessp (car mid) coding-system) | |
718 | (setq tem mid | |
719 | len (- len (/ len 2))) | |
720 | (setq len (/ len 2)))) | |
721 | (setcdr tem (cons coding-system (cdr tem)))))) | |
722 | ||
80a7463d | 723 | (defun coding-system-list (&optional base-only) |
c11a8f77 | 724 | "Return a list of all existing non-subsidiary coding systems. |
12504f57 DL |
725 | If optional arg BASE-ONLY is non-nil, only base coding systems are |
726 | listed. The value doesn't include subsidiary coding systems which are | |
c11a8f77 KH |
727 | made from bases and aliases automatically for various end-of-line |
728 | formats (e.g. iso-latin-1-unix, koi8-r-dos)." | |
80a7463d KH |
729 | (let* ((codings (copy-sequence coding-system-list)) |
730 | (tail (cons nil codings))) | |
731 | ;; Remove subsidiary coding systems (eol variants) and alias | |
732 | ;; coding systems (if necessary). | |
733 | (while (cdr tail) | |
734 | (let* ((coding (car (cdr tail))) | |
687441de | 735 | (aliases (coding-system-aliases coding))) |
80a7463d KH |
736 | (if (or |
737 | ;; CODING is an eol variant if not in ALIASES. | |
738 | (not (memq coding aliases)) | |
739 | ;; CODING is an alias if it is not car of ALIASES. | |
740 | (and base-only (not (eq coding (car aliases))))) | |
741 | (setcdr tail (cdr (cdr tail))) | |
742 | (setq tail (cdr tail))))) | |
743 | codings)) | |
744 | ||
4ed46869 | 745 | (defun set-buffer-file-coding-system (coding-system &optional force) |
358d28fb RS |
746 | "Set the file coding-system of the current buffer to CODING-SYSTEM. |
747 | This means that when you save the buffer, it will be converted | |
748 | according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM, | |
749 | use \\[list-coding-systems]. | |
750 | ||
751 | If the buffer's previous file coding-system value specifies end-of-line | |
752 | conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is | |
753 | merged with the already-specified end-of-line conversion. | |
b839fdcc KH |
754 | |
755 | If the buffer's previous file coding-system value specifies text | |
756 | conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is | |
757 | merged with the already-specified text conversion. | |
758 | ||
759 | However, if the optional prefix argument FORCE is non-nil, then | |
760 | CODING-SYSTEM is used exactly as specified. | |
aeef8f07 KH |
761 | |
762 | This marks the buffer modified so that the succeeding \\[save-buffer] | |
763 | surely saves the buffer with CODING-SYSTEM. From a program, if you | |
764 | don't want to mark the buffer modified, just set the variable | |
765 | `buffer-file-coding-system' directly." | |
d9e3229d | 766 | (interactive "zCoding system for visited file (default, nil): \nP") |
4ed46869 | 767 | (check-coding-system coding-system) |
36d455c4 | 768 | (if (and coding-system buffer-file-coding-system (null force)) |
8dd735c1 KH |
769 | (let ((base (coding-system-base buffer-file-coding-system)) |
770 | (eol (coding-system-eol-type buffer-file-coding-system))) | |
771 | ;; If CODING-SYSTEM doesn't specify text conversion, merge | |
772 | ;; with that of buffer-file-coding-system. | |
773 | (if (eq (coding-system-base coding-system) 'undecided) | |
774 | (setq coding-system (coding-system-change-text-conversion | |
775 | coding-system base))) | |
776 | ;; If CODING-SYSTEM doesn't specify eol conversion, merge with | |
777 | ;; that of buffer-file-coding-system. | |
778 | (if (and (vectorp (coding-system-eol-type coding-system)) | |
779 | (numberp eol) (>= eol 0) (<= eol 2)) | |
780 | (setq coding-system (coding-system-change-eol-conversion | |
781 | coding-system eol))))) | |
4ed46869 KH |
782 | (setq buffer-file-coding-system coding-system) |
783 | (set-buffer-modified-p t) | |
784 | (force-mode-line-update)) | |
785 | ||
358d28fb RS |
786 | (defvar default-terminal-coding-system nil |
787 | "Default value for the terminal coding system. | |
788 | This is normally set according to the selected language environment. | |
789 | See also the command `set-terminal-coding-system'.") | |
790 | ||
df100398 KH |
791 | (defun set-terminal-coding-system (coding-system) |
792 | "Set coding system of your terminal to CODING-SYSTEM. | |
358d28fb RS |
793 | All text output to the terminal will be encoded |
794 | with the specified coding system. | |
795 | For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]. | |
796 | The default is determined by the selected language environment | |
797 | or by the previous use of this command." | |
798 | (interactive | |
2e02a76f RS |
799 | (list (let ((default (if (and (not (terminal-coding-system)) |
800 | default-terminal-coding-system) | |
801 | default-terminal-coding-system))) | |
802 | (read-coding-system | |
803 | (format "Coding system for terminal display (default, %s): " | |
804 | default) | |
805 | default)))) | |
358d28fb RS |
806 | (if (and (not coding-system) |
807 | (not (terminal-coding-system))) | |
808 | (setq coding-system default-terminal-coding-system)) | |
809 | (if coding-system | |
521d4010 | 810 | (setq default-terminal-coding-system coding-system)) |
df100398 KH |
811 | (set-terminal-coding-system-internal coding-system) |
812 | (redraw-frame (selected-frame))) | |
813 | ||
358d28fb RS |
814 | (defvar default-keyboard-coding-system nil |
815 | "Default value of the keyboard coding system. | |
816 | This is normally set according to the selected language environment. | |
817 | See also the command `set-keyboard-coding-system'.") | |
818 | ||
df100398 | 819 | (defun set-keyboard-coding-system (coding-system) |
358d28fb RS |
820 | "Set coding system for keyboard input to CODING-SYSTEM. |
821 | In addition, this command enables Encoded-kbd minor mode. | |
6d34f495 DL |
822 | \(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off -- see |
823 | `encoded-kbd-mode'.) | |
358d28fb RS |
824 | For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]. |
825 | The default is determined by the selected language environment | |
826 | or by the previous use of this command." | |
827 | (interactive | |
2e02a76f RS |
828 | (list (let ((default (if (and (not (keyboard-coding-system)) |
829 | default-keyboard-coding-system) | |
830 | default-keyboard-coding-system))) | |
831 | (read-coding-system | |
832 | (format "Coding system for keyboard input (default, %s): " | |
833 | default) | |
834 | default)))) | |
358d28fb RS |
835 | (if (and (not coding-system) |
836 | (not (keyboard-coding-system))) | |
837 | (setq coding-system default-keyboard-coding-system)) | |
838 | (if coding-system | |
839 | (setq default-keyboard-coding-system coding-system)) | |
df100398 | 840 | (set-keyboard-coding-system-internal coding-system) |
b23bad0b | 841 | (setq keyboard-coding-system coding-system) |
df100398 KH |
842 | (encoded-kbd-mode (if coding-system 1 0))) |
843 | ||
6d34f495 DL |
844 | (defcustom keyboard-coding-system nil |
845 | "Specify coding system for keyboard input. | |
846 | If you set this on a terminal which can't distinguish Meta keys from | |
847 | 8-bit characters, you will have to use ESC to type Meta characters. | |
848 | See Info node `Specify Coding' and Info node `Single-Byte Character Support'. | |
849 | ||
850 | Setting this variable directly does not take effect; | |
851 | use either M-x customize or \\[set-keyboard-coding-system]." | |
852 | :type '(coding-system :tag "Coding system") | |
853 | :link '(info-link "(emacs)Specify Coding") | |
854 | :link '(info-link "(emacs)Single-Byte Character Support") | |
855 | :set (lambda (symbol value) | |
856 | ;; Don't load encoded-kbd-mode unnecessarily. | |
857 | (if (or value (boundp 'encoded-kbd-mode)) | |
858 | (set-keyboard-coding-system value) | |
859 | (set-default 'keyboard-coding-system nil))) ; must initialize | |
860 | :version "21.1" | |
861 | :group 'keyboard | |
862 | :group 'mule) | |
863 | ||
df100398 | 864 | (defun set-buffer-process-coding-system (decoding encoding) |
358d28fb | 865 | "Set coding systems for the process associated with the current buffer. |
df100398 | 866 | DECODING is the coding system to be used to decode input from the process, |
358d28fb RS |
867 | ENCODING is the coding system to be used to encode output to the process. |
868 | ||
869 | For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]." | |
4ed46869 | 870 | (interactive |
83911021 | 871 | "zCoding-system for output from the process: \nzCoding-system for input to the process: ") |
4ed46869 KH |
872 | (let ((proc (get-buffer-process (current-buffer)))) |
873 | (if (null proc) | |
521d4010 | 874 | (error "No process") |
df100398 KH |
875 | (check-coding-system decoding) |
876 | (check-coding-system encoding) | |
877 | (set-process-coding-system proc decoding encoding))) | |
4ed46869 KH |
878 | (force-mode-line-update)) |
879 | ||
d0b99881 RS |
880 | (defalias 'set-clipboard-coding-system 'set-selection-coding-system) |
881 | ||
14915c37 | 882 | (defun set-selection-coding-system (coding-system) |
b25eef20 KH |
883 | "Make CODING-SYSTEM used for communicating with other X clients . |
884 | When sending or receiving text via cut_buffer, selection, and clipboard, | |
885 | the text is encoded or decoded by CODING-SYSTEM." | |
a03b3ce1 | 886 | (interactive "zCoding system for X selection: ") |
b25eef20 | 887 | (check-coding-system coding-system) |
14915c37 | 888 | (setq selection-coding-system coding-system)) |
b25eef20 | 889 | |
e8dd0160 | 890 | ;; Coding system lastly specified by the command |
a03b3ce1 KH |
891 | ;; set-next-selection-coding-system. |
892 | (defvar last-next-selection-coding-system nil) | |
893 | ||
894 | (defun set-next-selection-coding-system (coding-system) | |
12504f57 | 895 | "Use CODING-SYSTEM for next communication with other window system clients. |
a03b3ce1 KH |
896 | This setting is effective for the next communication only." |
897 | (interactive | |
898 | (list (read-coding-system | |
899 | (if last-next-selection-coding-system | |
12504f57 | 900 | (format "Coding system for the next selection (default, %S): " |
a03b3ce1 | 901 | last-next-selection-coding-system) |
12504f57 | 902 | "Coding system for the next selection: ") |
a03b3ce1 KH |
903 | last-next-selection-coding-system))) |
904 | (if coding-system | |
905 | (setq last-next-selection-coding-system coding-system) | |
906 | (setq coding-system last-next-selection-coding-system)) | |
907 | (check-coding-system coding-system) | |
908 | ||
909 | (setq next-selection-coding-system coding-system)) | |
910 | ||
12504f57 | 911 | ;; Fixme: Should this just go? |
4ed46869 | 912 | (defun set-coding-priority (arg) |
521d4010 | 913 | "Set priority of coding categories according to ARG. |
c1841772 KH |
914 | ARG is a list of coding categories ordered by priority. |
915 | ||
916 | This function is provided for backward compatibility. | |
917 | Now we have more convenient function `set-coding-system-priority'." | |
d9e3229d KH |
918 | (let ((l arg) |
919 | (current-list (copy-sequence coding-category-list))) | |
e8dd0160 | 920 | ;; Check the validity of ARG while deleting coding categories in |
d9e3229d KH |
921 | ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST |
922 | ;; contains all coding categories. | |
923 | (while l | |
924 | (if (or (null (get (car l) 'coding-category-index)) | |
925 | (null (memq (car l) current-list))) | |
926 | (error "Invalid or duplicated element in argument: %s" arg)) | |
927 | (setq current-list (delq (car l) current-list)) | |
928 | (setq l (cdr l))) | |
4ed46869 | 929 | ;; Update `coding-category-list' and return it. |
2feaf204 | 930 | (setq coding-category-list (append arg current-list)) |
07513d64 | 931 | ;; Fixme: not defined. |
2feaf204 | 932 | (set-coding-priority-internal))) |
07513d64 | 933 | (make-obsolete 'set-coding-priority 'set-coding-system-priority "22.1") |
4ed46869 | 934 | |
835cbadb EZ |
935 | ;;; X selections |
936 | ||
937 | (defvar non-standard-icccm-encodings-alist | |
938 | '(("ISO8859-15" . latin-iso8859-15) | |
939 | ("ISO8859-14" . latin-iso8859-14) | |
940 | ("KOI8-R" . koi8-r) | |
941 | ("BIG5-0" . big5)) | |
942 | "Alist of font charset names defined by XLFD, and the corresponding Emacs | |
943 | charsets or coding systems.") | |
944 | ||
12504f57 DL |
945 | ;; Fixme: this needs sorting out |
946 | ||
835cbadb EZ |
947 | ;; Functions to support "Non-Standard Character Set Encodings" defined |
948 | ;; by the ICCCM spec. We support that by converting the leading | |
949 | ;; sequence of the ``extended segment'' to the corresponding ISO-2022 | |
950 | ;; sequences (if the leading sequence names an Emacs charset), or decode | |
951 | ;; the segment (if it names a coding system). Encoding does the reverse. | |
952 | (defun ctext-post-read-conversion (len) | |
953 | "Decode LEN characters encoded as Compound Text with Extended Segments." | |
954 | (buffer-disable-undo) ; minimize consing due to insertions and deletions | |
955 | (narrow-to-region (point) (+ (point) len)) | |
956 | (save-match-data | |
957 | (let ((pt (point-marker)) | |
958 | (oldpt (point-marker)) | |
959 | (newpt (make-marker)) | |
960 | (modified-p (buffer-modified-p)) | |
961 | (case-fold-search nil) | |
962 | last-coding-system-used | |
963 | encoding textlen chset) | |
964 | (while (re-search-forward | |
965 | "\\(\e\\)%/[0-4]\\([\200-\377][\200-\377]\\)\\([^\002]+\\)\002" | |
966 | nil 'move) | |
967 | (set-marker newpt (point)) | |
968 | (set-marker pt (match-beginning 0)) | |
969 | (setq encoding (match-string 3)) | |
970 | (setq textlen (- (+ (* (- (aref (match-string 2) 0) 128) 128) | |
971 | (- (aref (match-string 2) 1) 128)) | |
972 | (1+ (length encoding)))) | |
973 | (setq | |
974 | chset (cdr (assoc-ignore-case encoding | |
975 | non-standard-icccm-encodings-alist))) | |
976 | (cond ((null chset) | |
977 | ;; This charset is not supported--leave this extended | |
978 | ;; segment unaltered and skip over it. | |
979 | (goto-char (+ (point) textlen))) | |
980 | ((charsetp chset) | |
981 | ;; If it's a charset, replace the leading escape sequence | |
982 | ;; with a standard ISO-2022 sequence. We will decode all | |
983 | ;; such segments later, in one go, when we exit the loop | |
984 | ;; or find an extended segment that names a coding | |
985 | ;; system, not a charset. | |
986 | (replace-match | |
987 | (concat "\\1" | |
988 | (if (= 0 (charset-iso-graphic-plane chset)) | |
989 | ;; GL charsets | |
990 | (if (= 1 (charset-dimension chset)) "(" "$(") | |
991 | ;; GR charsets | |
992 | (if (= 96 (charset-chars chset)) | |
993 | "-" | |
994 | (if (= 1 (charset-dimension chset)) ")" "$)"))) | |
995 | (string (charset-iso-final-char chset))) | |
996 | t) | |
997 | (goto-char (+ (point) textlen))) | |
998 | ((coding-system-p chset) | |
999 | ;; If it's a coding system, we need to decode the segment | |
1000 | ;; right away. But first, decode what we've skipped | |
1001 | ;; across until now. | |
1002 | (when (> pt oldpt) | |
1003 | (decode-coding-region oldpt pt 'ctext-no-compositions)) | |
1004 | (delete-region pt newpt) | |
1005 | (set-marker newpt (+ newpt textlen)) | |
1006 | (decode-coding-region pt newpt chset) | |
1007 | (goto-char newpt) | |
1008 | (set-marker oldpt newpt)))) | |
1009 | ;; Decode what's left. | |
1010 | (when (> (point) oldpt) | |
1011 | (decode-coding-region oldpt (point) 'ctext-no-compositions)) | |
1012 | ;; This buffer started as unibyte, because the string we get from | |
1013 | ;; the X selection is a unibyte string. We must now make it | |
1014 | ;; multibyte, so that the decoded text is inserted as multibyte | |
1015 | ;; into its buffer. | |
1016 | (set-buffer-multibyte t) | |
1017 | (set-buffer-modified-p modified-p) | |
1018 | (- (point-max) (point-min))))) | |
1019 | ||
1020 | (defvar non-standard-designations-alist | |
1021 | '(("$(0" . (big5 "big5-0" 2)) | |
1022 | ("$(1" . (big5 "big5-0" 2)) | |
1023 | ("-V" . (t "iso8859-10" 1)) | |
1024 | ("-Y" . (t "iso8859-13" 1)) | |
1025 | ("-_" . (t "iso8859-14" 1)) | |
1026 | ("-b" . (t "iso8859-15" 1)) | |
1027 | ("-f" . (t "iso8859-16" 1))) | |
1028 | "Alist of ctext control sequences that introduce character sets which | |
1029 | are not in the list of approved ICCCM encodings, and the corresponding | |
1030 | coding system, identifier string, and number of octets per encoded | |
1031 | character. | |
1032 | ||
1033 | Each element has the form (CTLSEQ . (ENCODING CHARSET NOCTETS)). CTLSEQ | |
1034 | is the control sequence (sans the leading ESC) that introduces the character | |
1035 | set in the text encoded by compound-text. ENCODING is a coding system | |
1036 | symbol; if it is t, it means that the ctext coding system already encodes | |
1037 | the text correctly, and only the leading control sequence needs to be altered. | |
1038 | If ENCODING is a coding system, we need to re-encode the text with that | |
1039 | coding system. CHARSET is the ICCCM name of the charset we need to put into | |
1040 | the leading control sequence. NOCTETS is the number of octets (bytes) that | |
1041 | encode each character in this charset. NOCTETS can be 0 (meaning the number | |
1042 | of octets per character is variable), 1, 2, 3, or 4.") | |
1043 | ||
1044 | (defun ctext-pre-write-conversion (from to) | |
5dde3c71 EZ |
1045 | "Encode characters between FROM and TO as Compound Text w/Extended Segments. |
1046 | ||
1047 | If FROM is a string, or if the current buffer is not the one set up for us | |
1048 | by run_pre_post_conversion_on_str, generate a new temp buffer, insert the | |
1049 | text, and convert it in the temporary buffer. Otherwise, convert in-place." | |
1050 | (cond ((and (string= (buffer-name) " *code-converting-work*") | |
1051 | (not (stringp from))) | |
1052 | ; Minimize consing due to subsequent insertions and deletions. | |
1053 | (buffer-disable-undo) | |
1054 | (narrow-to-region from to)) | |
1055 | (t | |
1056 | (let ((buf (current-buffer))) | |
1057 | (set-buffer (generate-new-buffer " *temp")) | |
1058 | (buffer-disable-undo) | |
1059 | (if (stringp from) | |
1060 | (insert from) | |
1061 | (insert-buffer-substring buf from to))))) | |
835cbadb EZ |
1062 | (encode-coding-region from to 'ctext-no-compositions) |
1063 | ;; Replace ISO-2022 charset designations with extended segments, for | |
1064 | ;; those charsets that are not part of the official X registry. | |
1065 | (save-match-data | |
1066 | (goto-char (point-min)) | |
1067 | (let ((newpt (make-marker)) | |
1068 | (case-fold-search nil) | |
1069 | pt desig encode-info encoding chset noctets textlen) | |
1070 | (set-buffer-multibyte nil) | |
5dde3c71 EZ |
1071 | ;; The regexp below finds the leading sequences for big5 and |
1072 | ;; iso8859-1[03-6] charsets. | |
835cbadb EZ |
1073 | (while (re-search-forward "\e\\(\$([01]\\|-[VY_bf]\\)" nil 'move) |
1074 | (setq desig (match-string 1) | |
1075 | pt (point-marker) | |
1076 | encode-info (cdr (assoc desig non-standard-designations-alist)) | |
1077 | encoding (car encode-info) | |
1078 | chset (cadr encode-info) | |
1079 | noctets (car (cddr encode-info))) | |
1080 | (skip-chars-forward "^\e") | |
1081 | (set-marker newpt (point)) | |
1082 | (cond | |
1083 | ((eq encoding t) ; only the leading sequence needs to be changed | |
1084 | (setq textlen (+ (- newpt pt) (length chset) 1)) | |
5dde3c71 | 1085 | ;; Generate the ICCCM control sequence for an extended segment. |
835cbadb EZ |
1086 | (replace-match (format "\e%%/%d%c%c%s\ 2" |
1087 | noctets | |
1088 | (+ (/ textlen 128) 128) | |
1089 | (+ (% textlen 128) 128) | |
1090 | chset) | |
1091 | t t)) | |
1092 | ((coding-system-p encoding) ; need to recode the entire segment... | |
1093 | (set-marker pt (match-beginning 0)) | |
1094 | (decode-coding-region pt newpt 'ctext-no-compositions) | |
1095 | (set-buffer-multibyte t) | |
1096 | (encode-coding-region pt newpt encoding) | |
1097 | (set-buffer-multibyte nil) | |
1098 | (setq textlen (+ (- newpt pt) (length chset) 1)) | |
1099 | (goto-char pt) | |
1100 | (insert (format "\e%%/%d%c%c%s\ 2" | |
1101 | noctets | |
1102 | (+ (/ textlen 128) 128) | |
1103 | (+ (% textlen 128) 128) | |
1104 | chset)))) | |
1105 | (goto-char newpt)))) | |
1106 | (set-buffer-multibyte t) | |
5dde3c71 | 1107 | ;; Must return nil, as build_annotations_2 expects that. |
835cbadb EZ |
1108 | nil) |
1109 | ||
c1841772 KH |
1110 | (make-obsolete 'set-coding-priority 'set-coding-system-priority "22.0") |
1111 | ||
4ed46869 KH |
1112 | ;;; FILE I/O |
1113 | ||
e76938e7 | 1114 | (defcustom auto-coding-alist |
0735296c | 1115 | '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion) |
4f16d1d1 | 1116 | ("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion)) |
835f49b8 KH |
1117 | "Alist of filename patterns vs corresponding coding systems. |
1118 | Each element looks like (REGEXP . CODING-SYSTEM). | |
558b0c86 | 1119 | A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading. |
835f49b8 | 1120 | |
7fed493a RS |
1121 | The settings in this alist take priority over `coding:' tags |
1122 | in the file (see the function `set-auto-coding') | |
e76938e7 DL |
1123 | and the contents of `file-coding-system-alist'." |
1124 | :group 'files | |
1125 | :group 'mule | |
1126 | :type '(repeat (cons (regexp :tag "File name regexp") | |
1127 | (symbol :tag "Coding system")))) | |
835f49b8 | 1128 | |
502522b2 | 1129 | (defcustom auto-coding-regexp-alist |
4e4a5bca DL |
1130 | '(("^BABYL OPTIONS:[ \t]*-\\*-[ \t]*rmail[ \t]*-\\*-" . no-conversion) |
1131 | ("\\`;ELC\14\0\0\0" . emacs-mule)) ; Emacs 20-compiled | |
502522b2 GM |
1132 | "Alist of patterns vs corresponding coding systems. |
1133 | Each element looks like (REGEXP . CODING-SYSTEM). | |
1134 | A file whose first bytes match REGEXP is decoded by CODING-SYSTEM on reading. | |
1135 | ||
1136 | The settings in this alist take priority over `coding:' tags | |
1137 | in the file (see the function `set-auto-coding') | |
1138 | and the contents of `file-coding-system-alist'." | |
1139 | :group 'files | |
1140 | :group 'mule | |
1141 | :type '(repeat (cons (regexp :tag "Regexp") | |
1142 | (symbol :tag "Coding system")))) | |
1143 | ||
1c4cc63a KH |
1144 | (defvar set-auto-coding-for-load nil |
1145 | "Non-nil means look for `load-coding' property instead of `coding'. | |
1146 | This is used for loading and byte-compiling Emacs Lisp files.") | |
1147 | ||
8a592131 RS |
1148 | (defun auto-coding-alist-lookup (filename) |
1149 | "Return the coding system specified by `auto-coding-alist' for FILENAME." | |
1150 | (let ((alist auto-coding-alist) | |
ca128d75 | 1151 | (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos))) |
8a592131 RS |
1152 | coding-system) |
1153 | (while (and alist (not coding-system)) | |
1154 | (if (string-match (car (car alist)) filename) | |
1155 | (setq coding-system (cdr (car alist))) | |
1156 | (setq alist (cdr alist)))) | |
1157 | coding-system)) | |
1158 | ||
502522b2 GM |
1159 | |
1160 | (defun auto-coding-from-file-contents (size) | |
1161 | "Determine a coding system from the contents of the current buffer. | |
1162 | The current buffer contains SIZE bytes starting at point. | |
1163 | Value is either a coding system or nil." | |
1164 | (save-excursion | |
1165 | (let ((alist auto-coding-regexp-alist) | |
1166 | coding-system) | |
1167 | (while (and alist (not coding-system)) | |
1168 | (let ((regexp (car (car alist)))) | |
1169 | (when (re-search-forward regexp (+ (point) size) t) | |
1170 | (setq coding-system (cdr (car alist))))) | |
1171 | (setq alist (cdr alist))) | |
1172 | coding-system))) | |
1173 | ||
1174 | ||
835f49b8 KH |
1175 | (defun set-auto-coding (filename size) |
1176 | "Return coding system for a file FILENAME of which SIZE bytes follow point. | |
1c4cc63a KH |
1177 | These bytes should include at least the first 1k of the file |
1178 | and the last 3k of the file, but the middle may be omitted. | |
63561304 | 1179 | |
502522b2 GM |
1180 | It checks FILENAME against the variable `auto-coding-alist'. If |
1181 | FILENAME doesn't match any entries in the variable, it checks the | |
1182 | contents of the current buffer following point against | |
1183 | `auto-coding-regexp-alist'. If no match is found, it checks for a | |
1184 | `coding:' tag in the first one or two lines following point. If no | |
1185 | `coding:' tag is found, it checks for local variables list in the last | |
1186 | 3K bytes out of the SIZE bytes. | |
63561304 KH |
1187 | |
1188 | The return value is the specified coding system, | |
1189 | or nil if nothing specified. | |
87aba788 | 1190 | |
ba74e833 | 1191 | The variable `set-auto-coding-function' (which see) is set to this |
87aba788 | 1192 | function by default." |
502522b2 GM |
1193 | (or (auto-coding-alist-lookup filename) |
1194 | (auto-coding-from-file-contents size) | |
1195 | (let* ((case-fold-search t) | |
1196 | (head-start (point)) | |
1197 | (head-end (+ head-start (min size 1024))) | |
1198 | (tail-start (+ head-start (max (- size 3072) 0))) | |
1199 | (tail-end (+ head-start size)) | |
1200 | coding-system head-found tail-found pos) | |
1201 | ;; Try a short cut by searching for the string "coding:" | |
1202 | ;; and for "unibyte:" at the head and tail of SIZE bytes. | |
1203 | (setq head-found (or (search-forward "coding:" head-end t) | |
1204 | (search-forward "unibyte:" head-end t))) | |
1205 | (if (and head-found (> head-found tail-start)) | |
1206 | ;; Head and tail are overlapped. | |
1207 | (setq tail-found head-found) | |
1208 | (goto-char tail-start) | |
1209 | (setq tail-found (or (search-forward "coding:" tail-end t) | |
1210 | (search-forward "unibyte:" tail-end t)))) | |
1211 | ||
1212 | ;; At first check the head. | |
1213 | (when head-found | |
1214 | (goto-char head-start) | |
6b66d028 RS |
1215 | (setq head-end (set-auto-mode-1)) |
1216 | (setq head-start (point)) | |
1d8e9a7c | 1217 | (when (and head-end (< head-found head-end)) |
835f49b8 | 1218 | (goto-char head-start) |
502522b2 GM |
1219 | (when (and set-auto-coding-for-load |
1220 | (re-search-forward | |
6b66d028 | 1221 | "\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)" |
502522b2 GM |
1222 | head-end t)) |
1223 | (setq coding-system 'raw-text)) | |
1224 | (when (and (not coding-system) | |
1225 | (re-search-forward | |
6b66d028 | 1226 | "\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" |
502522b2 GM |
1227 | head-end t)) |
1228 | (setq coding-system (intern (match-string 2))) | |
1229 | (or (coding-system-p coding-system) | |
1230 | (setq coding-system nil))))) | |
1231 | ||
1232 | ;; If no coding: tag in the head, check the tail. | |
1233 | (when (and tail-found (not coding-system)) | |
1234 | (goto-char tail-start) | |
1235 | (search-forward "\n\^L" nil t) | |
1236 | (if (re-search-forward | |
1237 | "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t) | |
1238 | ;; The prefix is what comes before "local variables:" in its | |
1239 | ;; line. The suffix is what comes after "local variables:" | |
1240 | ;; in its line. | |
1241 | (let* ((prefix (regexp-quote (match-string 1))) | |
1242 | (suffix (regexp-quote (match-string 2))) | |
1243 | (re-coding | |
1244 | (concat | |
1245 | "^" prefix | |
cfe98f50 GM |
1246 | ;; N.B. without the \n below, the regexp can |
1247 | ;; eat newlines. | |
1248 | "[ \t]*coding[ \t]*:[ \t]*\\([^ \t\n]+\\)[ \t]*" | |
502522b2 GM |
1249 | suffix "$")) |
1250 | (re-unibyte | |
1251 | (concat | |
1252 | "^" prefix | |
cfe98f50 | 1253 | "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t\n]+\\)[ \t]*" |
502522b2 GM |
1254 | suffix "$")) |
1255 | (re-end | |
cfe98f50 | 1256 | (concat "^" prefix "[ \t]*End *:[ \t]*" suffix "$")) |
502522b2 GM |
1257 | (pos (point))) |
1258 | (re-search-forward re-end tail-end 'move) | |
1259 | (setq tail-end (point)) | |
1260 | (goto-char pos) | |
1261 | (when (and set-auto-coding-for-load | |
1262 | (re-search-forward re-unibyte tail-end t)) | |
1263 | (setq coding-system 'raw-text)) | |
1264 | (when (and (not coding-system) | |
1265 | (re-search-forward re-coding tail-end t)) | |
1266 | (setq coding-system (intern (match-string 1))) | |
1267 | (or (coding-system-p coding-system) | |
1268 | (setq coding-system nil)))))) | |
1269 | coding-system))) | |
63561304 KH |
1270 | |
1271 | (setq set-auto-coding-function 'set-auto-coding) | |
87aba788 | 1272 | |
4ed46869 | 1273 | (defun after-insert-file-set-buffer-file-coding-system (inserted) |
521d4010 | 1274 | "Set `buffer-file-coding-system' of current buffer after text is inserted." |
4ed46869 KH |
1275 | (if last-coding-system-used |
1276 | (let ((coding-system | |
1277 | (find-new-buffer-file-coding-system last-coding-system-used)) | |
1278 | (modified-p (buffer-modified-p))) | |
0269ddfb | 1279 | (when coding-system |
71983219 | 1280 | (set-buffer-file-coding-system coding-system t) |
d0c26c63 | 1281 | (if (and enable-multibyte-characters |
c1841772 | 1282 | (or (eq (coding-system-type coding-system) 'raw-text)) |
136e48e4 KH |
1283 | ;; If buffer was unmodified and the size is the |
1284 | ;; same as INSERTED, we must be visiting it. | |
1285 | (not modified-p) | |
1286 | (= (buffer-size) inserted)) | |
ccb77b4e RS |
1287 | ;; For coding systems no-conversion and raw-text..., |
1288 | ;; edit the buffer as unibyte. | |
d0c26c63 KH |
1289 | (let ((pos-byte (position-bytes (+ (point) inserted)))) |
1290 | (set-buffer-multibyte nil) | |
1291 | (setq inserted (- pos-byte (position-bytes (point)))))) | |
0269ddfb | 1292 | (set-buffer-modified-p modified-p)))) |
d0c26c63 | 1293 | inserted) |
4ed46869 | 1294 | |
84c9d215 KH |
1295 | (add-hook 'after-insert-file-functions |
1296 | 'after-insert-file-set-buffer-file-coding-system) | |
4ed46869 | 1297 | |
8057896b | 1298 | ;; The coding-spec and eol-type of coding-system returned is decided |
4ed46869 KH |
1299 | ;; independently in the following order. |
1300 | ;; 1. That of buffer-file-coding-system locally bound. | |
1301 | ;; 2. That of CODING. | |
1302 | ||
1303 | (defun find-new-buffer-file-coding-system (coding) | |
1304 | "Return a coding system for a buffer when a file of CODING is inserted. | |
a73a8c89 KH |
1305 | The local variable `buffer-file-coding-system' of the current buffer |
1306 | is set to the returned value. | |
509064c5 | 1307 | Return nil if there's no need to set `buffer-file-coding-system'." |
4ed46869 | 1308 | (let (local-coding local-eol |
b685f8d6 | 1309 | found-coding found-eol |
4ed46869 KH |
1310 | new-coding new-eol) |
1311 | (if (null coding) | |
1312 | ;; Nothing found about coding. | |
1313 | nil | |
1314 | ||
b685f8d6 RS |
1315 | ;; Get information of `buffer-file-coding-system' in LOCAL-EOL |
1316 | ;; and LOCAL-CODING. | |
1317 | (setq local-eol (coding-system-eol-type buffer-file-coding-system)) | |
1318 | (if (null (numberp local-eol)) | |
1319 | ;; But eol-type is not yet set. | |
1320 | (setq local-eol nil)) | |
0269ddfb | 1321 | (if (and buffer-file-coding-system |
c1841772 KH |
1322 | (not (eq (coding-system-type buffer-file-coding-system) |
1323 | 'undecided))) | |
0269ddfb | 1324 | (setq local-coding (coding-system-base buffer-file-coding-system))) |
b685f8d6 RS |
1325 | |
1326 | (if (and (local-variable-p 'buffer-file-coding-system) | |
1327 | local-eol local-coding) | |
4ed46869 KH |
1328 | ;; The current buffer has already set full coding-system, we |
1329 | ;; had better not change it. | |
1330 | nil | |
1331 | ||
8057896b | 1332 | (setq found-eol (coding-system-eol-type coding)) |
4ed46869 | 1333 | (if (null (numberp found-eol)) |
be02cd54 EZ |
1334 | ;; But eol-type is not found. |
1335 | ;; If EOL conversions are inhibited, force unix eol-type. | |
1336 | (setq found-eol (if inhibit-eol-conversion 0))) | |
c1841772 | 1337 | (setq found-coding (coding-system-base coding)) |
c76b5c99 KH |
1338 | |
1339 | (if (and (not found-eol) (eq found-coding 'undecided)) | |
1340 | ;; No valid coding information found. | |
1341 | nil | |
1342 | ||
1343 | ;; Some coding information (eol or text) found. | |
1344 | ||
1345 | ;; The local setting takes precedence over the found one. | |
1346 | (setq new-coding (if (local-variable-p 'buffer-file-coding-system) | |
1347 | (or local-coding found-coding) | |
1348 | (or found-coding local-coding))) | |
1349 | (setq new-eol (if (local-variable-p 'buffer-file-coding-system) | |
1350 | (or local-eol found-eol) | |
1351 | (or found-eol local-eol))) | |
1352 | ||
1353 | (let ((eol-type (coding-system-eol-type new-coding))) | |
1354 | (if (and (numberp new-eol) (vectorp eol-type)) | |
1355 | (aref eol-type new-eol) | |
1356 | new-coding))))))) | |
4ed46869 | 1357 | |
fe831d33 GV |
1358 | (defun modify-coding-system-alist (target-type regexp coding-system) |
1359 | "Modify one of look up tables for finding a coding system on I/O operation. | |
8c453b46 RS |
1360 | There are three of such tables, `file-coding-system-alist', |
1361 | `process-coding-system-alist', and `network-coding-system-alist'. | |
fe831d33 GV |
1362 | |
1363 | TARGET-TYPE specifies which of them to modify. | |
8c453b46 RS |
1364 | If it is `file', it affects `file-coding-system-alist' (which see). |
1365 | If it is `process', it affects `process-coding-system-alist' (which see). | |
e8dd0160 | 1366 | If it is `network', it affects `network-coding-system-alist' (which see). |
fe831d33 GV |
1367 | |
1368 | REGEXP is a regular expression matching a target of I/O operation. | |
1369 | The target is a file name if TARGET-TYPE is `file', a program name if | |
1370 | TARGET-TYPE is `process', or a network service name or a port number | |
1371 | to connect to if TARGET-TYPE is `network'. | |
1372 | ||
1373 | CODING-SYSTEM is a coding system to perform code conversion on the I/O | |
8c453b46 RS |
1374 | operation, or a cons cell (DECODING . ENCODING) specifying the coding systems |
1375 | for decoding and encoding respectively, | |
1376 | or a function symbol which, when called, returns such a cons cell." | |
fe831d33 GV |
1377 | (or (memq target-type '(file process network)) |
1378 | (error "Invalid target type: %s" target-type)) | |
1379 | (or (stringp regexp) | |
1380 | (and (eq target-type 'network) (integerp regexp)) | |
1381 | (error "Invalid regular expression: %s" regexp)) | |
1382 | (if (symbolp coding-system) | |
1383 | (if (not (fboundp coding-system)) | |
1384 | (progn | |
1385 | (check-coding-system coding-system) | |
1386 | (setq coding-system (cons coding-system coding-system)))) | |
1387 | (check-coding-system (car coding-system)) | |
1388 | (check-coding-system (cdr coding-system))) | |
1389 | (cond ((eq target-type 'file) | |
1390 | (let ((slot (assoc regexp file-coding-system-alist))) | |
1391 | (if slot | |
1392 | (setcdr slot coding-system) | |
1393 | (setq file-coding-system-alist | |
1394 | (cons (cons regexp coding-system) | |
1395 | file-coding-system-alist))))) | |
1396 | ((eq target-type 'process) | |
1397 | (let ((slot (assoc regexp process-coding-system-alist))) | |
1398 | (if slot | |
1399 | (setcdr slot coding-system) | |
1400 | (setq process-coding-system-alist | |
1401 | (cons (cons regexp coding-system) | |
1402 | process-coding-system-alist))))) | |
1403 | (t | |
1404 | (let ((slot (assoc regexp network-coding-system-alist))) | |
1405 | (if slot | |
1406 | (setcdr slot coding-system) | |
1407 | (setq network-coding-system-alist | |
1408 | (cons (cons regexp coding-system) | |
1409 | network-coding-system-alist))))))) | |
1410 | ||
b25eef20 | 1411 | (defun make-translation-table (&rest args) |
a284eea3 | 1412 | "Make a translation table from arguments. |
d38b07f9 | 1413 | A translation table is a char table intended for character |
a284eea3 DL |
1414 | translation in CCL programs. |
1415 | ||
d38b07f9 | 1416 | Each argument is a list of elements of the form (FROM . TO), where FROM |
a284eea3 | 1417 | is a character to be translated to TO. |
13d5617d | 1418 | |
4e003d37 KH |
1419 | The arguments and forms in each argument are processed in the given |
1420 | order, and if a previous form already translates TO to some other | |
1421 | character, say TO-ALT, FROM is also translated to TO-ALT." | |
f967223b | 1422 | (let ((table (make-char-table 'translation-table)) |
a73a8c89 KH |
1423 | revlist) |
1424 | (while args | |
1425 | (let ((elts (car args))) | |
1426 | (while elts | |
13d5617d KH |
1427 | (let* ((from (car (car elts))) |
1428 | (from-i 0) ; degree of freedom of FROM | |
1429 | (from-rev (nreverse (split-char from))) | |
1430 | (to (cdr (car elts))) | |
1431 | (to-i 0) ; degree of freedom of TO | |
1432 | (to-rev (nreverse (split-char to)))) | |
1433 | ;; Check numbers of heading 0s in FROM-REV and TO-REV. | |
1434 | (while (eq (car from-rev) 0) | |
1435 | (setq from-i (1+ from-i) from-rev (cdr from-rev))) | |
1436 | (while (eq (car to-rev) 0) | |
1437 | (setq to-i (1+ to-i) to-rev (cdr to-rev))) | |
1438 | (if (and (/= from-i to-i) (/= to-i 0)) | |
1439 | (error "Invalid character pair (%d . %d)" from to)) | |
b25eef20 | 1440 | ;; If we have already translated TO to TO-ALT, FROM should |
687441de | 1441 | ;; also be translated to TO-ALT. |
13d5617d | 1442 | (let ((to-alt (aref table to))) |
687441de | 1443 | (if (and to-alt (> to-i 0)) |
13d5617d KH |
1444 | (setq to to-alt))) |
1445 | (if (> from-i 0) | |
1446 | (set-char-table-default table from to) | |
1447 | (aset table from to)) | |
b25eef20 KH |
1448 | ;; If we have already translated some chars to FROM, they |
1449 | ;; should also be translated to TO. | |
a73a8c89 KH |
1450 | (let ((l (assq from revlist))) |
1451 | (if l | |
1452 | (let ((ch (car l))) | |
1453 | (setcar l to) | |
1454 | (setq l (cdr l)) | |
1455 | (while l | |
1456 | (aset table ch to) | |
1457 | (setq l (cdr l)) )))) | |
1458 | ;; Now update REVLIST. | |
1459 | (let ((l (assq to revlist))) | |
1460 | (if l | |
1461 | (setcdr l (cons from (cdr l))) | |
1462 | (setq revlist (cons (list to from) revlist))))) | |
1463 | (setq elts (cdr elts)))) | |
1464 | (setq args (cdr args))) | |
1465 | ;; Return TABLE just created. | |
1466 | table)) | |
1467 | ||
c76b5c99 KH |
1468 | (defun make-translation-table-from-vector (vec) |
1469 | "Make translation table from decoding vector VEC. | |
9e3b6057 DL |
1470 | VEC is an array of 256 elements to map unibyte codes to multibyte |
1471 | characters. Elements may be nil for undefined code points. | |
c76b5c99 KH |
1472 | See also the variable `nonascii-translation-table'." |
1473 | (let ((table (make-char-table 'translation-table)) | |
1474 | (rev-table (make-char-table 'translation-table)) | |
c76b5c99 | 1475 | ch) |
9e3b6057 | 1476 | (dotimes (i 256) |
c76b5c99 | 1477 | (setq ch (aref vec i)) |
9e3b6057 DL |
1478 | (when ch |
1479 | (aset table i ch) | |
1480 | (if (>= ch 256) | |
1481 | (aset rev-table ch i)))) | |
c76b5c99 KH |
1482 | (set-char-table-extra-slot table 0 rev-table) |
1483 | table)) | |
1484 | ||
f967223b | 1485 | (defun define-translation-table (symbol &rest args) |
a284eea3 DL |
1486 | "Define SYMBOL as the name of translation table made by ARGS. |
1487 | This sets up information so that the table can be used for | |
1488 | translations in a CCL program. | |
b25eef20 | 1489 | |
a284eea3 DL |
1490 | If the first element of ARGS is a char-table whose purpose is |
1491 | `translation-table', just define SYMBOL to name it. (Note that this | |
1492 | function does not bind SYMBOL.) | |
007c79c8 | 1493 | |
a284eea3 | 1494 | Any other ARGS should be suitable as arguments of the function |
007c79c8 | 1495 | `make-translation-table' (which see). |
b25eef20 | 1496 | |
452fdb31 | 1497 | This function sets properties `translation-table' and |
521d4010 DL |
1498 | `translation-table-id' of SYMBOL to the created table itself and the |
1499 | identification number of the table respectively. It also registers | |
1500 | the table in `translation-table-vector'." | |
007c79c8 KH |
1501 | (let ((table (if (and (char-table-p (car args)) |
1502 | (eq (char-table-subtype (car args)) | |
1503 | 'translation-table)) | |
1504 | (car args) | |
1505 | (apply 'make-translation-table args))) | |
f967223b | 1506 | (len (length translation-table-vector)) |
d9e3229d | 1507 | (id 0) |
b25eef20 | 1508 | (done nil)) |
f967223b | 1509 | (put symbol 'translation-table table) |
b25eef20 KH |
1510 | (while (not done) |
1511 | (if (>= id len) | |
f967223b KH |
1512 | (setq translation-table-vector |
1513 | (vconcat translation-table-vector (make-vector len nil)))) | |
1514 | (let ((slot (aref translation-table-vector id))) | |
b25eef20 KH |
1515 | (if (or (not slot) |
1516 | (eq (car slot) symbol)) | |
1517 | (progn | |
f967223b | 1518 | (aset translation-table-vector id (cons symbol table)) |
007c79c8 KH |
1519 | (setq done t)) |
1520 | (setq id (1+ id))))) | |
f967223b | 1521 | (put symbol 'translation-table-id id) |
d9e3229d KH |
1522 | id)) |
1523 | ||
35554641 KH |
1524 | (put 'with-category-table 'lisp-indent-function 1) |
1525 | ||
1526 | (defmacro with-category-table (category-table &rest body) | |
07513d64 | 1527 | "Execute BODY like `progn' with CATEGORY-TABLE the current category table." |
687441de DL |
1528 | (let ((current-category-table (make-symbol "current-category-table"))) |
1529 | `(let ((,current-category-table (category-table))) | |
1530 | (set-category-table ,category-table) | |
1531 | (unwind-protect | |
1532 | (progn ,@body) | |
1533 | (set-category-table ,current-category-table))))) | |
35554641 | 1534 | |
69eba008 KH |
1535 | ;;; Initialize some variables. |
1536 | ||
1537 | (put 'use-default-ascent 'char-table-extra-slots 0) | |
1538 | (setq use-default-ascent (make-char-table 'use-default-ascent)) | |
d6d6d592 KH |
1539 | (put 'ignore-relative-composition 'char-table-extra-slots 0) |
1540 | (setq ignore-relative-composition | |
1541 | (make-char-table 'ignore-relative-composition)) | |
69eba008 KH |
1542 | |
1543 | ;;; | |
4ed46869 KH |
1544 | (provide 'mule) |
1545 | ||
1546 | ;;; mule.el ends here |