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