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