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