]> code.delx.au - gnu-emacs/blobdiff - lisp/international/mule-cmds.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / international / mule-cmds.el
index 8e63729b7a5c17beaf87f0d03798b8a494398ba9..5bbe53e7ea7c2738a5b30e0c790b068796da924c 100644 (file)
@@ -1,8 +1,9 @@
-;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
+;;; mule-cmds.el --- commands for multilingual environment -*-coding: iso-2022-7bit -*-
 
-;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
-;;   Free Software Foundation, Inc.
-;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+;;   2006, 2007  Free Software Foundation, Inc.
+;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007
 ;;   National Institute of Advanced Industrial Science and Technology (AIST)
 ;;   Registration Number H14PRO021
 
@@ -12,7 +13,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -70,8 +71,7 @@
   (make-sparse-keymap "Set Coding System"))
 
 (define-key-after mule-menu-keymap [set-language-environment]
-  (list 'menu-item  "Set Language Environment" setup-language-environment-map
-       :help "Multilingual environment suitable for a specific language"))
+  (list 'menu-item  "Set Language Environment" setup-language-environment-map))
 (define-key-after mule-menu-keymap [separator-mule]
   '("--")
   t)
                    "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
 
 (defun coding-system-change-eol-conversion (coding-system eol-type)
-  "Return a coding system which differs from CODING-SYSTEM in eol conversion.
+  "Return a coding system which differs from CODING-SYSTEM in EOL conversion.
 The returned coding system converts end-of-line by EOL-TYPE
 but text as the same way as CODING-SYSTEM.
 EOL-TYPE should be `unix', `dos', `mac', or nil.
@@ -217,19 +217,21 @@ They means `unix', `dos', and `mac' respectively."
                           ((eq eol-type 'dos) 1)
                           ((eq eol-type 'mac) 2)
                           (t eol-type))))
-  (let ((orig-eol-type (coding-system-eol-type coding-system)))
-    (if (vectorp orig-eol-type)
-       (if (not eol-type)
-           coding-system
-         (aref orig-eol-type eol-type))
-      (let ((base (coding-system-base coding-system)))
-       (if (not eol-type)
-           base
-         (if (= eol-type orig-eol-type)
-             coding-system
-           (setq orig-eol-type (coding-system-eol-type base))
-           (if (vectorp orig-eol-type)
-               (aref orig-eol-type eol-type))))))))
+  ;; We call `coding-system-base' before `coding-system-eol-type',
+  ;; because the coding-system may not be initialized until then.
+  (let* ((base (coding-system-base coding-system))
+        (orig-eol-type (coding-system-eol-type coding-system)))
+    (cond ((vectorp orig-eol-type)
+          (if (not eol-type)
+              coding-system
+            (aref orig-eol-type eol-type)))
+         ((not eol-type)
+          base)
+         ((= eol-type orig-eol-type)
+          coding-system)
+         ((progn (setq orig-eol-type (coding-system-eol-type base))
+                 (vectorp orig-eol-type))
+          (aref orig-eol-type eol-type)))))
 
 (defun coding-system-change-text-conversion (coding-system coding)
   "Return a coding system which differs from CODING-SYSTEM in text conversion.
@@ -251,8 +253,8 @@ the buffer; it only changes the way those bytes are interpreted.
 In general, therefore, this command *changes* the sequence of
 characters that the current buffer contains.
 
-We suggest you avoid using use this command unless you know what you
-are doing.  If you use it by mistake, and the buffer is now displayed
+We suggest you avoid using this command unless you know what you are
+doing.  If you use it by mistake, and the buffer is now displayed
 wrong, use this command again to toggle back to the right mode."
   (interactive "P")
   (let ((new-flag
@@ -325,10 +327,10 @@ This sets the following coding systems:
   o default coding system for subprocess I/O
 This also sets the following values:
   o default value used as `file-name-coding-system' for converting file names
-      if CODING-SYSTEM is ASCII-compatible.
+      if CODING-SYSTEM is ASCII-compatible
   o default value for the command `set-terminal-coding-system' (not on MSDOS)
   o default value for the command `set-keyboard-coding-system'
-      if CODING-SYSTEM is ASCII-compatible.."
+      if CODING-SYSTEM is ASCII-compatible"
   (check-coding-system coding-system)
   (setq-default buffer-file-coding-system coding-system)
   (if (fboundp 'ucs-set-table-for-input)
@@ -336,11 +338,13 @@ This also sets the following values:
        (or (local-variable-p 'buffer-file-coding-system buffer)
            (ucs-set-table-for-input buffer))))
 
-  (if (and default-enable-multibyte-characters (not (eq system-type 'darwin))
-          (or (not coding-system)
-              (not (coding-system-get coding-system 'ascii-incompatible))))
+  (if (eq system-type 'darwin)
       ;; The file-name coding system on Darwin systems is always utf-8.
-      (setq default-file-name-coding-system coding-system))
+      (setq default-file-name-coding-system 'utf-8)
+    (if (and default-enable-multibyte-characters
+            (or (not coding-system)
+                (not (coding-system-get coding-system 'ascii-incompatible))))
+       (setq default-file-name-coding-system coding-system)))
   ;; If coding-system is nil, honor that on MS-DOS as well, so
   ;; that they could reset the terminal coding system.
   (unless (and (eq window-system 'pc) coding-system)
@@ -368,7 +372,7 @@ This also sets the following coding systems:
   o coding system of a newly created buffer
   o default coding system for subprocess I/O
 This also sets the following values:
-  o default value used as `file-name-coding-system' for converting file names.
+  o default value used as `file-name-coding-system' for converting file names
   o default value for the command `set-terminal-coding-system' (not on MSDOS)
   o default value for the command `set-keyboard-coding-system'
 
@@ -419,15 +423,16 @@ The function `sort-coding-systems' use it.")
 
 (defun sort-coding-systems (codings)
   "Sort coding system list CODINGS by a priority of each coding system.
+Return the sorted list.  CODINGS is modified by side effects.
 
 If a coding system is most preferred, it has the highest priority.
-Otherwise, a coding system corresponds to some MIME charset has higher
-priorities.  Among them, a coding system included in `coding-system'
-key of the current language environment has higher priorities.  See
-also the documentation of `language-info-alist'.
+Otherwise, coding systems that correspond to MIME charsets have
+higher priorities.  Among them, a coding system included in the
+`coding-system' key of the current language environment has higher
+priority.  See also the documentation of `language-info-alist'.
 
 If the variable `sort-coding-systems-predicate' (which see) is
-non-nil, it is used to sort CODINGS in the different way than above."
+non-nil, it is used to sort CODINGS instead."
   (if sort-coding-systems-predicate
       (sort codings sort-coding-systems-predicate)
     (let* ((from-categories (mapcar #'(lambda (x) (symbol-value x))
@@ -561,7 +566,7 @@ where
   COUNT is a number of characters,
   CHARs are the characters found from the character set.
 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
-Optional 4th arg EXCLUDE is a list of character sets to be ignored.
+Optional 4th arg EXCLUDES is a list of character sets to be ignored.
 
 For invalid characters, CHARs are actually strings."
   (let ((chars nil)
@@ -611,8 +616,8 @@ It asks which coding system to check.
 If such a character is found, set point after that character.
 Otherwise, don't move point.
 
-When called from a program, the value is a position of the found character,
-or nil if all characters are encodable."
+When called from a program, the value is the position of the unencodable
+character found, or nil if all characters are encodable."
   (interactive
    (list (let ((default (or buffer-file-coding-system 'us-ascii)))
           (read-coding-system
@@ -732,18 +737,18 @@ DEFAULT is the coding system to use by default in the query."
              (insert "\n")
              (fill-region-as-paragraph pos (point)))
            (when rejected
-             (insert "These safely encodes the target text,
-but it is not recommended for encoding text in this context,
+             (insert "These safely encode the text in the buffer,
+but are not recommended for encoding text in this context,
 e.g., for sending an email message.\n ")
              (dolist (x rejected)
                (princ " ") (princ x))
              (insert "\n"))
            (when unsafe
-             (insert (if rejected "And the others"
+             (insert (if rejected "The other coding systems"
                        "However, each of them")
-                     " encountered these problematic characters:\n")
+                     " encountered characters it couldn't encode:\n")
              (dolist (coding unsafe)
-               (insert (format "  %s:" (car coding)))
+               (insert (format "  %s cannot encode these:" (car coding)))
                (let ((i 0)
                      (func1
                       #'(lambda (bufname pos)
@@ -767,6 +772,7 @@ e.g., for sending an email message.\n ")
                          (insert-text-button
                           (cdr elt)
                           :type 'help-xref
+                          'face 'link
                           'help-echo
                           "mouse-2, RET: jump to this character"
                           'help-function func1
@@ -774,6 +780,7 @@ e.g., for sending an email message.\n ")
                        (insert-text-button
                         "..."
                         :type 'help-xref
+                        'face 'link
                         'help-echo
                         "mouse-2, RET: next unencodable character"
                         'help-function func2
@@ -781,20 +788,23 @@ e.g., for sending an email message.\n ")
                                          (car coding)))))
                    (setq i (1+ i))))
                (insert "\n"))
-             (insert "\
-The first problematic character is at point in the displayed buffer,\n"
-                     (substitute-command-keys "\
-and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
-         (insert "\nSelect \
-one of the following safe coding systems, or edit the buffer:\n")
+             (insert (substitute-command-keys "\
+
+Click on a character (or switch to this window by `\\[other-window]'\n\
+and select the characters by RET) to jump to the place it appears,\n\
+where `\\[universal-argument] \\[what-cursor-position]' will give information about it.\n"))))
+         (insert (substitute-command-keys "\nSelect \
+one of the safe coding systems listed below,\n\
+or cancel the writing with \\[keyboard-quit] and edit the buffer\n\
+   to remove or modify the problematic characters,\n\
+or specify any other coding system (and risk losing\n\
+   the problematic characters).\n\n"))
          (let ((pos (point))
                (fill-prefix "  "))
            (dolist (x codings)
              (princ "  ") (princ x))
            (insert "\n")
-           (fill-region-as-paragraph pos (point)))
-         (insert "Or specify any other coding system
-at the risk of losing the problematic characters.\n")))
+           (fill-region-as-paragraph pos (point)))))
 
       ;; Read a coding system.
       (setq coding-system
@@ -831,7 +841,7 @@ re-visited and edited.)
 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
 list of coding systems to be prepended to the default coding system
 list.  However, if DEFAULT-CODING-SYSTEM is a list and the first
-element is t, the cdr part is used as the defualt coding system list,
+element is t, the cdr part is used as the default coding system list,
 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
 and the most preferred coding system are not used.
 
@@ -855,7 +865,7 @@ and TO is ignored."
 
   (let ((no-other-defaults nil)
        auto-cs)
-    (unless (or (stringp from) find-file-literally)    
+    (unless (or (stringp from) find-file-literally)
       ;; Find an auto-coding that is specified for the the current
       ;; buffer and file from the region FROM and TO.
       (save-excursion
@@ -898,9 +908,6 @@ It is highly recommended to fix it before writing to a file."
              (rassq base default-coding-system)
              (push (cons auto-cs base) default-coding-system))))
 
-    ;; From now on, the list of defaults is reversed.
-    (setq default-coding-system (nreverse default-coding-system))
-
     (unless no-other-defaults
       ;; If buffer-file-coding-system is not nil nor undecided, append it
       ;; to the defaults.
@@ -908,8 +915,9 @@ It is highly recommended to fix it before writing to a file."
          (let ((base (coding-system-base buffer-file-coding-system)))
            (or (eq base 'undecided)
                (rassq base default-coding-system)
-               (push (cons buffer-file-coding-system base)
-                     default-coding-system))))
+               (setq default-coding-system
+                     (append default-coding-system
+                             (list (cons buffer-file-coding-system base)))))))
 
       ;; If default-buffer-file-coding-system is not nil nor undecided,
       ;; append it to the defaults.
@@ -917,8 +925,10 @@ It is highly recommended to fix it before writing to a file."
          (let ((base (coding-system-base default-buffer-file-coding-system)))
            (or (eq base 'undecided)
                (rassq base default-coding-system)
-               (push (cons default-buffer-file-coding-system base)
-                     default-coding-system))))
+               (setq default-coding-system
+                     (append default-coding-system
+                             (list (cons default-buffer-file-coding-system
+                                         base)))))))
 
       ;; If the most preferred coding system has the property mime-charset,
       ;; append it to the defaults.
@@ -930,18 +940,40 @@ It is highly recommended to fix it before writing to a file."
             (setq base (coding-system-base preferred))
             (coding-system-get preferred 'mime-charset)
             (not (rassq base default-coding-system))
-            (push (cons preferred base)
-                  default-coding-system))))
+            (setq default-coding-system
+                  (append default-coding-system
+                          (list (cons preferred base)))))))
 
     (if select-safe-coding-system-accept-default-p
        (setq accept-default-p select-safe-coding-system-accept-default-p))
 
+    ;; Decide the eol-type from the top of the default codings,
+    ;; buffer-file-coding-system, or
+    ;; default-buffer-file-coding-system.
+    (if default-coding-system
+       (let ((default-eol-type (coding-system-eol-type
+                                (caar default-coding-system))))
+         (if (and (vectorp default-eol-type) buffer-file-coding-system)
+             (setq default-eol-type (coding-system-eol-type
+                                     buffer-file-coding-system)))
+         (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
+             (setq default-eol-type (coding-system-eol-type
+                                     default-buffer-file-coding-system)))
+         (if (and default-eol-type (not (vectorp default-eol-type)))
+             (dolist (elt default-coding-system)
+               (setcar elt (coding-system-change-eol-conversion
+                            (car elt) default-eol-type))))))
+
     (let ((codings (find-coding-systems-region from to))
          (coding-system nil)
          safe rejected unsafe)
       (if (eq (car codings) 'undecided)
          ;; Any coding system is ok.
-         (setq coding-system t)
+         (setq coding-system (caar default-coding-system))
+       ;; Reverse the list so that elements are accumulated in safe,
+       ;; rejected, and unsafe in the correct order.
+       (setq default-coding-system (nreverse default-coding-system))
+
        ;; Classify the defaults into safe, rejected, and unsafe.
        (dolist (elt default-coding-system)
          (if (memq (cdr elt) codings)
@@ -958,14 +990,6 @@ It is highly recommended to fix it before writing to a file."
        (setq coding-system (select-safe-coding-system-interactively
                             from to codings unsafe rejected (car codings))))
 
-      (if (vectorp (coding-system-eol-type coding-system))
-         (let ((eol (coding-system-eol-type buffer-file-coding-system)))
-           (if (numberp eol)
-               (setq coding-system
-                     (coding-system-change-eol-conversion coding-system eol)))))
-
-      (if (eq coding-system t)
-         (setq coding-system buffer-file-coding-system))
       ;; Check we're not inconsistent with what `coding:' spec &c would
       ;; give when file is re-read.
       ;; But don't do this if we explicitly ignored the cookie
@@ -1128,7 +1152,19 @@ see `language-info-alist'."
       (setq lang-env (symbol-name lang-env)))
   (set-language-info-internal lang-env key info)
   (if (equal lang-env current-language-environment)
-      (set-language-environment lang-env)))
+      (cond ((eq key 'coding-priority)
+            (set-language-environment-coding-systems lang-env))
+           ((eq key 'input-method)
+            (set-language-environment-input-method lang-env))
+           ((eq key 'nonascii-translation)
+            (set-language-environment-nonascii-translation lang-env))
+           ((eq key 'charset)
+            (set-language-environment-charset lang-env))
+           ((eq key 'overriding-fontspec)
+            (set-language-environment-fontset lang-env))
+           ((and (not default-enable-multibyte-characters)
+                 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
+            (set-language-environment-unibyte lang-env)))))
 
 (defun set-language-info-internal (lang-env key info)
   "Internal use only.
@@ -1205,7 +1241,7 @@ in the European submenu in each of those two menus."
 
     (dolist (elt alist)
       (set-language-info-internal lang-env (car elt) (cdr elt)))
-    
+
     (if (equal lang-env current-language-environment)
        (set-language-environment lang-env))))
 
@@ -1259,7 +1295,7 @@ Emacs loads this file at startup time.")
 
 (defvar leim-list-entry-regexp "^(register-input-method"
   "Regexp matching head of each entry in LEIM list file.
-See also the variable `leim-list-header'")
+See also the variable `leim-list-header'.")
 
 (defvar update-leim-list-functions
   '(quail-update-leim-list-file)
@@ -1328,10 +1364,8 @@ See the function `register-input-method' for the meanings of the elements.")
 
 (defun register-input-method (input-method lang-env &rest args)
   "Register INPUT-METHOD as an input method for language environment LANG-ENV.
-INPUT-METHOD and LANG-ENV are symbols or strings.
 
-The remaining arguments are:
-       ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
+INPUT-METHOD and LANG-ENV are symbols or strings.
 ACTIVATE-FUNC is a function to call to activate this method.
 TITLE is a string to show in the mode line when this method is active.
 DESCRIPTION is a string describing this method and what it is good for.
@@ -1349,7 +1383,8 @@ string specified in this function takes precedence.)
 
 The commands `describe-input-method' and `list-input-methods' need
 these duplicated values to show some information about input methods
-without loading the relevant Quail packages."
+without loading the relevant Quail packages.
+\n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
   (if (symbolp lang-env)
       (setq lang-env (symbol-name lang-env)))
   (if (symbolp input-method)
@@ -1444,7 +1479,7 @@ When called interactively, the optional arg INTERACTIVE is non-nil,
 which marks the variable `default-input-method' as set for Custom buffers.
 
 To deactivate the input method interactively, use \\[toggle-input-method].
-To deactivate it programmatically, use \\[inactivate-input-method]."
+To deactivate it programmatically, use `inactivate-input-method'."
   (interactive
    (let* ((default (or (car input-method-history) default-input-method)))
      (list (read-input-method-name
@@ -1613,7 +1648,7 @@ just inactivated.")
   "This flag controls when an input method returns.
 Usually, the input method does not return while there's a possibility
 that it may find a different translation if a user types another key.
-But, it this flag is non-nil, the input method returns as soon as
+But, if this flag is non-nil, the input method returns as soon as
 the current key sequence gets long enough to have some valid translation.")
 
 (defvar input-method-use-echo-area nil
@@ -1645,7 +1680,7 @@ When this hook is run, the variable `current-language-environment'
 is still bound to the language environment being exited.
 
 This hook is mainly used for canceling the effect of
-`set-language-environment-hook' (which-see).")
+`set-language-environment-hook' (which see).")
 
 (put 'setup-specified-language-environment 'apropos-inhibit t)
 
@@ -1835,92 +1870,29 @@ specifies the character set for the major languages of Western Europe."
                                     'exit-function)))
        (run-hooks 'exit-language-environment-hook)
        (if (functionp func) (funcall func))))
-  (let ((default-eol-type (coding-system-eol-type
-                          default-buffer-file-coding-system)))
-    (reset-language-environment)
-
-    ;; The features might set up coding systems.
-    (let ((required-features (get-language-info language-name 'features)))
-      (while required-features
-       (require (car required-features))
-       (setq required-features (cdr required-features))))
-
-    (setq current-language-environment language-name)
-    (set-language-environment-coding-systems language-name default-eol-type))
-  (let ((input-method (get-language-info language-name 'input-method)))
-    (when input-method
-      (setq default-input-method input-method)
-      (if input-method-history
-         (setq input-method-history
-               (cons input-method
-                     (delete input-method input-method-history))))))
-  (let ((nonascii (get-language-info language-name 'nonascii-translation))
-       (dos-table
-        (if (eq window-system 'pc)
-            (intern
-             (format "cp%d-nonascii-translation-table" dos-codepage)))))
-    (cond
-     ((char-table-p nonascii)
-      (setq nonascii-translation-table nonascii))
-     ((and (eq window-system 'pc) (boundp dos-table))
-      ;; DOS terminals' default is to use a special non-ASCII translation
-      ;; table as appropriate for the installed codepage.
-      (setq nonascii-translation-table (symbol-value dos-table)))
-     ((charsetp nonascii)
-      (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
-
-  ;; Unibyte setups if necessary.
-  (unless default-enable-multibyte-characters
-    ;; Syntax and case table.
-    (let ((syntax (get-language-info language-name 'unibyte-syntax)))
-      (if syntax
-         (let ((set-case-syntax-set-multibyte nil))
-           (load syntax nil t))
-       ;; No information for syntax and case.  Reset to the defaults.
-       (let ((syntax-table (standard-syntax-table))
-             (standard-table (standard-case-table))
-             (case-table (make-char-table 'case-table))
-             (ch (if (eq window-system 'pc) 128 160)))
-         (while (< ch 256)
-           (modify-syntax-entry ch " " syntax-table)
-           (setq ch (1+ ch)))
-         (dotimes (i 128)
-           (aset case-table i (aref standard-table i)))
-         (set-char-table-extra-slot case-table 0 nil)
-         (set-char-table-extra-slot case-table 1 nil)
-         (set-char-table-extra-slot case-table 2 nil)
-         (set-standard-case-table case-table))
-       (let ((list (buffer-list)))
-         (while list
-           (with-current-buffer (car list)
-             (set-case-table (standard-case-table)))
-           (setq list (cdr list))))))
-    (set-display-table-and-terminal-coding-system language-name))
 
+  (reset-language-environment)
+  ;; The features might set up coding systems.
   (let ((required-features (get-language-info language-name 'features)))
     (while required-features
       (require (car required-features))
       (setq required-features (cdr required-features))))
 
-  ;; Don't invoke fontset-related functions if fontsets aren't
-  ;; supported in this build of Emacs.
-  (when (fboundp 'fontset-list)
-    (let ((overriding-fontspec (get-language-info language-name
-                                                 'overriding-fontspec)))
-      (if overriding-fontspec
-         (set-overriding-fontspec-internal overriding-fontspec))))
+  (setq current-language-environment language-name)
+
+  (set-language-environment-coding-systems language-name)
+  (set-language-environment-input-method language-name)
+  (set-language-environment-nonascii-translation language-name)
+  (set-language-environment-charset language-name)
+  (set-language-environment-fontset language-name)
+  ;; Unibyte setups if necessary.
+  (unless default-enable-multibyte-characters
+    (set-language-environment-unibyte language-name))
 
   (let ((func (get-language-info language-name 'setup-function)))
     (if (functionp func)
        (funcall func)))
-  (if (and utf-translate-cjk-mode
-          (not (eq utf-translate-cjk-lang-env language-name))
-          (catch 'tag
-            (dolist (charset (get-language-info language-name 'charset))
-              (if (memq charset utf-translate-cjk-charsets)
-                  (throw 'tag t)))
-            nil))
-      (utf-translate-cjk-load-tables))
+
   (run-hooks 'set-language-environment-hook)
   (force-mode-line-update t))
 
@@ -1949,14 +1921,11 @@ specifies the character set for the major languages of Western Europe."
        ;; proper windows-1252 coding system.  --fx]
        (aset standard-display-table 146 [39]))))
 
-(defun set-language-environment-coding-systems (language-name
-                                               &optional eol-type)
-  "Do various coding system setups for language environment LANGUAGE-NAME.
-
-The optional arg EOL-TYPE specifies the eol-type of the default value
-of `buffer-file-coding-system' set by this function."
+(defun set-language-environment-coding-systems (language-name)
+  "Do various coding system setups for language environment LANGUAGE-NAME."
   (let* ((priority (get-language-info language-name 'coding-priority))
-        (default-coding (car priority)))
+        (default-coding (car priority))
+        (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
     (if priority
        (let ((categories (mapcar 'coding-system-category priority)))
          (set-default-coding-systems
@@ -1971,8 +1940,82 @@ of `buffer-file-coding-system' set by this function."
          ;; Changing the binding of a coding category requires this call.
          (update-coding-systems-internal)))))
 
+(defun set-language-environment-input-method (language-name)
+  "Do various input method setups for language environment LANGUAGE-NAME."
+  (let ((input-method (get-language-info language-name 'input-method)))
+    (when input-method
+      (setq default-input-method input-method)
+      (if input-method-history
+         (setq input-method-history
+               (cons input-method
+                     (delete input-method input-method-history)))))))
+
+(defun set-language-environment-nonascii-translation (language-name)
+  "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
+  (let ((nonascii (get-language-info language-name 'nonascii-translation))
+       (dos-table
+        (if (eq window-system 'pc)
+            (intern
+             (format "cp%d-nonascii-translation-table" dos-codepage)))))
+    (cond
+     ((char-table-p nonascii)
+      (setq nonascii-translation-table nonascii))
+     ((and (eq window-system 'pc) (boundp dos-table))
+      ;; DOS terminals' default is to use a special non-ASCII translation
+      ;; table as appropriate for the installed codepage.
+      (setq nonascii-translation-table (symbol-value dos-table)))
+     ((charsetp nonascii)
+      (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
+
+(defun set-language-environment-charset (language-name)
+  "Do various charset setups for language environment LANGUAGE-NAME."
+  (if (and utf-translate-cjk-mode
+          (not (eq utf-translate-cjk-lang-env language-name))
+          (catch 'tag
+            (dolist (charset (get-language-info language-name 'charset))
+              (if (memq charset utf-translate-cjk-charsets)
+                  (throw 'tag t)))
+            nil))
+      (utf-translate-cjk-load-tables)))
+
+(defun set-language-environment-fontset (language-name)
+  "Do various fontset setups for language environment LANGUAGE-NAME."
+  ;; Don't invoke fontset-related functions if fontsets aren't
+  ;; supported in this build of Emacs.
+  (if (fboundp 'fontset-list)
+      (set-overriding-fontspec-internal
+       (get-language-info language-name 'overriding-fontspec))))
+
+(defun set-language-environment-unibyte (language-name)
+  "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
+  ;; Syntax and case table.
+  (let ((syntax (get-language-info language-name 'unibyte-syntax)))
+    (if syntax
+       (let ((set-case-syntax-set-multibyte nil))
+         (load syntax nil t))
+      ;; No information for syntax and case.  Reset to the defaults.
+      (let ((syntax-table (standard-syntax-table))
+           (standard-table (standard-case-table))
+           (case-table (make-char-table 'case-table))
+           (ch (if (eq window-system 'pc) 128 160)))
+       (while (< ch 256)
+         (modify-syntax-entry ch " " syntax-table)
+         (setq ch (1+ ch)))
+       (dotimes (i 128)
+         (aset case-table i (aref standard-table i)))
+       (set-char-table-extra-slot case-table 0 nil)
+       (set-char-table-extra-slot case-table 1 nil)
+       (set-char-table-extra-slot case-table 2 nil)
+       (set-standard-case-table case-table))
+      (let ((list (buffer-list)))
+       (while list
+         (with-current-buffer (car list)
+           (set-case-table (standard-case-table)))
+         (setq list (cdr list))))))
+  (set-display-table-and-terminal-coding-system language-name))
+
 (defsubst princ-list (&rest args)
-  "Print all arguments with `princ', then print \"\n\"."
+  "Print all arguments with `princ', then print \"\\n\"."
   (while args (princ (car args)) (setq args (cdr args)))
   (princ "\n"))
 
@@ -2134,6 +2177,7 @@ of `buffer-file-coding-system' set by this function."
     ("cy" "Welsh" iso-8859-14)
     ("da" . "Latin-1") ; Danish
     ("de" "German" iso-8859-1)
+    ; dv Divehi
     ; dz Bhutani
     ("el" "Greek" iso-8859-7)
     ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
@@ -2141,7 +2185,7 @@ of `buffer-file-coding-system' set by this function."
     ;; en_IN -- fx.
     ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
     ("en" "English" iso-8859-1) ; English
-    ("eo" . "Latin-3") ; Esperanto
+    ("eo" . "Esperanto") ; Esperanto
     ("es" "Spanish" iso-8859-1)
     ("et" . "Latin-1") ; Estonian
     ("eu" . "Latin-1") ; Basque
@@ -2295,12 +2339,16 @@ of `buffer-file-coding-system' set by this function."
     ("jp" . "Japanese") ; e.g. MS Windows
     ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
     ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
+    ("gbz" . "UTF-8") ; MS Windows Dari Persian
+    ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
+    ("wee" . "Latin-2") ; MS Windows Lower Sorbian
+    ("wen" . "Latin-2") ; MS Windows Upper Sorbian
     ))
   "Alist of locale regexps vs the corresponding languages and coding systems.
-Each element has these form:
+Each element has this form:
   \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
 The first element whose LOCALE-REGEXP matches the start of a
-downcased locale specifies the LANG-ENV \(language environtment)
+downcased locale specifies the LANG-ENV \(language environment)
 and CODING-SYSTEM corresponding to that locale.  If there is no
 appropriate language environment, the element may have this form:
   \(LOCALE-REGEXP . LANG-ENV)
@@ -2324,7 +2372,7 @@ specific encoding such as \"Latin-1\" and \"UTF-8\".")
 The first element whose locale regexp matches the start of a downcased locale
 specifies the language name whose charset corresponds to that locale.
 This language name is used if the locale is not listed in
-`locale-language-names'")
+`locale-language-names'.")
 
 (defconst locale-preferred-coding-systems
   (purecopy
@@ -2406,7 +2454,7 @@ is returned.  Thus, for instance, if charset \"ISO8859-2\",
 ;; too, for setting things such as calendar holidays, ps-print paper
 ;; size, spelling dictionary.
 
-(defun set-locale-environment (&optional locale-name display)
+(defun set-locale-environment (&optional locale-name frame)
   "Set up multi-lingual environment for using LOCALE-NAME.
 This sets the language environment, the coding system priority,
 the default input method and sometimes other things.
@@ -2427,10 +2475,9 @@ directory named `/usr/share/locale' or `/usr/lib/locale'.  LOCALE-NAME
 will be translated according to the table specified by
 `locale-translation-file-name'.
 
-If DISPLAY is non-nil, only set the keyboard coding system and
-the terminal coding system for the given display, and don't touch
-session-global parameters like the language environment.  DISPLAY
-may be a display id or a frame.
+If FRAME is non-nil, only set the keyboard coding system and the
+terminal coding system for the terminal of that frame, and don't
+touch session-global parameters like the language environment.
 
 See also `locale-charset-language-names', `locale-language-names',
 `locale-preferred-coding-systems' and `locale-coding-system'."
@@ -2461,7 +2508,7 @@ See also `locale-charset-language-names', `locale-language-names',
       (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
        (while (and vars
                    (= 0 (length locale))) ; nil or empty string
-         (setq locale (getenv (pop vars) display)))))
+         (setq locale (getenv (pop vars) frame)))))
 
     (unless locale
       ;; The two tests are kept separate so the byte-compiler sees
@@ -2500,6 +2547,8 @@ See also `locale-charset-language-names', `locale-language-names',
             (locale-name-match locale locale-language-names))
            (charset-language-name
             (locale-name-match locale locale-charset-language-names))
+           (default-eol-type (coding-system-eol-type
+                              default-buffer-file-coding-system))
            (coding-system
             (or (locale-name-match locale locale-preferred-coding-systems)
                 (when locale
@@ -2520,11 +2569,20 @@ See also `locale-charset-language-names', `locale-language-names',
          (if (not language-name)
              (setq language-name charset-language-name)))
 
+       ;; If a specific EOL conversion was specified in the default
+       ;; buffer-file-coding-system, preserve it in the coding system
+       ;; we will be using from now on.
+       (if (and (memq default-eol-type '(0 1 2 unix dos mac))
+                coding-system
+                (coding-system-p coding-system))
+           (setq coding-system (coding-system-change-eol-conversion
+                                coding-system default-eol-type)))
+
        (when language-name
 
          ;; Set up for this character set.  This is now the right way
          ;; to do it for both unibyte and multibyte modes.
-         (unless display
+         (unless frame
            (set-language-environment language-name))
 
          ;; If default-enable-multibyte-characters is nil,
@@ -2532,7 +2590,7 @@ See also `locale-charset-language-names', `locale-language-names',
          ;; so the display table and terminal coding system are irrelevant.
          (when default-enable-multibyte-characters
            (set-display-table-and-terminal-coding-system
-            language-name coding-system display))
+            language-name coding-system frame))
 
          ;; Set the `keyboard-coding-system' if appropriate (tty
          ;; only).  At least X and MS Windows can generate
@@ -2544,17 +2602,20 @@ See also `locale-charset-language-names', `locale-language-names',
          (let ((kcs (or coding-system
                         (car (get-language-info language-name
                                                 'coding-system)))))
-           (if kcs (set-keyboard-coding-system kcs display)))
+           (if kcs (set-keyboard-coding-system kcs frame)))
 
-         (unless display
+         (unless frame
            (setq locale-coding-system
                  (car (get-language-info language-name 'coding-priority)))))
 
-       (when (and (not display)
+       (when (and (not frame)
                   coding-system
                   (not (coding-system-equal coding-system
                                             locale-coding-system)))
          (prefer-coding-system coding-system)
+         ;; Fixme: perhaps prefer-coding-system should set this too.
+         ;; But it's not the time to do such a fundamental change.
+         (setq default-sendmail-coding-system coding-system)
          (setq locale-coding-system coding-system))))
 
     ;; On Windows, override locale-coding-system,
@@ -2563,9 +2624,9 @@ See also `locale-charset-language-names', `locale-language-names',
     (when (boundp 'w32-ansi-code-page)
       (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
        (when (coding-system-p code-page-coding)
-         (unless display (setq locale-coding-system code-page-coding))
-         (set-keyboard-coding-system code-page-coding display)
-         (set-terminal-coding-system code-page-coding display))))
+         (unless frame (setq locale-coding-system code-page-coding))
+         (set-keyboard-coding-system code-page-coding frame)
+         (set-terminal-coding-system code-page-coding frame))))
 
     (when (eq system-type 'darwin)
       ;; On Darwin, file names are always encoded in utf-8, no matter
@@ -2574,13 +2635,13 @@ See also `locale-charset-language-names', `locale-language-names',
       ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
       ;; the locale.
       (when (and (null window-system)
-                (equal (getenv "TERM_PROGRAM" display) "Apple_Terminal"))
+                (equal (getenv "TERM_PROGRAM" frame) "Apple_Terminal"))
        (set-terminal-coding-system 'utf-8)
        (set-keyboard-coding-system 'utf-8)))
 
     ;; Default to A4 paper if we're not in a C, POSIX or US locale.
     ;; (See comments in Flocale_info.)
-    (unless display
+    (unless frame
       (let ((locale locale)
            (paper (locale-info 'paper)))
        (if paper
@@ -2592,7 +2653,7 @@ See also `locale-charset-language-names', `locale-language-names',
              (setq ps-paper-type 'a4)))
          (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
            (while (and vars (= 0 (length locale)))
-             (setq locale (getenv (pop vars) display))))
+             (setq locale (getenv (pop vars) frame))))
          (when locale
            ;; As of glibc 2.2.5, these are the only US Letter locales,
            ;; and the rest are A4.