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