*** empty log message ***
[bpt/emacs.git] / lisp / international / mule.el
index 51ae883..ec633bf 100644 (file)
@@ -1,9 +1,9 @@
 ;;; mule.el --- basic commands for multilingual environment
 
 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
-;; Licensed to the Free Software Foundation.
+;;   Licensed to the Free Software Foundation.
 ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
-;; Copyright (C) 2001, 2002
+;; Copyright (C) 2003
 ;;   National Institute of Advanced Industrial Science and Technology (AIST)
 ;;   Registration Number H13PRO009
 
 
 ;;; Code:
 
-(defconst mule-version "7.0 (SAKAKI)" "\
+(defconst mule-version "6.0 (HANACHIRUSATO)" "\
 Version number and name of this version of MULE (multilingual environment).")
 
-(defconst mule-version-date "2002.2.28" "\
+(defconst mule-version-date "2003.9.1" "\
 Distribution date of this version of MULE (multilingual environment).")
 
 
@@ -258,7 +258,7 @@ Return t if file exists."
            (message "Loading %s (source)..." file)
          (message "Loading %s..." file)))
       (when purify-flag
-       (setq preloaded-file-list (cons file preloaded-file-list)))
+       (push file preloaded-file-list))
       (unwind-protect
          (let ((load-file-name fullname)
                (set-auto-coding-for-load t)
@@ -271,8 +271,8 @@ Return t if file exists."
              ;; Otherwise, eval-buffer might try to interpret random
              ;; binary junk as multibyte characters.
              (if (and enable-multibyte-characters
-                      (eq (coding-system-type last-coding-system-used)
-                          'raw-text))
+                      (or (eq (coding-system-type last-coding-system-used)
+                              'raw-text)))
                  (set-buffer-multibyte nil))
              ;; Make `kill-buffer' quiet.
              (set-buffer-modified-p nil))
@@ -295,9 +295,65 @@ Return t if file exists."
          (message "Loading %s...done" file)))
       t)))
 
-;; API (Application Program Interface) for charsets.
+(defun charset-info (charset)
+  "Return a vector of information of CHARSET.
+This function is provided for backward compatibility.
 
-;;; Charset property
+The elements of the vector are:
+       CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
+       LEADING-CODE-BASE, LEADING-CODE-EXT,
+       ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
+       REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
+       PLIST.
+where
+CHARSET-ID is always 0.
+BYTES is always 0.
+DIMENSION is the number of bytes of a code-point of the charset:
+  1, 2, 3, or 4.
+CHARS is the number of characters in a dimension:
+  94, 96, 128, or 256.
+WIDTH is always 0.
+DIRECTION is always 0.
+LEADING-CODE-BASE is always 0.
+LEADING-CODE-EXT is always 0.
+ISO-FINAL-CHAR (character) is the final character of the
+  corresponding ISO 2022 charset.  If the charset is not assigned
+  any final character, the value is -1.
+ISO-GRAPHIC-PLANE is always 0.
+REVERSE-CHARSET is always -1.
+SHORT-NAME (string) is the short name to refer to the charset.
+LONG-NAME (string) is the long name to refer to the charset
+DESCRIPTION (string) is the description string of the charset.
+PLIST (property list) may contain any type of information a user
+  want to put and get by functions `put-charset-property' and
+  `get-charset-property' respectively."
+  (vector 0
+         0
+         (charset-dimension charset)
+         (charset-chars charset)
+         0
+         0
+         0
+         0
+         (charset-iso-final-char charset)
+         0
+         -1
+         (get-charset-property charset :short-name)
+         (get-charset-property charset :short-name)
+         (charset-description charset)
+         (charset-plist charset)))
+
+;; It is better not to use backquote in this file,
+;; because that makes a bootstrapping problem
+;; if you need to recompile all the Lisp files using interpreted code.
+
+(defun charset-id (charset)
+  "Always return 0.  This is provided for backward compatibility."
+  0)
+
+(defmacro charset-bytes (charset)
+  "Always return 0.  This is provided for backward compatibility."
+  0)
 
 (defun get-charset-property (charset propname)
   "Return the value of CHARSET's PROPNAME property.
@@ -311,7 +367,6 @@ It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
   (set-charset-plist charset
                     (plist-put (charset-plist charset) propname value)))
 
-
 (defun charset-description (charset)
   "Return description string of CHARSET."
   (plist-get (charset-plist charset) :docstring))
@@ -351,7 +406,7 @@ Now we have the variable `charset-list'."
 (make-obsolete 'charset-list "Use variable `charset-list'" "22.1")
 
 (defun generic-char-p (char)
-  "Always return nil.  This exists only for backward compatibility."
+  "Always return nil.  This is provided for backward compatibility."
   nil)
 (make-obsolete 'generic-char-p "Generic characters no longer exist" "22.1")
 \f
@@ -512,6 +567,11 @@ arguments to this function are the same as those of a function in
 VALUE must be a character.  On encoding, a character not supported by
 the coding system is replaced with VALUE.
 
+`:for-unibyte'
+
+VALUE non-nil means that visiting a file with the coding system
+results in a unibyte buffer.
+
 `:eol-type'
 
 VALUE must be `unix', `dos', `mac'.  The symbol `unix' means Unix-like
@@ -603,6 +663,7 @@ encoding.  This attribute has a meaning only when `:coding-type' is
                                 :post-read-conversion
                                 :pre-write-conversion
                                 :default-char
+                                :prefer-unibyte
                                 :plist
                                 :eol-type)))
         (coding-type (plist-get props :coding-type))
@@ -669,7 +730,6 @@ encoding.  This attribute has a meaning only when `:coding-type' is
          (cons :name (cons name (cons :docstring (cons (purecopy docstring)
                                                        props)))))
     (setcdr (assq :plist common-attrs) props)
-
     (apply 'define-coding-system-internal 
           name (mapcar 'cdr (append common-attrs spec-attrs)))))
 
@@ -715,6 +775,17 @@ like `mime-charset' as well as the current style like `:mime-charset'."
 (defalias 'coding-system-parent 'coding-system-base)
 (make-obsolete 'coding-system-parent 'coding-system-base "20.3")
 
+(defun coding-system-eol-type-mnemonic (coding-system)
+  "Return the string indicating end-of-line format of CODING-SYSTEM."
+  (let* ((eol-type (coding-system-eol-type coding-system))
+        (val (cond ((eq eol-type 0) eol-mnemonic-unix)
+                   ((eq eol-type 1) eol-mnemonic-dos)
+                   ((eq eol-type 2) eol-mnemonic-mac)
+                   (t eol-mnemonic-undecided))))
+    (if (stringp val)
+       val
+      (char-to-string val))))
+
 (defun coding-system-lessp (x y)
   (cond ((eq x 'no-conversion) t)
        ((eq y 'no-conversion) nil)
@@ -765,47 +836,261 @@ formats (e.g. iso-latin-1-unix, koi8-r-dos)."
          (setq tail (cdr tail)))))
     codings))
 
+(defconst char-coding-system-table nil
+  "This is an obsolete variable.
+It exists just for backward compatibility, and the value is always nil.")
+
+(defun transform-make-coding-system-args (name type &optional doc-string props)
+  "For internal use only.
+Transform XEmacs style args for `make-coding-system' to Emacs style.
+Value is a list of transformed arguments."
+  (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
+       (eol-type (plist-get props 'eol-type))
+       properties tmp)
+    (cond
+     ((eq eol-type 'lf) (setq eol-type 'unix))
+     ((eq eol-type 'crlf) (setq eol-type 'dos))
+     ((eq eol-type 'cr) (setq eol-type 'mac)))
+    (if (setq tmp (plist-get props 'post-read-conversion))
+       (setq properties (plist-put properties 'post-read-conversion tmp)))
+    (if (setq tmp (plist-get props 'pre-write-conversion))
+       (setq properties (plist-put properties 'pre-write-conversion tmp)))
+    (cond
+     ((eq type 'shift-jis)
+      `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
+     ((eq type 'iso2022) ; This is not perfect.
+      (if (plist-get props 'escape-quoted)
+         (error "escape-quoted is not supported: %S"
+                `(,name ,type ,doc-string ,props)))
+      (let ((g0 (plist-get props 'charset-g0))
+           (g1 (plist-get props 'charset-g1))
+           (g2 (plist-get props 'charset-g2))
+           (g3 (plist-get props 'charset-g3))
+           (use-roman
+             (and
+             (eq (cadr (assoc 'latin-jisx0201
+                              (plist-get props 'input-charset-conversion)))
+                 'ascii)
+             (eq (cadr (assoc 'ascii
+                              (plist-get props 'output-charset-conversion)))
+                 'latin-jisx0201)))
+            (use-oldjis
+             (and
+             (eq (cadr (assoc 'japanese-jisx0208-1978
+                              (plist-get props 'input-charset-conversion)))
+                 'japanese-jisx0208)
+             (eq (cadr (assoc 'japanese-jisx0208
+                              (plist-get props 'output-charset-conversion)))
+                 'japanese-jisx0208-1978))))
+       (if (charsetp g0)
+           (if (plist-get props 'force-g0-on-output)
+               (setq g0 `(nil ,g0))
+             (setq g0 `(,g0 t))))
+       (if (charsetp g1)
+           (if (plist-get props 'force-g1-on-output)
+               (setq g1 `(nil ,g1))
+             (setq g1 `(,g1 t))))
+       (if (charsetp g2)
+           (if (plist-get props 'force-g2-on-output)
+               (setq g2 `(nil ,g2))
+             (setq g2 `(,g2 t))))
+       (if (charsetp g3)
+           (if (plist-get props 'force-g3-on-output)
+               (setq g3 `(nil ,g3))
+             (setq g3 `(,g3 t))))
+       `(,name 2 ,mnemonic ,doc-string
+         (,g0 ,g1 ,g2 ,g3
+          ,(plist-get props 'short)
+          ,(not (plist-get props 'no-ascii-eol))
+          ,(not (plist-get props 'no-ascii-cntl))
+          ,(plist-get props 'seven)
+          t
+          ,(not (plist-get props 'lock-shift))
+          ,use-roman
+          ,use-oldjis
+          ,(plist-get props 'no-iso6429)
+          nil nil nil nil)
+       ,properties ,eol-type)))
+     ((eq type 'big5)
+      `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
+     ((eq type 'ccl)
+      `(,name 4 ,mnemonic ,doc-string
+             (,(plist-get props 'decode) . ,(plist-get props 'encode))
+             ,properties ,eol-type))
+     (t
+      (error "unsupported XEmacs style make-coding-style arguments: %S"
+            `(,name ,type ,doc-string ,props))))))
+
+(defun make-coding-system (coding-system type mnemonic doc-string
+                                        &optional
+                                        flags
+                                        properties
+                                        eol-type)
+  "Define a new coding system CODING-SYSTEM (symbol).
+This function is provided for backward compatibility.
+Use `define-coding-system' instead."
+  ;; For compatiblity with XEmacs, we check the type of TYPE.  If it
+  ;; is a symbol, perhaps, this function is called with XEmacs-style
+  ;; arguments.  Here, try to transform that kind of arguments to
+  ;; Emacs style.
+  (if (symbolp type)
+      (let ((args (transform-make-coding-system-args coding-system type
+                                                    mnemonic doc-string)))
+       (setq coding-system (car args)
+             type (nth 1 args)
+             mnemonic (nth 2 args)
+             doc-string (nth 3 args)
+             flags (nth 4 args)
+             properties (nth 5 args)
+             eol-type (nth 6 args))))
+
+  (setq type
+       (cond ((eq type 0) 'emacs-mule)
+             ((eq type 1) 'shift-jis)
+             ((eq type 2) 'iso2022)
+             ((eq type 3) 'big5)
+             ((eq type 4) 'ccl)
+             ((eq type 5) 'raw-text)
+             (t
+              (error "Invalid coding system type: %s" type))))
+
+  (setq properties
+       (let ((plist nil) key)
+         (dolist (elt properties)
+           (setq key (car elt))
+           (cond ((eq key 'post-read-conversion)
+                  (setq key :post-read-conversion))
+                 ((eq key 'pre-write-conversion)
+                  (setq key :pre-write-conversion))
+                 ((eq key 'translation-table-for-decode)
+                  (setq key :decode-translation-table))
+                 ((eq key 'translation-table-for-encode)
+                  (setq key :encode-translation-table))
+                 ((eq key 'safe-charsets)
+                  (setq key :charset-list))
+                 ((eq key 'mime-charset)
+                  (setq key :mime-charset))
+                 ((eq key 'valid-codes)
+                  (setq key :valids)))
+           (setq plist (plist-put plist key (cdr elt))))
+         plist))
+  (plist-put properties :mnemonic mnemonic)
+  (plist-put properties :coding-type type)
+  (cond ((eq eol-type 0) (setq eol-type 'unix))
+       ((eq eol-type 1) (setq eol-type 'dos))
+       ((eq eol-type 2) (setq eol-type 'mac))
+       ((vectorp eol-type) (setq eol-type nil)))
+  (plist-put properties :eol-type eol-type)
+
+  (cond
+   ((eq type 'iso2022)
+    (plist-put properties :flags
+              (list (and (or (consp (nth 0 flags))
+                             (consp (nth 1 flags))
+                             (consp (nth 2 flags))
+                             (consp (nth 3 flags))) 'designation)
+                    (or (nth 4 flags) 'long-form)
+                    (and (nth 5 flags) 'ascii-at-eol)
+                    (and (nth 6 flags) 'ascii-at-cntl)
+                    (and (nth 7 flags) '7-bit)
+                    (and (nth 8 flags) 'locking-shift)
+                    (and (nth 9 flags) 'single-shift)
+                    (and (nth 10 flags) 'use-roman)
+                    (and (nth 11 flags) 'use-oldjis)
+                    (or (nth 12 flags) 'direction)
+                    (and (nth 13 flags) 'init-at-bol)
+                    (and (nth 14 flags) 'designate-at-bol)
+                    (and (nth 15 flags) 'safe)
+                    (and (nth 16 flags) 'latin-extra)))
+    (plist-put properties :designation
+              (let ((vec (make-vector 4 nil)))
+                (dotimes (i 4)
+                  (let ((spec (nth i flags)))
+                    (if (eq spec t)
+                        (aset vec i '(94 96))
+                    (if (consp spec)
+                        (progn
+                          (if (memq t spec)
+                              (setq spec (append (delq t spec) '(94 96))))
+                          (aset vec i spec))))))
+                vec)))
+
+   ((eq type 'ccl)
+    (plist-put properties :ccl-decoder (car flags))
+    (plist-put properties :ccl-encoder (cdr flags))))
+
+  (apply 'define-coding-system coding-system doc-string properties))
+
+(defun merge-coding-systems (first second)
+  "Fill in any unspecified aspects of coding system FIRST from SECOND.
+Return the resulting coding system."
+  (let ((base (coding-system-base second))
+       (eol (coding-system-eol-type second)))
+    ;; If FIRST doesn't specify text conversion, merge with that of SECOND.
+    (if (eq (coding-system-base first) 'undecided)
+       (setq first (coding-system-change-text-conversion first base)))
+    ;; If FIRST doesn't specify eol conversion, merge with that of SECOND.
+    (if (and (vectorp (coding-system-eol-type first))
+            (numberp eol) (>= eol 0) (<= eol 2))
+       (setq first (coding-system-change-eol-conversion
+                    first eol)))
+    first))
+
 (defun set-buffer-file-coding-system (coding-system &optional force)
   "Set the file coding-system of the current buffer to CODING-SYSTEM.
 This means that when you save the buffer, it will be converted
 according to CODING-SYSTEM.  For a list of possible values of CODING-SYSTEM,
 use \\[list-coding-systems].
 
-If the buffer's previous file coding-system value specifies end-of-line
-conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
-merged with the already-specified end-of-line conversion.
-
-If the buffer's previous file coding-system value specifies text
-conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
-merged with the already-specified text conversion.
-
-However, if the optional prefix argument FORCE is non-nil, then
-CODING-SYSTEM is used exactly as specified.
+If CODING-SYSTEM leaves the text conversion unspecified, or if it
+leaves the end-of-line conversion unspecified, FORCE controls what to
+do.  If FORCE is nil, get the unspecified aspect (or aspects) from the
+buffer's previous `buffer-file-coding-system' value (if it is
+specified there).  Otherwise, levae it unspecified.
 
 This marks the buffer modified so that the succeeding \\[save-buffer]
 surely saves the buffer with CODING-SYSTEM.  From a program, if you
 don't want to mark the buffer modified, just set the variable
 `buffer-file-coding-system' directly."
-  (interactive "zCoding system for visited file (default, nil): \nP")
+  (interactive "zCoding system for saving file (default, nil): \nP")
   (check-coding-system coding-system)
   (if (and coding-system buffer-file-coding-system (null force))
-      (let ((base (coding-system-base buffer-file-coding-system))
-           (eol (coding-system-eol-type buffer-file-coding-system)))
-       ;; If CODING-SYSTEM doesn't specify text conversion, merge
-       ;; with that of buffer-file-coding-system.
-       (if (eq (coding-system-base coding-system) 'undecided)
-           (setq coding-system (coding-system-change-text-conversion
-                                coding-system base)))
-       ;; If CODING-SYSTEM doesn't specify eol conversion, merge with
-       ;; that of buffer-file-coding-system.
-       (if (and (vectorp (coding-system-eol-type coding-system))
-                (numberp eol) (>= eol 0) (<= eol 2))
-           (setq coding-system (coding-system-change-eol-conversion
-                                coding-system eol)))))
+      (setq coding-system
+           (merge-coding-systems coding-system buffer-file-coding-system)))
   (setq buffer-file-coding-system coding-system)
+  ;; This is in case of an explicit call.  Normally, `normal-mode' and
+  ;; `set-buffer-major-mode-hook' take care of setting the table.
+  (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
+      (ucs-set-table-for-input))
   (set-buffer-modified-p t)
   (force-mode-line-update))
 
+(defun revert-buffer-with-coding-system (coding-system &optional force)
+  "Visit the current buffer's file again using coding system CODING-SYSTEM.
+For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
+
+If CODING-SYSTEM leaves the text conversion unspecified, or if it
+leaves the end-of-line conversion unspecified, FORCE controls what to
+do.  If FORCE is nil, get the unspecified aspect (or aspects) from the
+buffer's previous `buffer-file-coding-system' value (if it is
+specified there).  Otherwise, determine it from the file contents as
+usual for visiting a file."
+  (interactive "zCoding system for visited file (default, nil): \nP")
+  (check-coding-system coding-system)
+  (if (and coding-system buffer-file-coding-system (null force))
+      (setq coding-system
+           (merge-coding-systems coding-system buffer-file-coding-system)))
+  (let ((coding-system-for-read coding-system))
+    (revert-buffer)))
+
+(defun set-file-name-coding-system (coding-system)
+  "Set coding system for decoding and encoding file names to CODING-SYSTEM.
+It actually just set the variable `file-name-coding-system' (which
+see) to CODING-SYSTEM."
+  (interactive "zCoding system for file names (default, nil): ")
+  (check-coding-system coding-system)
+  (setq file-name-coding-system coding-system))
+
 (defvar default-terminal-coding-system nil
   "Default value for the terminal coding system.
 This is normally set according to the selected language environment.
@@ -870,6 +1155,8 @@ If you set this on a terminal which can't distinguish Meta keys from
 8-bit characters, you will have to use ESC to type Meta characters.
 See Info node `Specify Coding' and Info node `Single-Byte Character Support'.
 
+On non-windowing terminals, this is set from the locale by default.
+
 Setting this variable directly does not take effect;
 use either M-x customize or \\[set-keyboard-coding-system]."
   :type '(coding-system :tag "Coding system")
@@ -880,7 +1167,7 @@ use either M-x customize or \\[set-keyboard-coding-system]."
         (if (or value (boundp 'encoded-kbd-mode))
             (set-keyboard-coding-system value)
           (set-default 'keyboard-coding-system nil))) ; must initialize
-  :version "21.1"
+  :version "21.4"
   :group 'keyboard
   :group 'mule)
 
@@ -903,7 +1190,7 @@ For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
 
 (defun set-selection-coding-system (coding-system)
-  "Make CODING-SYSTEM used for communicating with other X clients .
+  "Make CODING-SYSTEM used for communicating with other X clients.
 When sending or receiving text via cut_buffer, selection, and clipboard,
 the text is encoded or decoded by CODING-SYSTEM."
   (interactive "zCoding system for X selection: ")
@@ -944,19 +1231,13 @@ Now we have more convenient function `set-coding-system-priority'."
 ;;; X selections
 
 (defvar ctext-non-standard-encodings-alist
-  '(("ISO8859-10" . iso-8859-10)
-    ("ISO8859-13" . iso-8859-13)
-    ("ISO8859-14" . iso-8859-14)
-    ("ISO8859-15" . iso-8859-15)
-    ("ISO8859-16" . iso-8859-16)
+  '(("ISO8859-15" . latin-iso8859-15)
+    ("ISO8859-14" . latin-iso8859-14)
     ("KOI8-R" . koi8-r)
     ("BIG5-0" . big5))
   "Alist of non-standard encoding names vs Emacs coding systems.
 This alist is used to decode an extened segment of a compound text.")
 
-;; Functions to support "Non-Standard Character Set Encodings" defined
-;; by the COMPOUND TEXT spec.
-
 (defvar ctext-non-standard-encodings-regexp
   (string-to-multibyte
    (concat
@@ -966,6 +1247,15 @@ This alist is used to decode an extened segment of a compound text.")
     ;; For UTF-8 encoding.
     "\\(\e%G[^\e]*\e%@\\)")))
 
+;; Functions to support "Non-Standard Character Set Encodings" defined
+;; by the COMPOUND-TEXT spec.
+;; We support that by decoding the whole data by `ctext' which just
+;; pertains byte sequences belonging to ``extended segment'', then
+;; decoding those byte sequences one by one in Lisp.
+;; This function also supports "The UTF-8 encoding" described in the
+;; section 7 of the documentation fo COMPOUND-TEXT distributed with
+;; XFree86.
+
 (defun ctext-post-read-conversion (len)
   "Decode LEN characters encoded as Compound Text with Extended Segments."
   ;; We don't need the following because it is expected that this
@@ -1007,16 +1297,19 @@ This alist is used to decode an extened segment of a compound text.")
       (goto-char (point-min))
       (- (point-max) (point)))))
 
+;; If you add charsets here, be sure to modify the regexp used by
+;; ctext-pre-write-conversion to look up non-standard charsets.
 (defvar ctext-non-standard-designations-alist
   '(("$(0" . (big5 "big5-0" 2))
     ("$(1" . (big5 "big5-0" 2))
     ;; The following are actually standard; generating extended
     ;; segments for them is wrong and screws e.g. Latin-9 users.
-    ;; ("-V"  . (t "iso8859-10" 1))
-    ;; ("-Y"  . (t "iso8859-13" 1))
-    ;; ("-_"  . (t "iso8859-14" 1))
-    ;; ("-b"  . (t "iso8859-15" 1))
-    ;; ("-f"  . (t "iso8859-16" 1))
+    ;; 8859-{10,13,16} aren't Emacs charsets anyhow.  -- fx
+;;     ("-V"  . (t "iso8859-10" 1))
+;;     ("-Y"  . (t "iso8859-13" 1))
+;;     ("-_"  . (t "iso8859-14" 1))
+;;     ("-b"  . (t "iso8859-15" 1))
+;;     ("-f"  . (t "iso8859-16" 1))
     )
   "Alist of ctext control sequences that introduce character sets which
 are not in the list of approved encodings, and the corresponding
@@ -1038,7 +1331,7 @@ of octets per character is variable), 1, 2, 3, or 4.")
   "Encode characters between FROM and TO as Compound Text w/Extended Segments.
 
 If FROM is a string, or if the current buffer is not the one set up for us
-by run_pre_post_conversion_on_str, generate a new temp buffer, insert the
+by encode-coding-string, generate a new temp buffer, insert the
 text, and convert it in the temporary buffer.  Otherwise, convert in-place."
   (save-match-data
     ;; Setup a working buffer if necessary.
@@ -1093,13 +1386,13 @@ text, and convert it in the temporary buffer.  Otherwise, convert in-place."
   ;; Must return nil, as build_annotations_2 expects that.
   nil)
 
-(make-obsolete 'set-coding-priority 'set-coding-system-priority "22.1")
-
 ;;; FILE I/O
 
 (defcustom auto-coding-alist
-  '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
-    ("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion))
+  '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|sx[dmicw]\\|tar\\)\\'" . no-conversion-multibyte)
+    ("\\.tgz\\'" . no-conversion)
+    ("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion)
+    ("/#[^/]+#\\'" . emacs-mule))
   "Alist of filename patterns vs corresponding coding systems.
 Each element looks like (REGEXP . CODING-SYSTEM).
 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
@@ -1127,6 +1420,27 @@ and the contents of `file-coding-system-alist'."
   :type '(repeat (cons (regexp :tag "Regexp")
                       (symbol :tag "Coding system"))))
 
+;; See the bottom of this file for built-in auto coding functions.
+(defcustom auto-coding-functions '(sgml-xml-auto-coding-function
+                                  sgml-html-meta-auto-coding-function)
+  "A list of functions which attempt to determine a coding system.
+
+Each function in this list should be written to operate on the
+current buffer, but should not modify it in any way.  The buffer
+will contain undecoded text of parts of the file.  Each function
+should take one argument, SIZE, which says how many
+characters (starting from point) it should look at.
+
+If one of these functions succeeds in determining a coding
+system, it should return that coding system.  Otherwise, it
+should return nil.
+
+If a file has a `coding:' tag, that takes precedence over these
+functions, so they won't be called at all."
+  :group 'files
+  :group 'mule
+  :type '(repeat function))
+
 (defvar set-auto-coding-for-load nil
   "Non-nil means look for `load-coding' property instead of `coding'.
 This is used for loading and byte-compiling Emacs Lisp files.")
@@ -1134,7 +1448,7 @@ This is used for loading and byte-compiling Emacs Lisp files.")
 (defun auto-coding-alist-lookup (filename)
   "Return the coding system specified by `auto-coding-alist' for FILENAME."
   (let ((alist auto-coding-alist)
-       (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
+       (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos cygwin)))
        coding-system)
     (while (and alist (not coding-system))
       (if (string-match (car (car alist)) filename)
@@ -1142,42 +1456,37 @@ This is used for loading and byte-compiling Emacs Lisp files.")
        (setq alist (cdr alist))))
     coding-system))
 
-
-(defun auto-coding-from-file-contents (size)
-  "Determine a coding system from the contents of the current buffer.
-The current buffer contains SIZE bytes starting at point.
-Value is either a coding system or nil."
-  (save-excursion
-    (let ((alist auto-coding-regexp-alist)
-         coding-system)
-      (while (and alist (not coding-system))
-       (let ((regexp (car (car alist))))
-         (when (re-search-forward regexp (+ (point) size) t)
-           (setq coding-system (cdr (car alist)))))
-       (setq alist (cdr alist)))
-      coding-system)))
-               
-
 (defun set-auto-coding (filename size)
   "Return coding system for a file FILENAME of which SIZE bytes follow point.
 These bytes should include at least the first 1k of the file
 and the last 3k of the file, but the middle may be omitted.
 
-It checks FILENAME against the variable `auto-coding-alist'.  If
-FILENAME doesn't match any entries in the variable, it checks the
+The function checks FILENAME against the variable `auto-coding-alist'.
+If FILENAME doesn't match any entries in the variable, it checks the
 contents of the current buffer following point against
 `auto-coding-regexp-alist'.  If no match is found, it checks for a
 `coding:' tag in the first one or two lines following point.  If no
-`coding:' tag is found, it checks for local variables list in the last
-3K bytes out of the SIZE bytes.
+`coding:' tag is found, it checks any local variables list in the last
+3K bytes out of the SIZE bytes.  Finally, if none of these methods
+succeed, it checks to see if any function in `auto-coding-functions'
+gives a match.
 
-The return value is the specified coding system,
-or nil if nothing specified.
+The return value is the specified coding system, or nil if nothing is
+specified.
 
 The variable `set-auto-coding-function' (which see) is set to this
 function by default."
   (or (auto-coding-alist-lookup filename)
-      (auto-coding-from-file-contents size)
+      ;; Try using `auto-coding-regexp-alist'.
+      (save-excursion
+       (let ((alist auto-coding-regexp-alist)
+             coding-system)
+         (while (and alist (not coding-system))
+           (let ((regexp (car (car alist))))
+             (when (re-search-forward regexp (+ (point) size) t)
+               (setq coding-system (cdr (car alist)))))
+           (setq alist (cdr alist)))
+         coding-system))
       (let* ((case-fold-search t)
             (head-start (point))
             (head-end (+ head-start (min size 1024)))
@@ -1252,35 +1561,35 @@ function by default."
                  (setq coding-system (intern (match-string 1)))
                  (or (coding-system-p coding-system)
                      (setq coding-system nil))))))
+       coding-system)
+      ;; Finally, try all the `auto-coding-functions'.
+      (let ((funcs auto-coding-functions)
+           (coding-system nil))
+       (while (and funcs (not coding-system))
+         (setq coding-system (condition-case e
+                                 (save-excursion
+                                   (goto-char (point-min))
+                                   (funcall (pop funcs) size))
+                               (error nil))))
        coding-system)))
 
 (setq set-auto-coding-function 'set-auto-coding)
 
-(defun after-insert-file-set-buffer-file-coding-system (inserted)
-  "Set `buffer-file-coding-system' of current buffer after text is inserted."
+(defun after-insert-file-set-coding (inserted)
+  "Set `buffer-file-coding-system' of current buffer after text is inserted.
+INSERTED is the number of characters that were inserted, as figured
+in the situation before this function.  Return the number of characters
+inserted, as figured in the situation after.  The two numbers can be
+different if the buffer has become unibyte."
   (if last-coding-system-used
       (let ((coding-system
             (find-new-buffer-file-coding-system last-coding-system-used))
            (modified-p (buffer-modified-p)))
        (when coding-system
          (set-buffer-file-coding-system coding-system t)
-         (if (and enable-multibyte-characters
-                  (or (eq (coding-system-type coding-system) 'raw-text))
-                  ;; If buffer was unmodified and the size is the
-                  ;; same as INSERTED, we must be visiting it.
-                  (not modified-p)
-                  (= (buffer-size) inserted))
-             ;; For coding systems no-conversion and raw-text...,
-             ;; edit the buffer as unibyte.
-             (let ((pos-byte (position-bytes (+ (point) inserted))))
-               (set-buffer-multibyte nil)
-               (setq inserted (- pos-byte (position-bytes (point))))))
          (set-buffer-modified-p modified-p))))
   inserted)
 
-(add-hook 'after-insert-file-functions
-         'after-insert-file-set-buffer-file-coding-system)
-
 ;; The coding-spec and eol-type of coding-system returned is decided
 ;; independently in the following order.
 ;;     1. That of buffer-file-coding-system locally bound.
@@ -1394,6 +1703,32 @@ or a function symbol which, when called, returns such a cons cell."
                   (cons (cons regexp coding-system)
                         network-coding-system-alist)))))))
 
+(defun decode-coding-inserted-region (from to filename
+                                          &optional visit beg end replace)
+  "Decode the region between FROM and TO as if it is read from file FILENAME.
+Optional arguments VISIT, BEG, END, and REPLACE are the same as those
+of the function `insert-file-contents'."
+  (save-excursion
+    (save-restriction
+      (narrow-to-region from to)
+      (goto-char (point-min))
+      (let ((coding coding-system-for-read))
+       (or coding
+           (setq coding (funcall set-auto-coding-function
+                                 filename (- (point-max) (point-min)))))
+       (or coding
+           (setq coding (find-operation-coding-system
+                         'insert-file-contents
+                         filename visit beg end replace)))
+       (if (coding-system-p coding)
+           (or enable-multibyte-characters
+               (setq coding
+                     (coding-system-change-text-conversion coding 'raw-text)))
+         (setq coding nil))
+       (if coding
+           (decode-coding-region (point-min) (point-max) coding))
+       (setq last-coding-system-used coding)))))
+
 (defun make-translation-table (&rest args)
   "Make a translation table from arguments.
 A translation table is a char table intended for character
@@ -1493,14 +1828,49 @@ the table in `translation-table-vector'."
 
 (put 'with-category-table 'lisp-indent-function 1)
 
-(defmacro with-category-table (category-table &rest body)
-  "Execute BODY like `progn' with CATEGORY-TABLE the current category table."
-  (let ((current-category-table (make-symbol "current-category-table")))
-    `(let ((,current-category-table (category-table)))
-       (set-category-table ,category-table)
+(defmacro with-category-table (table &rest body)
+  "Execute BODY like `progn' with CATEGORY-TABLE the current category table.
+The category table of the current buffer is saved, BODY is evaluated,
+then the saved table is restored, even in case of an abnormal exit.
+Value is what BODY returns."
+  (let ((old-table (make-symbol "old-table"))
+       (old-buffer (make-symbol "old-buffer")))
+    `(let ((,old-table (category-table))
+          (,old-buffer (current-buffer)))
        (unwind-protect
-          (progn ,@body)
-        (set-category-table ,current-category-table)))))
+          (progn
+            (set-category-table ,table)
+            ,@body)
+        (save-current-buffer
+          (set-buffer ,old-buffer)
+          (set-category-table ,old-table))))))
+
+(defun define-translation-hash-table (symbol table)
+  "Define SYMBOL as the name of the hash translation TABLE for use in CCL.
+
+Analogous to `define-translation-table', but updates
+`translation-hash-table-vector' and the table is for use in the CCL
+`lookup-integer' and `lookup-character' functions."
+  (unless (and (symbolp symbol)
+              (hash-table-p table))
+    (error "Bad args to define-translation-hash-table"))
+  (let ((len (length translation-hash-table-vector))
+       (id 0)
+       done)
+    (put symbol 'translation-hash-table table)
+    (while (not done)
+      (if (>= id len)
+         (setq translation-hash-table-vector
+               (vconcat translation-hash-table-vector [nil])))
+      (let ((slot (aref translation-hash-table-vector id)))
+       (if (or (not slot)
+               (eq (car slot) symbol))
+           (progn
+             (aset translation-hash-table-vector id (cons symbol table))
+             (setq done t))
+         (setq id (1+ id)))))
+    (put symbol 'translation-hash-table-id id)
+    id))
 
 ;;; Initialize some variables.
 
@@ -1512,6 +1882,44 @@ the table in `translation-table-vector'."
 
 (make-obsolete 'set-char-table-default
               "Generic characters no longer exist" "22.1")
+
+;;; Built-in auto-coding-functions:
+
+(defun sgml-xml-auto-coding-function (size)
+  "Determine whether the buffer is XML, and if so, its encoding.
+This function is intended to be added to `auto-coding-functions'."
+  (setq size (+ (point) size))
+  (when (re-search-forward "\\`[[:space:]\n]*<\\?xml" size t)
+    (let ((end (save-excursion
+                ;; This is a hack.
+                (re-search-forward "\"\\s-*\\?>" size t))))
+      (when end
+       (if (re-search-forward "encoding=\"\\(.+?\\)\"" end t)
+           (let* ((match (match-string 1))
+                  (sym (intern (downcase match))))
+             (if (coding-system-p sym)
+                 sym
+               (message "Warning: unknown coding system \"%s\"" match)
+               nil))
+         'utf-8)))))
+
+(defun sgml-html-meta-auto-coding-function (size)
+  "If the buffer has an HTML meta tag, use it to determine encoding.
+This function is intended to be added to `auto-coding-functions'."
+  (setq size (min (+ (point) size)
+                 ;; Only search forward 10 lines
+                 (save-excursion
+                   (forward-line 10)
+                   (point))))
+  (when (and (search-forward "<html>" size t)
+            (re-search-forward "<meta\\s-+http-equiv=\"content-type\"\\s-+content=\"text/\\sw+;\\s-*charset=\\(.+?\\)\"" size t))
+      (let* ((match (match-string 1))
+            (sym (intern (downcase match))))
+       (if (coding-system-p sym)
+           sym
+         (message "Warning: unknown coding system \"%s\"" match)
+         nil))))
+
 ;;;
 (provide 'mule)