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