(font-lock-keywords): Add defvar.
[bpt/emacs.git] / lisp / files.el
index 81e014d..1ffd17e 100644 (file)
@@ -1,7 +1,7 @@
 ;;; files.el --- file input and output commands for Emacs
 
-;; Copyright (C) 1985,86,87,92,93,94,95,96,97,98,99,2000,01,02,03,2004
-;;;   Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
+;;   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 
@@ -19,8 +19,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -30,6 +30,9 @@
 
 ;;; Code:
 
+(defvar font-lock-keywords)
+
+
 (defgroup backup nil
   "Backups of edited data files."
   :group 'files)
@@ -159,8 +162,7 @@ both at the file level and at the levels of the containing directories."
   :type 'boolean
   :group 'find-file)
 
-(defcustom revert-without-query
-  nil
+(defcustom revert-without-query nil
   "*Specify which files should be reverted without query.
 The value is a list of regular expressions.
 If the file name matches one of these regular expressions,
@@ -178,7 +180,7 @@ If the buffer is visiting a new file, the value is nil.")
 (put 'buffer-file-number 'permanent-local t)
 
 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt)))
-  "Non-nil means that buffer-file-number uniquely identifies files.")
+  "Non-nil means that `buffer-file-number' uniquely identifies files.")
 
 (defvar buffer-file-read-only nil
   "Non-nil if visited file was read-only when visited.")
@@ -273,14 +275,46 @@ Includes the new backup.  Must be > 0"
   :group 'backup)
 
 (defcustom require-final-newline nil
-  "*Value of t says silently ensure a file ends in a newline when it is saved.
-Non-nil but not t says ask user whether to add a newline when there isn't one.
-nil means don't add newlines."
-  :type '(choice (const :tag "Off" nil)
-                (const :tag "Add" t)
-                (other :tag "Ask" ask))
+  "*Whether to add a newline automatically at the end of the file.
+
+A value of t means do this only when the file is about to be saved.
+A value of `visit' means do this right after the file is visited.
+A value of `visit-save' means do it at both of those times.
+Any other non-nil value means ask user whether to add a newline, when saving.
+nil means don't add newlines.
+
+Certain major modes set this locally to the value obtained
+from `mode-require-final-newline'."
+  :type '(choice (const :tag "When visiting" visit)
+                (const :tag "When saving" t)
+                (const :tag "When visiting or saving" visit-save)
+                (const :tag "Don't add newlines" nil)
+                (other :tag "Ask each time" ask))
   :group 'editing-basics)
 
+(defcustom mode-require-final-newline t
+  "*Whether to add a newline at end of file, in certain major modes.
+Those modes set `require-final-newline' to this value when you enable them.
+They do so because they are often used for files that are supposed
+to end in newlines, and the question is how to arrange that.
+
+A value of t means do this only when the file is about to be saved.
+A value of `visit' means do this right after the file is visited.
+A value of `visit-save' means do it at both of those times.
+Any other non-nil value means ask user whether to add a newline, when saving.
+
+nil means do not add newlines.  That is a risky choice in this variable
+since this value is used for modes for files that ought to have final newlines.
+So if you set this to nil, you must explicitly check and add
+a final newline, whenever you save a file that really needs one."
+  :type '(choice (const :tag "When visiting" visit)
+                (const :tag "When saving" t)
+                (const :tag "When visiting or saving" visit-save)
+                (const :tag "Don't add newlines" nil)
+                (other :tag "Ask each time" ask))
+  :group 'editing-basics
+  :version "22.1")
+
 (defcustom auto-save-default t
   "*Non-nil says by default do auto-saving of every file-visiting buffer."
   :type 'boolean
@@ -349,9 +383,8 @@ and should return either a buffer or nil."
 These functions are called as soon as the error is detected.
 Variable `buffer-file-name' is already set up.
 The functions are called in the order given until one of them returns non-nil.")
-(defvaralias 'find-file-not-found-hooks 'find-file-not-found-functions)
-(make-obsolete-variable
- 'find-file-not-found-hooks 'find-file-not-found-functions "21.4")
+(define-obsolete-variable-alias 'find-file-not-found-hooks
+    'find-file-not-found-functions "22.1")
 
 ;;;It is not useful to make this a local variable.
 ;;;(put 'find-file-hooks 'permanent-local t)
@@ -362,9 +395,8 @@ functions are called."
   :group 'find-file
   :type 'hook
   :options '(auto-insert)
-  :version "21.4")
-(defvaralias 'find-file-hooks 'find-file-hook)
-(make-obsolete-variable 'find-file-hooks 'find-file-hook "21.4")
+  :version "22.1")
+(define-obsolete-variable-alias 'find-file-hooks 'find-file-hook "22.1")
 
 (defvar write-file-functions nil
   "List of functions to be called before writing out a buffer to a file.
@@ -380,20 +412,20 @@ This hook is not run if any of the functions in
 to how to save a buffer to file, for instance, choosing a suitable
 coding system and setting mode bits.  (See Info
 node `(elisp)Saving Buffers'.)  To perform various checks or
-updates before the buffer is saved, use `before-save-hook' .")
+updates before the buffer is saved, use `before-save-hook'.")
 (put 'write-file-functions 'permanent-local t)
-(defvaralias 'write-file-hooks 'write-file-functions)
-(make-obsolete-variable 'write-file-hooks 'write-file-functions "21.4")
+(define-obsolete-variable-alias 'write-file-hooks 'write-file-functions "22.1")
 
 (defvar local-write-file-hooks nil)
 (make-variable-buffer-local 'local-write-file-hooks)
 (put 'local-write-file-hooks 'permanent-local t)
-(make-obsolete-variable 'local-write-file-hooks 'write-file-functions "21.4")
+(make-obsolete-variable 'local-write-file-hooks 'write-file-functions "22.1")
 
 (defvar write-contents-functions nil
   "List of functions to be called before writing out a buffer to a file.
 If one of them returns non-nil, the file is considered already written
-and the rest are not called.
+and the rest are not called and neither are the functions in
+`write-file-functions'.
 
 This variable is meant to be used for hooks that pertain to the
 buffer's contents, not to the particular visited file; thus,
@@ -406,8 +438,8 @@ For hooks that _do_ pertain to the particular visited file, use
 To perform various checks or updates before the buffer is saved,
 use `before-save-hook'.")
 (make-variable-buffer-local 'write-contents-functions)
-(defvaralias 'write-contents-hooks 'write-contents-functions)
-(make-obsolete-variable 'write-contents-hooks 'write-contents-functions "21.4")
+(define-obsolete-variable-alias 'write-contents-hooks
+    'write-contents-functions "22.1")
 
 (defcustom enable-local-variables t
   "*Control use of local variables in files you visit.
@@ -438,10 +470,7 @@ specified in a -*- line.")
   "*Control processing of the \"variable\" `eval' in a file's local variables.
 The value can be t, nil or something else.
 A value of t means obey `eval' variables;
-nil means ignore them; anything else means query.
-
-The command \\[normal-mode] always obeys local-variables lists
-and ignores this variable."
+nil means ignore them; anything else means query."
   :type '(choice (const :tag "Obey" t)
                 (const :tag "Ignore" nil)
                 (other :tag "Query" other))
@@ -480,16 +509,22 @@ Runs the usual ange-ftp hook, but only for completion operations."
       (apply op args))))
 
 (defun convert-standard-filename (filename)
-  "Convert a standard file's name to something suitable for the current OS.
+  "Convert a standard file's name to something suitable for the OS.
 This means to guarantee valid names and perhaps to canonicalize
 certain patterns.
 
+FILENAME should be an absolute file name since the conversion rules
+sometimes vary depending on the position in the file name.  E.g. c:/foo
+is a valid DOS file name, but c:/bar/c:/foo is not.
+
 This function's standard definition is trivial; it just returns
 the argument.  However, on Windows and DOS, replace invalid
 characters.  On DOS, make sure to obey the 8.3 limitations.  On
 Windows, turn Cygwin names into native names, and also turn
 slashes into backslashes if the shell requires it (see
-`w32-shell-dos-semantics')."
+`w32-shell-dos-semantics').
+
+See Info node `(elisp)Standard File Names' for more details."
   filename)
 
 (defun read-directory-name (prompt &optional dir default-dirname mustmatch initial)
@@ -497,8 +532,8 @@ slashes into backslashes if the shell requires it (see
 Value is not expanded---you must call `expand-file-name' yourself.
 Default name to DEFAULT-DIRNAME if user exits with the same
 non-empty string that was inserted by this function.
- (If DEFAULT-DIRNAME is omitted, the current buffer's directory is used,
-  except that if INITIAL is specified, that combined with DIR is used.)
+ (If DEFAULT-DIRNAME is omitted, DIR combined with INITIAL is used,
+  or just DIR if INITIAL is nil.)
 If the user exits with an empty minibuffer, this function returns
 an empty string.  (This can only happen if the user erased the
 pre-inserted contents or if `insert-default-directory' is nil.)
@@ -509,10 +544,10 @@ DIR should be an absolute directory name.  It defaults to
 the value of `default-directory'."
   (unless dir
     (setq dir default-directory))
-  (unless default-dirname
-    (setq default-dirname
-         (if initial (concat dir initial) default-directory)))
-  (read-file-name prompt dir default-dirname mustmatch initial
+  (read-file-name prompt dir (or default-dirname
+                                (if initial (expand-file-name initial dir)
+                                  dir))
+                 mustmatch initial
                  'file-directory-p))
 
 \f
@@ -526,8 +561,9 @@ the value of `default-directory'."
 Not actually set up until the first time you use it.")
 
 (defun parse-colon-path (cd-path)
-  "Explode a colon-separated search path into a list of directory names.
-\(For values of `colon' equal to `path-separator'.)"
+  "Explode a search path into a list of directory names.
+Directories are separated by occurrences of `path-separator'
+\(which is colon in GNU and GNU-like systems)."
   ;; We could use split-string here.
   (and cd-path
        (let (cd-list (cd-start 0) cd-colon)
@@ -560,8 +596,10 @@ Not actually set up until the first time you use it.")
 
 (defun cd (dir)
   "Make DIR become the current buffer's default directory.
-If your environment includes a `CDPATH' variable, try each one of that
-colon-separated list of directories when resolving a relative directory name."
+If your environment includes a `CDPATH' variable, try each one of
+that list of directories (separated by occurrences of
+`path-separator') when resolving a relative directory name.
+The path separator is colon in GNU and GNU-like systems."
   (interactive
    (list (read-directory-name "Change default directory: "
                         default-directory default-directory
@@ -594,8 +632,13 @@ colon-separated list of directories when resolving a relative directory name."
 
 (defun locate-file (filename path &optional suffixes predicate)
   "Search for FILENAME through PATH.
+If found, return the absolute file name of FILENAME, with its suffixes;
+otherwise return nil.
+PATH should be a list of directories to look in, like the lists in
+`exec-path' or `load-path'.
 If SUFFIXES is non-nil, it should be a list of suffixes to append to
 file name when searching.  If SUFFIXES is nil, it is equivalent to '(\"\").
+Use '(\"/\") to disable PATH search, but still try the suffixes in SUFFIXES.
 If non-nil, PREDICATE is used instead of `file-readable-p'.
 PREDICATE can also be an integer to pass to the `access' system call,
 in which case file-name handlers are ignored.  This usage is deprecated.
@@ -614,7 +657,7 @@ one or more of those symbols."
 
 (defun locate-file-completion (string path-and-suffixes action)
   "Do completion for file names passed to `locate-file'.
-PATH-AND-SUFFIXES is a pair of lists (DIRECTORIES . SUFFIXES)."
+PATH-AND-SUFFIXES is a pair of lists, (DIRECTORIES . SUFFIXES)."
   (if (file-name-absolute-p string)
       (read-file-name-internal string nil action)
     (let ((names nil)
@@ -636,6 +679,13 @@ PATH-AND-SUFFIXES is a pair of lists (DIRECTORIES . SUFFIXES)."
        ((null action) (try-completion string names))
        (t (test-completion string names))))))
 
+(defun executable-find (command)
+  "Search for COMMAND in `exec-path' and return the absolute file name.
+Return nil if COMMAND is not found anywhere in `exec-path'."
+  ;; Use 1 rather than file-executable-p to better match the behavior of
+  ;; call-process.
+  (locate-file command exec-path exec-suffixes 1))
+
 (defun load-library (library)
   "Load the library named LIBRARY.
 This is an interface to the function `load'."
@@ -646,7 +696,13 @@ This is an interface to the function `load'."
   (load library))
 
 (defun file-remote-p (file)
-  "Test whether FILE specifies a location on a remote system."
+  "Test whether FILE specifies a location on a remote system.
+Return an identification of the system if the location is indeed
+remote.  The identification of the system may comprise a method
+to access the system and its hostname, amongst other things.
+
+For example, the filename \"/user@host:/foo\" specifies a location
+on the system \"/user@host:\"."
   (let ((handler (find-file-name-handler file 'file-remote-p)))
     (if handler
        (funcall handler 'file-remote-p file)
@@ -669,7 +725,7 @@ The truename of a file name is found by chasing symbolic links
 both at the level of the file and at the level of the directories
 containing it, until no links are left at any level.
 
-\(fn FILENAME)"
+\(fn FILENAME)"  ;; Don't document the optional arguments.
   ;; COUNTER and PREV-DIRS are only used in recursive calls.
   ;; COUNTER can be a cons cell whose car is the count of how many
   ;; more links to chase before getting an error.
@@ -809,8 +865,8 @@ it means chase no more than that many links and then stop."
   "Change the encoding of FILE's name from CODING to NEW-CODING.
 The value is a new name of FILE.
 Signals a `file-already-exists' error if a file of the new name
-already exists unless optional third argument OK-IF-ALREADY-EXISTS
-is non-nil.  A number as third arg means request confirmation if
+already exists unless optional fourth argument OK-IF-ALREADY-EXISTS
+is non-nil.  A number as fourth arg means request confirmation if
 the new name already exists.  This is what happens in interactive
 use with M-x."
   (interactive
@@ -859,8 +915,15 @@ use with M-x."
 \f
 (defun switch-to-buffer-other-window (buffer &optional norecord)
   "Select buffer BUFFER in another window.
+If BUFFER does not identify an existing buffer, then this function
+creates a buffer with that name.
+
+When called from Lisp, BUFFER can be a buffer, a string \(a buffer name),
+or nil.  If BUFFER is nil, then this function chooses a buffer
+using `other-buffer'.
 Optional second arg NORECORD non-nil means
 do not put this buffer at the front of the list of recently selected ones.
+This function returns the buffer it switched to.
 
 This uses the function `display-buffer' as a subroutine; see its
 documentation for additional customization information."
@@ -886,20 +949,32 @@ documentation for additional customization information."
 (defvar find-file-default nil
   "Used within `find-file-read-args'.")
 
+(defmacro minibuffer-with-setup-hook (fun &rest body)
+  "Add FUN to `minibuffer-setup-hook' while executing BODY.
+BODY should use the minibuffer at most once.
+Recursive uses of the minibuffer will not be affected."
+  (declare (indent 1) (debug t))
+  (let ((hook (make-symbol "setup-hook")))
+    `(let (,hook)
+       (setq ,hook
+            (lambda ()
+              ;; Clear out this hook so it does not interfere
+              ;; with any recursive minibuffer usage.
+              (remove-hook 'minibuffer-setup-hook ,hook)
+              (,fun)))
+       (unwind-protect
+          (progn
+            (add-hook 'minibuffer-setup-hook ,hook)
+            ,@body)
+        (remove-hook 'minibuffer-setup-hook ,hook)))))
+
 (defun find-file-read-args (prompt mustmatch)
   (list (let ((find-file-default
               (and buffer-file-name
-                   (abbreviate-file-name buffer-file-name)))
-             (munge-default-fun
-              (lambda ()
-                (setq minibuffer-default find-file-default)
-                ;; Clear out this hook so it does not interfere
-                ;; with any recursive minibuffer usage.
-                (pop minibuffer-setup-hook)))
-             (minibuffer-setup-hook
-              minibuffer-setup-hook))
-         (add-hook 'minibuffer-setup-hook munge-default-fun)
-         (read-file-name prompt nil default-directory mustmatch))
+                   (abbreviate-file-name buffer-file-name))))
+         (minibuffer-with-setup-hook
+             (lambda () (setq minibuffer-default find-file-default))
+           (read-file-name prompt nil default-directory mustmatch)))
        t))
 
 (defun find-file (filename &optional wildcards)
@@ -916,8 +991,7 @@ suppress wildcard expansion by setting `find-file-wildcards'.
 
 To visit a file without any kind of conversion and without
 automatically choosing a major mode, use \\[find-file-literally]."
-  (interactive
-   (find-file-read-args "Find file: " nil))
+  (interactive (find-file-read-args "Find file: " nil))
   (let ((value (find-file-noselect filename nil nil wildcards)))
     (if (listp value)
        (mapcar 'switch-to-buffer (nreverse value))
@@ -939,8 +1013,8 @@ expand wildcards (if any) and visit multiple files."
     (if (listp value)
        (progn
          (setq value (nreverse value))
-         (switch-to-buffer-other-window (car value))
-         (mapcar 'switch-to-buffer (cdr value)))
+         (cons (switch-to-buffer-other-window (car value))
+               (mapcar 'switch-to-buffer (cdr value))))
       (switch-to-buffer-other-window value))))
 
 (defun find-file-other-frame (filename &optional wildcards)
@@ -959,43 +1033,69 @@ expand wildcards (if any) and visit multiple files."
     (if (listp value)
        (progn
          (setq value (nreverse value))
-         (switch-to-buffer-other-frame (car value))
-         (mapcar 'switch-to-buffer (cdr value)))
+         (cons (switch-to-buffer-other-frame (car value))
+               (mapcar 'switch-to-buffer (cdr value))))
       (switch-to-buffer-other-frame value))))
 
+(defun find-file-existing (filename &optional wildcards)
+  "Edit the existing file FILENAME.
+Like \\[find-file] but only allow files that exists."
+  (interactive (find-file-read-args "Find existing file: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
+  (find-file filename wildcards)
+  (current-buffer))
+
 (defun find-file-read-only (filename &optional wildcards)
   "Edit file FILENAME but don't allow changes.
 Like \\[find-file] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
+  (interactive (find-file-read-args "Find file read-only: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
 
 (defun find-file-read-only-other-window (filename &optional wildcards)
   "Edit file FILENAME in another window but don't allow changes.
 Like \\[find-file-other-window] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only other window: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file-other-window filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
+  (interactive (find-file-read-args "Find file read-only other window: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file-other-window filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
 
 (defun find-file-read-only-other-frame (filename &optional wildcards)
   "Edit file FILENAME in another frame but don't allow changes.
 Like \\[find-file-other-frame] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only other frame: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file-other-frame filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
-
-(defun find-alternate-file-other-window (filename)
+  (interactive (find-file-read-args "Find file read-only other frame: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file-other-frame filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
+
+(defun find-alternate-file-other-window (filename &optional wildcards)
   "Find file FILENAME as a replacement for the file in the next window.
-This command does not select that window."
+This command does not select that window.
+
+Interactively, or if WILDCARDS is non-nil in a call from Lisp,
+expand wildcards (if any) and replace the file with multiple files."
   (interactive
    (save-selected-window
      (other-window 1)
@@ -1006,17 +1106,21 @@ This command does not select that window."
            (setq file-name (file-name-nondirectory file)
                  file-dir (file-name-directory file)))
        (list (read-file-name
-             "Find alternate file: " file-dir nil nil file-name)))))
+             "Find alternate file: " file-dir nil nil file-name)
+            t))))
   (if (one-window-p)
-      (find-file-other-window filename)
+      (find-file-other-window filename wildcards)
     (save-selected-window
       (other-window 1)
-      (find-alternate-file filename))))
+      (find-alternate-file filename wildcards))))
 
-(defun find-alternate-file (filename)
+(defun find-alternate-file (filename &optional wildcards)
   "Find file FILENAME, select its buffer, kill previous buffer.
 If the current buffer now contains an empty file that you just visited
-\(presumably by mistake), use this command to visit the file you really want."
+\(presumably by mistake), use this command to visit the file you really want.
+
+Interactively, or if WILDCARDS is non-nil in a call from Lisp,
+expand wildcards (if any) and replace the file with multiple files."
   (interactive
    (let ((file buffer-file-name)
         (file-name nil)
@@ -1025,7 +1129,8 @@ If the current buffer now contains an empty file that you just visited
          (setq file-name (file-name-nondirectory file)
                file-dir (file-name-directory file)))
      (list (read-file-name
-           "Find alternate file: " file-dir nil nil file-name))))
+           "Find alternate file: " file-dir nil nil file-name)
+          t)))
   (unless (run-hook-with-args-until-failure 'kill-buffer-query-functions)
     (error "Aborted"))
   (when (and (buffer-modified-p) (buffer-file-name))
@@ -1053,7 +1158,7 @@ If the current buffer now contains an empty file that you just visited
          (setq buffer-file-truename nil)
          ;; Likewise for dired buffers.
          (setq dired-directory nil)
-         (find-file filename))
+         (find-file filename wildcards))
       (when (eq obuf (current-buffer))
        ;; This executes if find-file gets an error
        ;; and does not really find anything.
@@ -1154,7 +1259,8 @@ name to this list as a string."
   "Return the buffer visiting file FILENAME (a string).
 This is like `get-file-buffer', except that it checks for any buffer
 visiting the same file, possibly under a different name.
-If PREDICATE is non-nil, only a buffer satisfying it can be returned.
+If PREDICATE is non-nil, only buffers satisfying it are eligible,
+and others are ignored.
 If there is no such live buffer, return nil."
   (let ((predicate (or predicate #'identity))
         (truename (abbreviate-file-name (file-truename filename))))
@@ -1211,6 +1317,7 @@ suppresses this warning."
 When nil, never request confirmation."
   :group 'files
   :group 'find-file
+  :version "22.1"
   :type '(choice integer (const :tag "Never request confirmation" nil)))
 
 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
@@ -1218,12 +1325,12 @@ When nil, never request confirmation."
 If a buffer exists visiting FILENAME, return that one, but
 verify that the file has not changed since visited or saved.
 The buffer is not selected, just returned to the caller.
-Optional first arg NOWARN non-nil means suppress any warning messages.
-Optional second arg RAWFILE non-nil means the file is read literally.
-Optional third arg WILDCARDS non-nil means do wildcard processing
+Optional second arg NOWARN non-nil means suppress any warning messages.
+Optional third arg RAWFILE non-nil means the file is read literally.
+Optional fourth arg WILDCARDS non-nil means do wildcard processing
 and visit all the matching files.  When wildcards are actually
-used and expanded, the value is a list of buffers
-that are visiting the various files."
+used and expanded, return a list of buffers that are visiting
+the various files."
   (setq filename
        (abbreviate-file-name
         (expand-file-name filename)))
@@ -1360,7 +1467,6 @@ that are visiting the various files."
              buf)
          ;; Create a new buffer.
          (setq buf (create-file-buffer filename))
-         (set-buffer-major-mode buf)
          ;; find-file-noselect-1 may use a different buffer.
          (find-file-noselect-1 buf filename nowarn
                                rawfile truename number))))))
@@ -1434,6 +1540,7 @@ that are visiting the various files."
          (progn
            (set-buffer-multibyte nil)
            (setq buffer-file-coding-system 'no-conversion)
+           (set-buffer-major-mode buf)
            (make-local-variable 'find-file-literally)
            (setq find-file-literally t))
        (after-find-file error (not nowarn)))
@@ -1579,6 +1686,15 @@ unless NOMODES is non-nil."
     (when (and view-read-only view-mode)
       (view-mode-disable))
     (normal-mode t)
+    ;; If requested, add a newline at the end of the file.
+    (and (memq require-final-newline '(visit visit-save))
+        (> (point-max) (point-min))
+        (/= (char-after (1- (point-max))) ?\n)
+        (not (and (eq selective-display t)
+                  (= (char-after (1- (point-max))) ?\r)))
+        (save-excursion
+          (goto-char (point-max))
+          (insert "\n")))
     (when (and buffer-read-only
               view-read-only
               (not (eq (get major-mode 'mode-class) 'special)))
@@ -1614,12 +1730,18 @@ not set local variables (though we do notice a mode specified with -*-.)
 or from Lisp without specifying the optional argument FIND-FILE;
 in that case, this function acts as if `enable-local-variables' were t."
   (interactive)
-  (or find-file (funcall (or default-major-mode 'fundamental-mode)))
-  (report-errors "File mode specification error: %s"
-    (set-auto-mode))
-  (report-errors "File local-variables error: %s"
-    (let ((enable-local-variables (or (not find-file) enable-local-variables)))
+  (funcall (or default-major-mode 'fundamental-mode))
+  (let ((enable-local-variables (or (not find-file) enable-local-variables)))
+    (report-errors "File mode specification error: %s"
+      (set-auto-mode))
+    (report-errors "File local-variables error: %s"
       (hack-local-variables)))
+  ;; Turn font lock off and on, to make sure it takes account of
+  ;; whatever file local variables are relevant to it.
+  (when (and font-lock-mode (eq (car font-lock-keywords) t))
+    (setq font-lock-keywords (cadr font-lock-keywords))
+    (font-lock-mode 1))
+
   (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
       (ucs-set-table-for-input)))
 
@@ -1631,41 +1753,40 @@ in that case, this function acts as if `enable-local-variables' were t."
   (mapc
    (lambda (elt)
      (cons (purecopy (car elt)) (cdr elt)))
-   '(("\\.te?xt\\'" . text-mode)
-     ("\\.tex\\'" . tex-mode)
+   `(;; do this first, so that .html.pl is Polish html, not Perl
+     ("\\.s?html?\\(\\.[a-zA-Z_]+\\)?\\'" . html-mode)
+     ("\\.te?xt\\'" . text-mode)
+     ("\\.[tT]e[xX]\\'" . tex-mode)
      ("\\.ins\\'" . tex-mode)          ;Installation files for TeX packages.
      ("\\.ltx\\'" . latex-mode)
      ("\\.dtx\\'" . doctex-mode)
      ("\\.el\\'" . emacs-lisp-mode)
      ("\\.\\(scm\\|stk\\|ss\\|sch\\)\\'" . scheme-mode)
      ("\\.l\\'" . lisp-mode)
-     ("\\.lisp\\'" . lisp-mode)
-     ("\\.f\\'" . fortran-mode)
-     ("\\.F\\'" . fortran-mode)
+     ("\\.li?sp\\'" . lisp-mode)
+     ("\\.[fF]\\'" . fortran-mode)
      ("\\.for\\'" . fortran-mode)
      ("\\.p\\'" . pascal-mode)
      ("\\.pas\\'" . pascal-mode)
      ("\\.ad[abs]\\'" . ada-mode)
      ("\\.ad[bs].dg\\'" . ada-mode)
-     ("\\.\\([pP]\\([Llm]\\|erl\\)\\|al\\)\\'" . perl-mode)
-     ("\\.s?html?\\'" . html-mode)
-     ("\\.mk\\'" . makefile-mode)
-     ("\\(M\\|m\\|GNUm\\)akefile\\'" . makefile-mode)
-     ("\\.am\\'" . makefile-mode)      ;For Automake.
+     ("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)
+     ,@(if (memq system-type '(berkeley-unix next-mach darwin))
+          '(("\\.mk\\'" . makefile-bsdmake-mode)
+            ("GNUmakefile\\'" . makefile-gmake-mode)
+            ("[Mm]akefile\\'" . makefile-bsdmake-mode))
+        '(("\\.mk\\'" . makefile-gmake-mode)   ; Might be any make, give Gnu the host advantage
+          ("[Mm]akefile\\'" . makefile-gmake-mode)))
+     ("Makeppfile\\'" . makefile-makepp-mode)
+     ("\\.am\\'" . makefile-automake-mode)
      ;; Less common extensions come here
      ;; so more common ones above are found faster.
      ("\\.texinfo\\'" . texinfo-mode)
      ("\\.te?xi\\'" . texinfo-mode)
-     ("\\.s\\'" . asm-mode)
-     ("\\.S\\'" . asm-mode)
+     ("\\.[sS]\\'" . asm-mode)
      ("\\.asm\\'" . asm-mode)
-     ("ChangeLog\\'" . change-log-mode)
-     ("change\\.log\\'" . change-log-mode)
-     ("changelo\\'" . change-log-mode)
-     ("ChangeLog\\.[0-9]+\\'" . change-log-mode)
-     ;; for MSDOS and MS-Windows (which are case-insensitive)
-     ("changelog\\'" . change-log-mode)
-     ("changelog\\.[0-9]+\\'" . change-log-mode)
+     ("[cC]hange\\.?[lL]og?\\'" . change-log-mode)
+     ("[cC]hange[lL]og\\.[0-9]+\\'" . change-log-mode)
      ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
      ("\\.scm\\.[0-9]*\\'" . scheme-mode)
      ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode)
@@ -1674,40 +1795,34 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\(/\\|\\`\\)\\.\\(bash_logout\\|shrc\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode)
      ("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode)
      ("\\.m?spec\\'" . sh-mode)
-     ("\\.mm\\'" . nroff-mode)
-     ("\\.me\\'" . nroff-mode)
-     ("\\.ms\\'" . nroff-mode)
+     ("\\.m[mes]\\'" . nroff-mode)
      ("\\.man\\'" . nroff-mode)
-     ("\\.TeX\\'" . tex-mode)
      ("\\.sty\\'" . latex-mode)
-     ("\\.cls\\'" . latex-mode)                ;LaTeX 2e class
-     ("\\.clo\\'" . latex-mode)                ;LaTeX 2e class option
+     ("\\.cl[so]\\'" . latex-mode)             ;LaTeX 2e class option
      ("\\.bbl\\'" . latex-mode)
      ("\\.bib\\'" . bibtex-mode)
      ("\\.sql\\'" . sql-mode)
-     ("\\.m4\\'" . m4-mode)
-     ("\\.mc\\'" . m4-mode)
+     ("\\.m[4c]\\'" . m4-mode)
      ("\\.mf\\'" . metafont-mode)
      ("\\.mp\\'" . metapost-mode)
      ("\\.vhdl?\\'" . vhdl-mode)
      ("\\.article\\'" . text-mode)
      ("\\.letter\\'" . text-mode)
-     ("\\.tcl\\'" . tcl-mode)
+     ("\\.i?tcl\\'" . tcl-mode)
      ("\\.exp\\'" . tcl-mode)
-     ("\\.itcl\\'" . tcl-mode)
      ("\\.itk\\'" . tcl-mode)
      ("\\.icn\\'" . icon-mode)
      ("\\.sim\\'" . simula-mode)
      ("\\.mss\\'" . scribe-mode)
-     ("\\.f90\\'" . f90-mode)
-     ("\\.f95\\'" . f90-mode)
+     ("\\.f9[05]\\'" . f90-mode)
      ("\\.indent\\.pro\\'" . fundamental-mode) ; to avoid idlwave-mode
      ("\\.pro\\'" . idlwave-mode)
-     ("\\.lsp\\'" . lisp-mode)
      ("\\.prolog\\'" . prolog-mode)
      ("\\.tar\\'" . tar-mode)
-     ("\\.\\(arc\\|zip\\|lzh\\|zoo\\|ear\\|jar\\|war\\)\\'" . archive-mode)
-     ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|EAR\\|JAR\\|WAR\\)\\'" . archive-mode)
+     ;; The list of archive file extensions should be in sync with
+     ;; `auto-coding-alist' with `no-conversion' coding system.
+     ("\\.\\(arc\\|zip\\|lzh\\|zoo\\|[jew]ar\\|xpi\\)\\'" . archive-mode)
+     ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|[JEW]AR\\|XPI\\)\\'" . archive-mode)
      ("\\.sx[dmicw]\\'" . archive-mode)        ; OpenOffice.org
      ;; Mailer puts message to be edited in
      ;; /tmp/Re.... or Message
@@ -1719,10 +1834,11 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\`/tmp/fol/" . text-mode)
      ("\\.oak\\'" . scheme-mode)
      ("\\.sgml?\\'" . sgml-mode)
-     ("\\.xml\\'" . sgml-mode)
-     ("\\.xsl\\'" . sgml-mode)
+     ("\\.x[ms]l\\'" . xml-mode)
      ("\\.dtd\\'" . sgml-mode)
      ("\\.ds\\(ss\\)?l\\'" . dsssl-mode)
+     ("\\.js\\'" . java-mode)          ; javascript-mode would be better
+     ("\\.x[bp]m\\'" . c-mode)
      ;; .emacs or .gnus or .viper following a directory delimiter in
      ;; Unix, MSDOG or VMS syntax.
      ("[]>:/\\]\\..*\\(emacs\\|gnus\\|viper\\)\\'" . emacs-lisp-mode)
@@ -1741,20 +1857,43 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("BROWSE\\'" . ebrowse-tree-mode)
      ("\\.ebrowse\\'" . ebrowse-tree-mode)
      ("#\\*mail\\*" . mail-mode)
+     ("\\.g\\'" . antlr-mode)
+     ("\\.ses\\'" . ses-mode)
+     ("\\.\\(soa\\|zone\\)\\'" . dns-mode)
+     ("\\.docbook\\'" . sgml-mode)
+     ("\\.com\\'" . dcl-mode)
+     ("/config\\.\\(?:bat\\|log\\)\\'" . fundamental-mode)
+     ;; Windows candidates may be opened case sensitively on Unix
+     ("\\.\\(?:[iI][nN][iI]\\|[lL][sS][tT]\\|[rR][eE][gG]\\|[sS][yY][sS]\\)\\'" . conf-mode)
+     ("\\.\\(?:desktop\\|la\\)\\'" . conf-unix-mode)
+     ("\\.ppd\\'" . conf-ppd-mode)
+     ("java.+\\.conf\\'" . conf-javaprop-mode)
+     ("\\.properties\\(?:\\.[a-zA-Z0-9._-]+\\)?\\'" . conf-javaprop-mode)
+     ;; *.cf, *.cfg, *.conf, *.config[.local|.de_DE.UTF8|...], */config
+     ("[/.]c\\(?:on\\)?f\\(?:i?g\\)?\\(?:\\.[a-zA-Z0-9._-]+\\)?\\'" . conf-mode)
+     ("\\`/etc/\\(?:DIR_COLORS\\|ethers\\|.?fstab\\|.*hosts\\|lesskey\\|login\\.?de\\(?:fs\\|vperm\\)\\|magic\\|mtab\\|pam\\.d/.*\\|permissions\\(?:\\.d/.+\\)?\\|protocols\\|rpc\\|services\\)\\'" . conf-space-mode)
+     ("\\`/etc/\\(?:acpid?/.+\\|aliases\\(?:\\.d/.+\\)?\\|default/.+\\|group-?\\|hosts\\..+\\|inittab\\|ksysguarddrc\\|opera6rc\\|passwd-?\\|shadow-?\\|sysconfig/.+\\)\\'" . conf-mode)
+     ;; either user's dot-files or under /etc or some such
+     ("/\\.?\\(?:gnokiirc\\|kde.*rc\\|mime\\.types\\|wgetrc\\)\\'" . conf-mode)
+     ;; alas not all ~/.*rc files are like this
+     ("/\\.\\(?:enigma\\|gltron\\|gtk\\|hxplayer\\|net\\|neverball\\|qt/.+\\|realplayer\\|scummvm\\|sversion\\|sylpheed/.+\\|xmp\\)rc\\'" . conf-mode)
+     ("/\\.\\(?:gdbtkinit\\|grip\\|orbital/.+txt\\|rhosts\\|tuxracer/options\\)\\'" . conf-mode)
+     ("/\\.?X\\(?:default\\|resource\\|re\\)s\\>" . conf-xdefaults-mode)
+     ("/X11.+app-defaults/" . conf-xdefaults-mode)
+     ("/X11.+locale/.+/Compose\\'" . conf-colon-mode)
+     ;; this contains everything twice, with space and with colon :-(
+     ("/X11.+locale/compose\\.dir\\'" . conf-javaprop-mode)
      ;; Get rid of any trailing .n.m and try again.
      ;; This is for files saved by cvs-merge that look like .#<file>.<rev>
      ;; or .#<file>.<rev>-<rev> or VC's <file>.~<rev>~.
      ;; Using mode nil rather than `ignore' would let the search continue
      ;; through this list (with the shortened name) rather than start over.
-     ("\\.~?[0-9]+\\.[0-9][-.0-9]*~?\\'" ignore t)
+     ("\\.~?[0-9]+\\.[0-9][-.0-9]*~?\\'" nil t)
      ;; The following should come after the ChangeLog pattern
      ;; for the sake of ChangeLog.1, etc.
      ;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too.
      ("\\.[1-9]\\'" . nroff-mode)
-     ("\\.g\\'" . antlr-mode)
-     ("\\.ses\\'" . ses-mode)
-     ("\\.orig\\'" nil t)              ; from patch
-     ("\\.in\\'" nil t)))
+     ("\\.\\(?:orig\\|in\\|[bB][aA][kK]\\)\\'" nil t)))
   "Alist of filename patterns vs corresponding major mode functions.
 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
 \(NON-NIL stands for anything that is not nil; the value does not matter.)
@@ -1763,8 +1902,14 @@ mode function to use.  FUNCTION will be called, unless it is nil.
 
 If the element has the form (REGEXP FUNCTION NON-NIL), then after
 calling FUNCTION (if it's not nil), we delete the suffix that matched
-REGEXP and search the list again for another match.")
+REGEXP and search the list again for another match.
 
+If the file name matches `inhibit-first-line-modes-regexps',
+then `auto-mode-alist' is not processed.
+
+See also `interpreter-mode-alist', which detects executable script modes
+based on the interpreters they specify to run,
+and `magic-mode-alist', which determines modes based on file contents.")
 
 (defvar interpreter-mode-alist
   ;; Note: The entries for the modes defined in cc-mode.el (awk-mode
@@ -1805,15 +1950,16 @@ REGEXP and search the list again for another match.")
      ("more" . text-mode)
      ("less" . text-mode)
      ("pg" . text-mode)
-     ("make" . makefile-mode)          ; Debian uses this
+     ("make" . makefile-gmake-mode)            ; Debian uses this
      ("guile" . scheme-mode)
      ("clisp" . lisp-mode)))
   "Alist mapping interpreter names to major modes.
-This alist applies to files whose first line starts with `#!'.
+This is used for files whose first lines match `auto-mode-interpreter-regexp'.
 Each element looks like (INTERPRETER . MODE).
-The car of each element is compared with
-the name of the interpreter specified in the first line.
-If it matches, mode MODE is selected.")
+If INTERPRETER matches the name of the interpreter specified in the first line
+of a script, mode MODE is enabled.
+
+See also `auto-mode-alist'.")
 
 (defvar inhibit-first-line-modes-regexps '("\\.tar\\'" "\\.tgz\\'")
   "List of regexps; if one matches a file name, don't look for `-*-'.")
@@ -1826,20 +1972,47 @@ from the end of the file name anything that matches one of these regexps.")
 (defvar auto-mode-interpreter-regexp
   "#![ \t]?\\([^ \t\n]*\
 /bin/env[ \t]\\)?\\([^ \t\n]+\\)"
-  "Regular expression matching interpreters, for file mode determination.
+  "Regexp matching interpreters, for file mode determination.
 This regular expression is matched against the first line of a file
-to determine the file's mode in `set-auto-mode' when Emacs can't deduce
-a mode from the file's name.  If it matches, the file is assumed to
-be interpreted by the interpreter matched by the second group of the
-regular expression.  The mode is then determined as the mode associated
-with that interpreter in `interpreter-mode-alist'.")
-
-(defun set-auto-mode (&optional just-from-file-name)
+to determine the file's mode in `set-auto-mode'.  If it matches, the file
+is assumed to be interpreted by the interpreter matched by the second group
+of the regular expression.  The mode is then determined as the mode
+associated with that interpreter in `interpreter-mode-alist'.")
+
+(defvar magic-mode-alist
+  `(;; The < comes before the groups (but the first) to reduce backtracking.
+    ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff.
+    (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
+            (comment-re (concat "\\(?:!--" incomment-re "*-->\\s *<\\)")))
+       (concat "\\(?:<\\?xml\\s +[^>]*>\\)?\\s *<"
+               comment-re "*"
+               "\\(?:!DOCTYPE\\s +[^>]*>\\s *<\\s *" comment-re "*\\)?"
+               "[Hh][Tt][Mm][Ll]"))
+     . html-mode)
+    ;; These two must come after html, because they are more general:
+    ("<\\?xml " . xml-mode)
+    (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
+            (comment-re (concat "\\(?:!--" incomment-re "*-->\\s *<\\)")))
+       (concat "\\s *<" comment-re "*!DOCTYPE "))
+     . sgml-mode)
+    ("%![^V]" . ps-mode)
+    ("# xmcd " . conf-unix-mode))
+  "Alist of buffer beginnings vs. corresponding major mode functions.
+Each element looks like (REGEXP . FUNCTION).  After visiting a file,
+if REGEXP matches the text at the beginning of the buffer,
+`normal-mode' will call FUNCTION rather than allowing `auto-mode-alist'
+to decide the buffer's major mode.
+
+If FUNCTION is nil, then it is not called.  (That is a way of saying
+\"allow `auto-mode-alist' to decide for these files.\")")
+
+(defun set-auto-mode (&optional keep-mode-if-same)
   "Select major mode appropriate for current buffer.
-This checks for a -*- mode tag in the buffer's text,
-compares the filename against the entries in `auto-mode-alist',
-or checks the interpreter that runs this file against
-`interpreter-mode-alist'.
+
+This checks for a -*- mode tag in the buffer's text, checks the
+interpreter that runs this file against `interpreter-mode-alist',
+compares the buffer beginning against `magic-mode-alist', or
+compares the filename against the entries in `auto-mode-alist'.
 
 It does not check for the `mode:' local variable in the
 Local Variables section of the file; for that, use `hack-local-variables'.
@@ -1847,11 +2020,11 @@ Local Variables section of the file; for that, use `hack-local-variables'.
 If `enable-local-variables' is nil, this function does not check for a
 -*- mode tag.
 
-If the optional argument JUST-FROM-FILE-NAME is non-nil,
-then we do not set anything but the major mode,
-and we don't even do that unless it would come from the file name."
+If the optional argument KEEP-MODE-IF-SAME is non-nil, then we
+only set the major mode, if that would change it."
   ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
-  (let (end done modes)
+  (let (end done mode modes)
+    ;; Find a -*- mode tag
     (save-excursion
       (goto-char (point-min))
       (skip-chars-forward " \t\n")
@@ -1876,63 +2049,75 @@ and we don't even do that unless it would come from the file name."
             (push (intern (concat (downcase (buffer-substring (point) end))
                                   "-mode"))
                   modes))))
-    ;; If we found modes to use, invoke them now,
-    ;; outside the save-excursion.
-    (unless just-from-file-name
-      (dolist (mode (nreverse modes))
-       (if (not (functionp mode))
-           (message "Ignoring unknown mode `%s'" mode)
-         (setq done t)
-         (funcall mode))))
-    ;; If we didn't find a mode from a -*- line, try using the file name.
-    (if (and (not done) buffer-file-name)
-       (let ((name buffer-file-name)
-             (keep-going t))
-         ;; Remove backup-suffixes from file name.
-         (setq name (file-name-sans-versions name))
-         (while keep-going
-           (setq keep-going nil)
-           (let ((alist auto-mode-alist)
-                 (mode nil))
-             ;; Find first matching alist entry.
-             (let ((case-fold-search
-                    (memq system-type '(vax-vms windows-nt cygwin))))
-               (while (and (not mode) alist)
-                 (if (string-match (car (car alist)) name)
-                     (if (and (consp (cdr (car alist)))
-                              (nth 2 (car alist)))
-                         (setq mode (car (cdr (car alist)))
-                               name (substring name 0 (match-beginning 0))
-                               keep-going t)
-                       (setq mode (cdr (car alist))
-                             keep-going nil)))
-                 (setq alist (cdr alist))))
-             (if mode
-                 ;; When JUST-FROM-FILE-NAME is set,
-                 ;; we are working on behalf of set-visited-file-name.
-                 ;; In that case, if the major mode specified is the
-                 ;; same one we already have, don't actually reset it.
-                 ;; We don't want to lose minor modes such as Font Lock.
-                 (unless (and just-from-file-name (eq mode major-mode))
-                   (funcall mode))
-               ;; If we can't deduce a mode from the file name,
-               ;; look for an interpreter specified in the first line.
-               ;; As a special case, allow for things like "#!/bin/env perl",
-               ;; which finds the interpreter anywhere in $PATH.
-               (let ((interpreter
-                      (save-excursion
-                        (goto-char (point-min))
-                        (if (looking-at auto-mode-interpreter-regexp)
-                            (match-string 2)
-                          "")))
-                     elt)
-                 ;; Map interpreter name to a mode.
-                 (setq elt (assoc (file-name-nondirectory interpreter)
-                                  interpreter-mode-alist))
-                 (unless just-from-file-name
-                   (if elt
-                       (funcall (cdr elt))))))))))))
-
+    ;; If we found modes to use, invoke them now, outside the save-excursion.
+    (if modes
+       (catch 'nop
+         (dolist (mode (nreverse modes))
+           (if (not (functionp mode))
+               (message "Ignoring unknown mode `%s'" mode)
+             (setq done t)
+             (or (set-auto-mode-0 mode keep-mode-if-same)
+                 ;; continuing would call minor modes again, toggling them off
+                 (throw 'nop nil)))))
+      ;; If we didn't, look for an interpreter specified in the first line.
+      ;; As a special case, allow for things like "#!/bin/env perl", which
+      ;; finds the interpreter anywhere in $PATH.
+      (setq mode (save-excursion
+                  (goto-char (point-min))
+                  (if (looking-at auto-mode-interpreter-regexp)
+                      (match-string 2)
+                    ""))
+           ;; Map interpreter name to a mode, signalling we're done at the
+           ;; same time.
+           done (assoc (file-name-nondirectory mode)
+                       interpreter-mode-alist))
+      ;; If we found an interpreter mode to use, invoke it now.
+      (if done
+         (set-auto-mode-0 (cdr done) keep-mode-if-same)))
+    ;; If we didn't, match the buffer beginning against magic-mode-alist.
+    (unless done
+      (if (setq done (save-excursion
+                      (goto-char (point-min))
+                      (assoc-default nil magic-mode-alist
+                                     (lambda (re dummy)
+                                       (looking-at re)))))
+         (set-auto-mode-0 done keep-mode-if-same)
+       ;; Compare the filename against the entries in auto-mode-alist.
+       (if buffer-file-name
+           (let ((name buffer-file-name))
+             ;; Remove backup-suffixes from file name.
+             (setq name (file-name-sans-versions name))
+             (while name
+               ;; Find first matching alist entry.
+               (let ((case-fold-search
+                      (memq system-type '(vax-vms windows-nt cygwin))))
+                 (if (and (setq mode (assoc-default name auto-mode-alist
+                                                    'string-match))
+                          (consp mode)
+                          (cadr mode))
+                     (setq mode (car mode)
+                           name (substring name 0 (match-beginning 0)))
+                   (setq name)))
+               (when mode
+                 (set-auto-mode-0 mode keep-mode-if-same)))))))))
+
+;; When `keep-mode-if-same' is set, we are working on behalf of
+;; set-visited-file-name.  In that case, if the major mode specified is the
+;; same one we already have, don't actually reset it.  We don't want to lose
+;; minor modes such as Font Lock.
+(defun set-auto-mode-0 (mode &optional keep-mode-if-same)
+  "Apply MODE and return it.
+If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of
+any aliases and compared to current major mode.  If they are the
+same, do nothing and return nil."
+  (when keep-mode-if-same
+    (while (symbolp (symbol-function mode))
+      (setq mode (symbol-function mode)))
+    (if (eq mode major-mode)
+       (setq mode nil)))
+  (when mode
+    (funcall mode)
+    mode))
 
 (defun set-auto-mode-1 ()
   "Find the -*- spec in the buffer.
@@ -1958,22 +2143,19 @@ Otherwise, return nil; point may be changed."
         (setq temp (cdr temp)))
        (not temp))
 
-     (search-forward "-*-" (save-excursion
-                            ;; If the file begins with "#!"
-                            ;; (exec interpreter magic), look
-                            ;; for mode frobs in the first two
-                            ;; lines.  You cannot necessarily
-                            ;; put them in the first line of
-                            ;; such a file without screwing up
-                            ;; the interpreter invocation.
-                            (end-of-line (and (looking-at "^#!") 2))
-                            (point)) t)
+     (search-forward "-*-" (line-end-position
+                            ;; If the file begins with "#!"
+                            ;; (exec interpreter magic), look
+                            ;; for mode frobs in the first two
+                            ;; lines.  You cannot necessarily
+                            ;; put them in the first line of
+                            ;; such a file without screwing up
+                            ;; the interpreter invocation.
+                            (and (looking-at "^#!") 2)) t)
      (progn
        (skip-chars-forward " \t")
        (setq beg (point))
-       (search-forward "-*-"
-                      (save-excursion (end-of-line) (point))
-                      t))
+       (search-forward "-*-" (line-end-position) t))
      (progn
        (forward-char -3)
        (skip-chars-backward " \t")
@@ -1981,6 +2163,28 @@ Otherwise, return nil; point may be changed."
        (goto-char beg)
        end))))
 
+(defun hack-local-variables-confirm (string flag-to-check)
+  (or (eq flag-to-check t)
+      (and flag-to-check
+          (save-window-excursion
+            (condition-case nil
+                (switch-to-buffer (current-buffer))
+              (error
+               ;; If we fail to switch in the selected window,
+               ;; it is probably a minibuffer or dedicated window.
+               ;; So try another window.
+               (let ((pop-up-frames nil))
+                 ;; Refrain from popping up frames since it can't
+                 ;; be undone by save-window-excursion.
+                 (pop-to-buffer (current-buffer)))))
+            (save-excursion
+              (beginning-of-line)
+              (set-window-start (selected-window) (point)))
+            (y-or-n-p (format string
+                              (if buffer-file-name
+                                  (file-name-nondirectory buffer-file-name)
+                                (concat "buffer " (buffer-name)))))))))
+
 (defun hack-local-variables-prop-line (&optional mode-only)
   "Set local variables specified in the -*- line.
 Ignore any specification for `mode:' and `coding:';
@@ -2035,21 +2239,9 @@ is specified, returning t if it is specified."
       (if mode-only mode-specified
        (if (and result
                 (or mode-only
-                    (eq enable-local-variables t)
-                    (and enable-local-variables
-                         (save-window-excursion
-                           (condition-case nil
-                               (switch-to-buffer (current-buffer))
-                             (error
-                              ;; If we fail to switch in the selected window,
-                              ;; it is probably a minibuffer.
-                              ;; So try another window.
-                              (condition-case nil
-                                  (switch-to-buffer-other-window (current-buffer))
-                                (error
-                                 (switch-to-buffer-other-frame (current-buffer))))))
-                           (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
-                                             (file-name-nondirectory buffer-file-name)))))))
+                    (hack-local-variables-confirm
+                     "Set local variables as specified in -*- line of %s? "
+                     enable-local-variables)))
            (let ((enable-local-eval enable-local-eval))
              (while result
                (hack-one-local-variable (car (car result)) (cdr (car result)))
@@ -2076,81 +2268,82 @@ is specified, returning t if it is specified."
     (save-excursion
       (goto-char (point-max))
       (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
-      (if (let ((case-fold-search t))
-           (and (search-forward "Local Variables:" nil t)
-                (or (eq enable-local-variables t)
-                    mode-only
-                    (and enable-local-variables
-                         (save-window-excursion
-                           (switch-to-buffer (current-buffer))
-                           (save-excursion
-                             (beginning-of-line)
-                             (set-window-start (selected-window) (point)))
-                           (y-or-n-p (format "Set local variables as specified at end of %s? "
-                                             (if buffer-file-name
-                                                 (file-name-nondirectory
-                                                  buffer-file-name)
-                                               (concat "buffer "
-                                                       (buffer-name))))))))))
-         (let ((continue t)
-               prefix prefixlen suffix beg
-               mode-specified
-               (enable-local-eval enable-local-eval))
-           ;; The prefix is what comes before "local variables:" in its line.
-           ;; The suffix is what comes after "local variables:" in its line.
-           (skip-chars-forward " \t")
-           (or (eolp)
-               (setq suffix (buffer-substring (point)
-                                              (progn (end-of-line) (point)))))
-           (goto-char (match-beginning 0))
-           (or (bolp)
-               (setq prefix
-                     (buffer-substring (point)
-                                       (progn (beginning-of-line) (point)))))
-
-           (if prefix (setq prefixlen (length prefix)
-                            prefix (regexp-quote prefix)))
-           (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
-           (while continue
-             ;; Look at next local variable spec.
-             (if selective-display (re-search-forward "[\n\C-m]")
+      (when (let ((case-fold-search t))
+             (and (search-forward "Local Variables:" nil t)
+                  (or mode-only
+                      (hack-local-variables-confirm
+                       "Set local variables as specified at end of %s? "
+                       enable-local-variables))))
+       (skip-chars-forward " \t")
+       (let ((enable-local-eval enable-local-eval)
+             ;; suffix is what comes after "local variables:" in its line.
+             (suffix
+              (concat
+               (regexp-quote (buffer-substring (point) (line-end-position)))
+               "$"))
+             ;; prefix is what comes before "local variables:" in its line.
+             (prefix
+              (concat "^" (regexp-quote
+                           (buffer-substring (line-beginning-position)
+                                             (match-beginning 0)))))
+             beg)
+
+         (forward-line 1)
+         (let ((startpos (point))
+               endpos
+               (thisbuf (current-buffer)))
+           (save-excursion
+             (unless (let ((case-fold-search t))
+                       (re-search-forward
+                        (concat prefix "[ \t]*End:[ \t]*" suffix)
+                        nil t))
+               (error "Local variables list is not properly terminated"))
+             (beginning-of-line)
+             (setq endpos (point)))
+
+           (with-temp-buffer
+             (insert-buffer-substring thisbuf startpos endpos)
+             (goto-char (point-min))
+             (subst-char-in-region (point) (point-max) ?\^m ?\n)
+             (while (not (eobp))
+               ;; Discard the prefix.
+               (if (looking-at prefix)
+                   (delete-region (point) (match-end 0))
+                 (error "Local variables entry is missing the prefix"))
+               (end-of-line)
+               ;; Discard the suffix.
+               (if (looking-back suffix)
+                   (delete-region (match-beginning 0) (point))
+                 (error "Local variables entry is missing the suffix"))
                (forward-line 1))
-             ;; Skip the prefix, if any.
-             (if prefix
-                 (if (looking-at prefix)
-                     (forward-char prefixlen)
-                   (error "Local variables entry is missing the prefix")))
-             ;; Find the variable name; strip whitespace.
-             (skip-chars-forward " \t")
-             (setq beg (point))
-             (skip-chars-forward "^:\n")
-             (if (eolp) (error "Missing colon in local variables entry"))
-             (skip-chars-backward " \t")
-             (let* ((str (buffer-substring beg (point)))
-                    (var (read str))
-                   val)
-               ;; Setting variable named "end" means end of list.
-               (if (string-equal (downcase str) "end")
-                   (setq continue nil)
-                 ;; Otherwise read the variable value.
+             (goto-char (point-min))
+
+             (while (not (eobp))
+               ;; Find the variable name; strip whitespace.
+               (skip-chars-forward " \t")
+               (setq beg (point))
+               (skip-chars-forward "^:\n")
+               (if (eolp) (error "Missing colon in local variables entry"))
+               (skip-chars-backward " \t")
+               (let* ((str (buffer-substring beg (point)))
+                      (var (read str))
+                      val)
+                 ;; Read the variable value.
                  (skip-chars-forward "^:")
                  (forward-char 1)
                  (setq val (read (current-buffer)))
-                 (skip-chars-backward "\n")
-                 (skip-chars-forward " \t")
-                 (or (if suffix (looking-at suffix) (eolp))
-                     (error "Local variables entry is terminated incorrectly"))
                  (if mode-only
                      (if (eq var 'mode)
                          (setq mode-specified t))
                    ;; Set the variable.  "Variables" mode and eval are funny.
-                   (hack-one-local-variable var val))))))))
+                   (with-current-buffer thisbuf
+                     (hack-one-local-variable var val))))
+               (forward-line 1)))))))
     (unless mode-only
       (run-hooks 'hack-local-variables-hook))
     mode-specified))
 
-(defvar ignored-local-variables
-  '(enable-local-eval)
+(defvar ignored-local-variables ()
   "Variables to be ignored in a file's local variable spec.")
 
 ;; Get confirmation before setting these variables as locals in a file.
@@ -2216,9 +2409,8 @@ is specified, returning t if it is specified."
 If VAL is nil or omitted, the question is whether any value might be
 dangerous."
   (let ((safep (get sym 'safe-local-variable)))
-    (or (memq sym ignored-local-variables)
-       (get sym 'risky-local-variable)
-       (and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
+    (or (get sym 'risky-local-variable)
+       (and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-commands?$\\|-predicates?$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
                           (symbol-name sym))
             (not safep))
        ;; If the safe-local-variable property isn't t or nil,
@@ -2233,7 +2425,7 @@ Add expressions to this list if you want Emacs to evaluate them, when
 they appear in an `eval' local variable specification, without first
 asking you for confirmation."
   :group 'find-file
-  :version "21.4"
+  :version "22.1"
   :type '(repeat sexp))
 
 (put 'c-set-style 'safe-local-eval-function t)
@@ -2291,6 +2483,8 @@ is considered risky."
        ((eq var 'coding)
         ;; We have already handled coding: tag in set-auto-coding.
         nil)
+       ((memq var ignored-local-variables)
+        nil)
        ;; "Setting" eval means either eval it or do nothing.
        ;; Likewise for setting hook variables.
        ((risky-local-variable-p var val)
@@ -2300,18 +2494,9 @@ is considered risky."
                      (hack-one-local-variable-eval-safep val))
                 ;; Permit eval if not root and user says ok.
                 (and (not (zerop (user-uid)))
-                     (or (eq enable-local-eval t)
-                         (and enable-local-eval
-                              (save-window-excursion
-                                (switch-to-buffer (current-buffer))
-                                (save-excursion
-                                  (beginning-of-line)
-                                  (set-window-start (selected-window) (point)))
-                                (setq enable-local-eval
-                                      (y-or-n-p (format "Process `eval' or hook local variables in %s? "
-                                                        (if buffer-file-name
-                                                            (concat "file " (file-name-nondirectory buffer-file-name))
-                                                          (concat "buffer " (buffer-name)))))))))))
+                     (hack-local-variables-confirm
+                      "Process `eval' or hook local variables in %s? "
+                      enable-local-eval)))
             (if (eq var 'eval)
                 (save-excursion (eval val))
               (make-local-variable var)
@@ -2363,6 +2548,10 @@ the old visited file has been renamed to the new name FILENAME."
          (setq truename (file-truename filename))
          (if find-file-visit-truename
              (setq filename truename))))
+    (if filename
+       (let ((new-name (file-name-nondirectory filename)))
+         (if (string= new-name "")
+             (error "Empty file name"))))
     (let ((buffer (and filename (find-buffer-visiting filename))))
       (and buffer (not (eq buffer (current-buffer)))
           (not no-query)
@@ -2376,8 +2565,6 @@ the old visited file has been renamed to the new name FILENAME."
     (setq buffer-file-name filename)
     (if filename                       ; make buffer name reflect filename.
        (let ((new-name (file-name-nondirectory buffer-file-name)))
-         (if (string= new-name "")
-             (error "Empty file name"))
          (if (eq system-type 'vax-vms)
              (setq new-name (downcase new-name)))
          (setq default-directory (file-name-directory buffer-file-name))
@@ -2539,6 +2726,7 @@ BACKUPNAME is the backup file name, which is the old file renamed."
                              backup-by-copying
                              ;; Don't rename a suid or sgid file.
                              (and modes (< 0 (logand modes #o6000)))
+                             (not (file-writable-p (file-name-directory real-file-name)))
                              (and backup-by-copying-when-linked
                                   (> (file-nlinks real-file-name) 1))
                              (and (or backup-by-copying-when-mismatch
@@ -2575,15 +2763,26 @@ BACKUPNAME is the backup file name, which is the old file renamed."
            (file-error nil))))))
 
 (defun backup-buffer-copy (from-name to-name modes)
-  (condition-case ()
-      (copy-file from-name to-name t t)
-    (file-error
-     ;; If copying fails because file TO-NAME
-     ;; is not writable, delete that file and try again.
-     (if (and (file-exists-p to-name)
-             (not (file-writable-p to-name)))
-        (delete-file to-name))
-     (copy-file from-name to-name t t)))
+  (let ((umask (default-file-modes)))
+    (unwind-protect
+       (progn
+         ;; Create temp files with strict access rights.  It's easy to
+         ;; loosen them later, whereas it's impossible to close the
+         ;; time-window of loose permissions otherwise.
+         (set-default-file-modes ?\700)
+         (while (condition-case ()
+                    (progn
+                      (condition-case nil
+                          (delete-file to-name)
+                        (file-error nil))
+                      (copy-file from-name to-name t t 'excl)
+                      nil)
+                  (file-already-exists t))
+           ;; The file was somehow created by someone else between
+           ;; `delete-file' and `copy-file', so let's try again.
+           nil))
+      ;; Reset the umask.
+      (set-default-file-modes umask)))
   (and modes
        (set-file-modes to-name (logand modes #o1777))))
 
@@ -2661,7 +2860,7 @@ the value is \"\"."
 
 (defcustom make-backup-file-name-function nil
   "A function to use instead of the default `make-backup-file-name'.
-A value of nil gives the default `make-backup-file-name' behaviour.
+A value of nil gives the default `make-backup-file-name' behavior.
 
 This could be buffer-local to do something special for specific
 files.  If you define it, you may need to change `backup-file-name-p'
@@ -2697,13 +2896,18 @@ ignored."
 
 (defun normal-backup-enable-predicate (name)
   "Default `backup-enable-predicate' function.
-Checks for files in `temporary-file-directory' or
-`small-temporary-file-directory'."
+Checks for files in `temporary-file-directory',
+`small-temporary-file-directory', and /tmp."
   (not (or (let ((comp (compare-strings temporary-file-directory 0 nil
                                        name 0 nil)))
             ;; Directory is under temporary-file-directory.
             (and (not (eq comp t))
                  (< comp (- (length temporary-file-directory)))))
+          (let ((comp (compare-strings "/tmp" 0 nil
+                                       name 0 nil)))
+            ;; Directory is under /tmp.
+            (and (not (eq comp t))
+                 (< comp (- (length "/tmp")))))
           (if small-temporary-file-directory
               (let ((comp (compare-strings small-temporary-file-directory
                                            0 nil
@@ -2800,7 +3004,7 @@ Uses the free variable `backup-extract-version-start', whose value should be
 the index in the name where the version number begins."
   (if (and (string-match "[0-9]+~$" fn backup-extract-version-start)
           (= (match-beginning 0) backup-extract-version-start))
-      (string-to-int (substring fn backup-extract-version-start -1))
+      (string-to-number (substring fn backup-extract-version-start -1))
       0))
 
 ;; I believe there is no need to alter this behavior for VMS;
@@ -2912,8 +3116,8 @@ on a DOS/Windows machine, it returns FILENAME on expanded form."
          (file-name-as-directory (expand-file-name (or directory
                                                        default-directory))))
     (setq filename (expand-file-name filename))
-    (let ((hf (find-file-name-handler filename 'file-remote-p))
-          (hd (find-file-name-handler directory 'file-remote-p)))
+    (let ((fremote (file-remote-p filename))
+          (dremote (file-remote-p directory)))
       (if ;; Conditions for separate trees
          (or
           ;; Test for different drives on DOS/Windows
@@ -2921,20 +3125,8 @@ on a DOS/Windows machine, it returns FILENAME on expanded form."
            ;; Should `cygwin' really be included here?  --stef
            (memq system-type '(ms-dos cygwin windows-nt))
            (not (eq t (compare-strings filename 0 2 directory 0 2))))
-          ;; Test for different remote file handlers
-          (not (eq hf hd))
           ;; Test for different remote file system identification
-          (and
-           hf
-           (let ((re (car (rassq hf file-name-handler-alist))))
-             (not
-              (equal
-               (and
-                (string-match re filename)
-                (substring filename 0 (match-end 0)))
-               (and
-                (string-match re directory)
-                (substring directory 0 (match-end 0))))))))
+          (not (equal fremote dremote)))
          filename
         (let ((ancestor ".")
              (filename-dir (file-name-as-directory filename)))
@@ -2962,18 +3154,19 @@ on a DOS/Windows machine, it returns FILENAME on expanded form."
             ancestor))))))
 \f
 (defun save-buffer (&optional args)
-  "Save current buffer in visited file if modified.  Versions described below.
+  "Save current buffer in visited file if modified.  Variations are described below.
 By default, makes the previous version into a backup file
  if previously requested or if this is the first save.
-With 1 \\[universal-argument], marks this version
+Prefixed with one \\[universal-argument], marks this version
  to become a backup when the next save is done.
-With 2 \\[universal-argument]'s,
+Prefixed with two \\[universal-argument]'s,
  unconditionally makes the previous version into a backup file.
-With 3 \\[universal-argument]'s, marks this version
+Prefixed with three \\[universal-argument]'s, marks this version
  to become a backup when the next save is done,
  and unconditionally makes the previous version into a backup file.
 
-With argument of 0, never make the previous version into a backup file.
+With a numeric argument of 0, never make the previous version
+into a backup file.
 
 If a file's name is FOO, the names of its numbered backup versions are
  FOO.~i~ for various integers i.  A non-numbered backup file is called FOO~.
@@ -3023,7 +3216,7 @@ the last real save, but optional arg FORCE non-nil means delete anyway."
 
 (defcustom before-save-hook nil
   "Normal hook that is run before a buffer is saved to its file."
-  :options '(copyright-update)
+  :options '(copyright-update time-stamp)
   :type 'hook
   :group 'files)
 
@@ -3048,7 +3241,7 @@ in such cases.")
   "Save the current buffer in its visited file, if it has been modified.
 The hooks `write-contents-functions' and `write-file-functions' get a chance
 to do the job of saving; if they do not, then the buffer is saved in
-the visited file file in the usual way.
+the visited file in the usual way.
 Before and after saving the buffer, this function runs
 `before-save-hook' and `after-save-hook', respectively."
   (interactive)
@@ -3097,6 +3290,7 @@ Before and after saving the buffer, this function runs
                   (not (and (eq selective-display t)
                             (= (char-after (1- (point-max))) ?\r)))
                   (or (eq require-final-newline t)
+                      (eq require-final-newline 'visit-save)
                       (and require-final-newline
                            (y-or-n-p
                             (format "Buffer %s does not end in newline.  Add one? "
@@ -3138,10 +3332,12 @@ Before and after saving the buffer, this function runs
 ;; but inhibited if one of write-file-functions returns non-nil.
 ;; It returns a value (MODES . BACKUPNAME), like backup-buffer.
 (defun basic-save-buffer-1 ()
-  (if save-buffer-coding-system
-      (let ((coding-system-for-write save-buffer-coding-system))
+  (prog1
+      (if save-buffer-coding-system
+         (let ((coding-system-for-write save-buffer-coding-system))
+           (basic-save-buffer-2))
        (basic-save-buffer-2))
-    (basic-save-buffer-2)))
+    (setq buffer-file-coding-system-explicit last-coding-system-used)))
 
 ;; This returns a value (MODES . BACKUPNAME), like backup-buffer.
 (defun basic-save-buffer-2 ()
@@ -3169,39 +3365,41 @@ Before and after saving the buffer, this function runs
          ;; This requires write access to the containing dir,
          ;; which is why we don't try it if we don't have that access.
          (let ((realname buffer-file-name)
-               tempname nogood i succeed
+               tempname succeed
+               (umask (default-file-modes))
                (old-modtime (visited-file-modtime)))
-           (setq i 0)
-           (setq nogood t)
-           ;; Find the temporary name to write under.
-           (while nogood
-             (setq tempname (format
-                             (if (and (eq system-type 'ms-dos)
-                                      (not (msdos-long-file-names)))
-                                 "%s#%d.tm#" ; MSDOS limits files to 8+3
-                               (if (memq system-type '(vax-vms axp-vms))
-                                   "%s$tmp$%d"
-                                 "%s#tmp#%d"))
-                             dir i))
-             (setq nogood (file-exists-p tempname))
-             (setq i (1+ i)))
+           ;; Create temp files with strict access rights.  It's easy to
+           ;; loosen them later, whereas it's impossible to close the
+           ;; time-window of loose permissions otherwise.
            (unwind-protect
-               (progn (clear-visited-file-modtime)
-                      (write-region (point-min) (point-max)
-                                    tempname nil realname
-                                    buffer-file-truename)
-                      (setq succeed t))
-             ;; If writing the temp file fails,
-             ;; delete the temp file.
-             (or succeed
-                 (progn
-                   (condition-case nil
-                       (delete-file tempname)
-                     (file-error nil))
-                   (set-visited-file-modtime old-modtime))))
-           ;; Since we have created an entirely new file
-           ;; and renamed it, make sure it gets the
-           ;; right permission bits set.
+               (progn
+                 (clear-visited-file-modtime)
+                 (set-default-file-modes ?\700)
+                 ;; Try various temporary names.
+                 ;; This code follows the example of make-temp-file,
+                 ;; but it calls write-region in the appropriate way
+                 ;; for saving the buffer.
+                 (while (condition-case ()
+                            (progn
+                              (setq tempname
+                                    (make-temp-name
+                                     (expand-file-name "tmp" dir)))
+                              (write-region (point-min) (point-max)
+                                            tempname nil  realname
+                                            buffer-file-truename 'excl)
+                              nil)
+                          (file-already-exists t))
+                   ;; The file was somehow created by someone else between
+                   ;; `make-temp-name' and `write-region', let's try again.
+                   nil)
+                 (setq succeed t))
+             ;; Reset the umask.
+             (set-default-file-modes umask)
+             ;; If we failed, restore the buffer's modtime.
+             (unless succeed
+               (set-visited-file-modtime old-modtime)))
+           ;; Since we have created an entirely new file,
+           ;; make sure it gets the right permission bits set.
            (setq setmodes (or setmodes (cons (file-modes buffer-file-name)
                                              buffer-file-name)))
            ;; We succeeded in writing the temp file,
@@ -3260,17 +3458,21 @@ This requires the external program `diff' to be in your `exec-path'."
        (recursive-edit)
        ;; Return nil to ask about BUF again.
        nil)
-     "display the current buffer")
+     "view this file")
     (?d diff-buffer-with-file
-       "show difference to last saved version"))
+       "view changes in file"))
   "ACTION-ALIST argument used in call to `map-y-or-n-p'.")
 (put 'save-some-buffers-action-alist 'risky-local-variable t)
 
+(defvar buffer-save-without-query nil
+  "Non-nil means `save-some-buffers' should save this buffer without asking.")
+(make-variable-buffer-local 'buffer-save-without-query)
+
 (defun save-some-buffers (&optional arg pred)
   "Save some modified file-visiting buffers.  Asks user about each one.
 You can answer `y' to save, `n' not to save, `C-r' to look at the
 buffer in question with `view-buffer' before deciding or `d' to
-view the differences using `diff-buffer-to-file'.
+view the differences using `diff-buffer-with-file'.
 
 Optional argument (the prefix) non-nil means save all with no questions.
 Optional second argument PRED determines which buffers are considered:
@@ -3283,8 +3485,18 @@ See `save-some-buffers-action-alist' if you want to
 change the additional actions you can take on files."
   (interactive "P")
   (save-window-excursion
-    (let* ((queried nil)
-          (files-done
+    (let* (queried some-automatic
+          files-done abbrevs-done)
+      (dolist (buffer (buffer-list))
+       ;; First save any buffers that we're supposed to save unconditionally.
+       ;; That way the following code won't ask about them.
+       (with-current-buffer buffer
+         (when (and buffer-save-without-query (buffer-modified-p))
+           (setq some-automatic t)
+           (save-buffer))))
+      ;; Ask about those buffers that merit it,
+      ;; and record the number thus saved.
+      (setq files-done
            (map-y-or-n-p
             (function
              (lambda (buffer)
@@ -3313,19 +3525,22 @@ change the additional actions you can take on files."
             (buffer-list)
             '("buffer" "buffers" "save")
             save-some-buffers-action-alist))
-          (abbrevs-done
-           (and save-abbrevs abbrevs-changed
-                (progn
-                  (if (or arg
-                          (eq save-abbrevs 'silently)
-                          (y-or-n-p (format "Save abbrevs in %s? "
-                                            abbrev-file-name)))
-                      (write-abbrev-file nil))
-                  ;; Don't keep bothering user if he says no.
-                  (setq abbrevs-changed nil)
-                  t))))
+      ;; Maybe to save abbrevs, and record whether
+      ;; we either saved them or asked to.
+      (and save-abbrevs abbrevs-changed
+          (progn
+            (if (or arg
+                    (eq save-abbrevs 'silently)
+                    (y-or-n-p (format "Save abbrevs in %s? "
+                                      abbrev-file-name)))
+                (write-abbrev-file nil))
+            ;; Don't keep bothering user if he says no.
+            (setq abbrevs-changed nil)
+            (setq abbrevs-done t)))
       (or queried (> files-done 0) abbrevs-done
-         (message "(No files need saving)")))))
+         (message (if some-automatic
+                      "(Some special files were saved without asking)"
+                    "(No files need saving)"))))))
 \f
 (defun not-modified (&optional arg)
   "Mark current buffer as unmodified, not needing to be saved.
@@ -3470,7 +3685,7 @@ The function you specify is responsible for updating (or preserving) point.")
 (defvar buffer-stale-function nil
   "Function to check whether a non-file buffer needs reverting.
 This should be a function with one optional argument NOCONFIRM.
-Auto Revert Mode sets NOCONFIRM to t.  The function should return
+Auto Revert Mode passes t for NOCONFIRM.  The function should return
 non-nil if the buffer should be reverted.  A return value of
 `fast' means that the need for reverting was not checked, but
 that reverting the buffer is fast.  The buffer is current when
@@ -3479,7 +3694,10 @@ this function is called.
 The idea behind the NOCONFIRM argument is that it should be
 non-nil if the buffer is going to be reverted without asking the
 user.  In such situations, one has to be careful with potentially
-time consuming operations.")
+time consuming operations.
+
+For more information on how this variable is used by Auto Revert mode,
+see Info node `(emacs-xtra)Supporting additional buffers'.")
 
 (defvar before-revert-hook nil
   "Normal hook for `revert-buffer' to run before reverting.
@@ -3536,91 +3754,93 @@ non-nil, it is called instead of rereading visited file contents."
   (interactive (list (not current-prefix-arg)))
   (if revert-buffer-function
       (funcall revert-buffer-function ignore-auto noconfirm)
-    (let* ((auto-save-p (and (not ignore-auto)
-                            (recent-auto-save-p)
-                            buffer-auto-save-file-name
-                            (file-readable-p buffer-auto-save-file-name)
-                            (y-or-n-p
-   "Buffer has been auto-saved recently.  Revert from auto-save file? ")))
-          (file-name (if auto-save-p
-                         buffer-auto-save-file-name
-                       buffer-file-name)))
-      (cond ((null file-name)
-            (error "Buffer does not seem to be associated with any file"))
-           ((or noconfirm
-                (and (not (buffer-modified-p))
-                     (let ((tail revert-without-query)
-                           (found nil))
-                       (while tail
-                         (if (string-match (car tail) file-name)
-                             (setq found t))
-                         (setq tail (cdr tail)))
-                       found))
-                (yes-or-no-p (format "Revert buffer from file %s? "
-                                     file-name)))
-            (run-hooks 'before-revert-hook)
-            ;; If file was backed up but has changed since,
-            ;; we shd make another backup.
-            (and (not auto-save-p)
-                 (not (verify-visited-file-modtime (current-buffer)))
-                 (setq buffer-backed-up nil))
-            ;; Get rid of all undo records for this buffer.
-            (or (eq buffer-undo-list t)
-                (setq buffer-undo-list nil))
-            ;; Effectively copy the after-revert-hook status,
-            ;; since after-find-file will clobber it.
-            (let ((global-hook (default-value 'after-revert-hook))
-                  (local-hook-p (local-variable-p 'after-revert-hook))
-                  (local-hook (and (local-variable-p 'after-revert-hook)
-                                   after-revert-hook)))
-              (let (buffer-read-only
-                    ;; Don't make undo records for the reversion.
-                    (buffer-undo-list t))
-                (if revert-buffer-insert-file-contents-function
-                    (funcall revert-buffer-insert-file-contents-function
-                             file-name auto-save-p)
-                  (if (not (file-exists-p file-name))
-                      (error (if buffer-file-number
-                                 "File %s no longer exists!"
-                               "Cannot revert nonexistent file %s")
-                             file-name))
-                  ;; Bind buffer-file-name to nil
-                  ;; so that we don't try to lock the file.
-                  (let ((buffer-file-name nil))
-                    (or auto-save-p
-                        (unlock-buffer)))
-                  (widen)
-                  (let ((coding-system-for-read
-                         ;; Auto-saved file shoule be read without
-                         ;; any code conversion.
-                         (if auto-save-p 'emacs-mule-unix
-                           (or coding-system-for-read
-                               buffer-file-coding-system))))
-                    ;; This force after-insert-file-set-coding
-                    ;; (called from insert-file-contents) to set
-                    ;; buffer-file-coding-system to a proper value.
-                    (kill-local-variable 'buffer-file-coding-system)
-
-                    ;; Note that this preserves point in an intelligent way.
-                    (if preserve-modes
-                        (let ((buffer-file-format buffer-file-format))
-                          (insert-file-contents file-name (not auto-save-p)
-                                                nil nil t))
-                      (insert-file-contents file-name (not auto-save-p)
-                                            nil nil t)))))
-              ;; Recompute the truename in case changes in symlinks
-              ;; have changed the truename.
-              (setq buffer-file-truename
-                    (abbreviate-file-name (file-truename buffer-file-name)))
-              (after-find-file nil nil t t preserve-modes)
-              ;; Run after-revert-hook as it was before we reverted.
-              (setq-default revert-buffer-internal-hook global-hook)
-              (if local-hook-p
-                  (set (make-local-variable 'revert-buffer-internal-hook)
-                       local-hook)
-                (kill-local-variable 'revert-buffer-internal-hook))
-              (run-hooks 'revert-buffer-internal-hook))
-            t)))))
+    (with-current-buffer (or (buffer-base-buffer (current-buffer))
+                            (current-buffer))
+      (let* ((auto-save-p (and (not ignore-auto)
+                              (recent-auto-save-p)
+                              buffer-auto-save-file-name
+                              (file-readable-p buffer-auto-save-file-name)
+                              (y-or-n-p
+     "Buffer has been auto-saved recently.  Revert from auto-save file? ")))
+            (file-name (if auto-save-p
+                           buffer-auto-save-file-name
+                         buffer-file-name)))
+       (cond ((null file-name)
+              (error "Buffer does not seem to be associated with any file"))
+             ((or noconfirm
+                  (and (not (buffer-modified-p))
+                       (let ((tail revert-without-query)
+                             (found nil))
+                         (while tail
+                           (if (string-match (car tail) file-name)
+                               (setq found t))
+                           (setq tail (cdr tail)))
+                         found))
+                  (yes-or-no-p (format "Revert buffer from file %s? "
+                                       file-name)))
+              (run-hooks 'before-revert-hook)
+              ;; If file was backed up but has changed since,
+              ;; we shd make another backup.
+              (and (not auto-save-p)
+                   (not (verify-visited-file-modtime (current-buffer)))
+                   (setq buffer-backed-up nil))
+              ;; Get rid of all undo records for this buffer.
+              (or (eq buffer-undo-list t)
+                  (setq buffer-undo-list nil))
+              ;; Effectively copy the after-revert-hook status,
+              ;; since after-find-file will clobber it.
+              (let ((global-hook (default-value 'after-revert-hook))
+                    (local-hook-p (local-variable-p 'after-revert-hook))
+                    (local-hook (and (local-variable-p 'after-revert-hook)
+                                     after-revert-hook)))
+                (let (buffer-read-only
+                      ;; Don't make undo records for the reversion.
+                      (buffer-undo-list t))
+                  (if revert-buffer-insert-file-contents-function
+                      (funcall revert-buffer-insert-file-contents-function
+                               file-name auto-save-p)
+                    (if (not (file-exists-p file-name))
+                        (error (if buffer-file-number
+                                   "File %s no longer exists!"
+                                 "Cannot revert nonexistent file %s")
+                               file-name))
+                    ;; Bind buffer-file-name to nil
+                    ;; so that we don't try to lock the file.
+                    (let ((buffer-file-name nil))
+                      (or auto-save-p
+                          (unlock-buffer)))
+                    (widen)
+                    (let ((coding-system-for-read
+                           ;; Auto-saved file shoule be read by Emacs'
+                           ;; internal coding.
+                           (if auto-save-p 'auto-save-coding
+                             (or coding-system-for-read
+                                 buffer-file-coding-system-explicit))))
+                      ;; This force after-insert-file-set-coding
+                      ;; (called from insert-file-contents) to set
+                      ;; buffer-file-coding-system to a proper value.
+                      (kill-local-variable 'buffer-file-coding-system)
+
+                      ;; Note that this preserves point in an intelligent way.
+                      (if preserve-modes
+                          (let ((buffer-file-format buffer-file-format))
+                            (insert-file-contents file-name (not auto-save-p)
+                                                  nil nil t))
+                        (insert-file-contents file-name (not auto-save-p)
+                                              nil nil t)))))
+                ;; Recompute the truename in case changes in symlinks
+                ;; have changed the truename.
+                (setq buffer-file-truename
+                      (abbreviate-file-name (file-truename buffer-file-name)))
+                (after-find-file nil nil t t preserve-modes)
+                ;; Run after-revert-hook as it was before we reverted.
+                (setq-default revert-buffer-internal-hook global-hook)
+                (if local-hook-p
+                    (set (make-local-variable 'revert-buffer-internal-hook)
+                         local-hook)
+                  (kill-local-variable 'revert-buffer-internal-hook))
+                (run-hooks 'revert-buffer-internal-hook))
+              t))))))
 
 (defun recover-this-file ()
   "Recover the visited file--get contents from its last auto-save file."
@@ -3782,7 +4002,7 @@ This command is used in the special Dired buffer created by
 
 (defun kill-some-buffers (&optional list)
   "Kill some buffers.  Asks the user whether to kill each one of them.
-Non-interactively, if optional argument LIST is non-`nil', it
+Non-interactively, if optional argument LIST is non-nil, it
 specifies the list of buffers to kill, asking for approval for each one."
   (interactive)
   (if (null list)
@@ -3790,8 +4010,10 @@ specifies the list of buffers to kill, asking for approval for each one."
   (while list
     (let* ((buffer (car list))
           (name (buffer-name buffer)))
-      (and (not (string-equal name ""))
-          (/= (aref name 0) ? )
+      (and name                                ; Can be nil for an indirect buffer
+                                       ; if we killed the base buffer.
+          (not (string-equal name ""))
+          (/= (aref name 0) ?\s)
           (yes-or-no-p
            (format "Buffer %s %s.  Kill? "
                    name
@@ -3843,53 +4065,57 @@ Does not consider `auto-save-visited-file-name' as that variable is checked
 before calling this function.  You can redefine this for customization.
 See also `auto-save-file-name-p'."
   (if buffer-file-name
-      (let ((list auto-save-file-name-transforms)
-           (filename buffer-file-name)
-           result uniq)
-       ;; Apply user-specified translations
-       ;; to the file name.
-       (while (and list (not result))
-         (if (string-match (car (car list)) filename)
-             (setq result (replace-match (cadr (car list)) t nil
-                                         filename)
-                   uniq (car (cddr (car list)))))
-         (setq list (cdr list)))
-       (if result
-           (if uniq
-               (setq filename (concat
-                               (file-name-directory result)
-                               (subst-char-in-string
-                                ?/ ?!
-                                (replace-regexp-in-string "!" "!!"
-                                                          filename))))
-             (setq filename result)))
-       (setq result
-             (if (and (eq system-type 'ms-dos)
-                      (not (msdos-long-file-names)))
-                 ;; We truncate the file name to DOS 8+3 limits
-                 ;; before doing anything else, because the regexp
-                 ;; passed to string-match below cannot handle
-                 ;; extensions longer than 3 characters, multiple
-                 ;; dots, and other atrocities.
-                 (let ((fn (dos-8+3-filename
-                            (file-name-nondirectory buffer-file-name))))
-                   (string-match
-                    "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'"
-                    fn)
-                   (concat (file-name-directory buffer-file-name)
-                           "#" (match-string 1 fn)
-                           "." (match-string 3 fn) "#"))
-               (concat (file-name-directory filename)
-                       "#"
-                       (file-name-nondirectory filename)
-                       "#")))
-       ;; Make sure auto-save file names don't contain characters
-       ;; invalid for the underlying filesystem.
-       (if (and (memq system-type '(ms-dos windows-nt))
-                ;; Don't modify remote (ange-ftp) filenames
-                (not (string-match "^/\\w+@[-A-Za-z0-9._]+:" result)))
-           (convert-standard-filename result)
-         result))
+      (let ((handler (find-file-name-handler buffer-file-name
+                                            'make-auto-save-file-name)))
+       (if handler
+           (funcall handler 'make-auto-save-file-name)
+         (let ((list auto-save-file-name-transforms)
+               (filename buffer-file-name)
+               result uniq)
+           ;; Apply user-specified translations
+           ;; to the file name.
+           (while (and list (not result))
+             (if (string-match (car (car list)) filename)
+                 (setq result (replace-match (cadr (car list)) t nil
+                                             filename)
+                       uniq (car (cddr (car list)))))
+             (setq list (cdr list)))
+           (if result
+               (if uniq
+                   (setq filename (concat
+                                   (file-name-directory result)
+                                   (subst-char-in-string
+                                    ?/ ?!
+                                    (replace-regexp-in-string "!" "!!"
+                                                              filename))))
+                 (setq filename result)))
+           (setq result
+                 (if (and (eq system-type 'ms-dos)
+                          (not (msdos-long-file-names)))
+                     ;; We truncate the file name to DOS 8+3 limits
+                     ;; before doing anything else, because the regexp
+                     ;; passed to string-match below cannot handle
+                     ;; extensions longer than 3 characters, multiple
+                     ;; dots, and other atrocities.
+                     (let ((fn (dos-8+3-filename
+                                (file-name-nondirectory buffer-file-name))))
+                       (string-match
+                        "\\`\\([^.]+\\)\\(\\.\\(..?\\)?.?\\|\\)\\'"
+                        fn)
+                       (concat (file-name-directory buffer-file-name)
+                               "#" (match-string 1 fn)
+                               "." (match-string 3 fn) "#"))
+                   (concat (file-name-directory filename)
+                           "#"
+                           (file-name-nondirectory filename)
+                           "#")))
+           ;; Make sure auto-save file names don't contain characters
+           ;; invalid for the underlying filesystem.
+           (if (and (memq system-type '(ms-dos windows-nt))
+                    ;; Don't modify remote (ange-ftp) filenames
+                    (not (string-match "^/\\w+@[-A-Za-z0-9._]+:" result)))
+               (convert-standard-filename result)
+             result))))
 
     ;; Deal with buffers that don't have any associated files.  (Mail
     ;; mode tends to create a good number of these.)
@@ -4174,9 +4400,9 @@ preference to the program given by this variable."
   :group 'dired)
 
 (defun get-free-disk-space (dir)
-  "Return the mount of free space on directory DIR's file system.
+  "Return the amount of free space on directory DIR's file system.
 The result is a string that gives the number of free 1KB blocks,
-or nil if the system call or the program which retrieve the infornmation
+or nil if the system call or the program which retrieve the information
 fail.
 
 This function calls `file-system-info' if it is available, or invokes the
@@ -4209,6 +4435,8 @@ program specified by `directory-free-space-program' if that is non-nil."
                  (buffer-substring (point) end)))))))))
 
 
+(defvar insert-directory-ls-version 'unknown)
+
 ;; insert-directory
 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
 ;;   FULL-DIRECTORY-P is nil.
@@ -4318,6 +4546,56 @@ normally equivalent short `-D' option is just passed on to
                                   (concat (file-name-as-directory file) ".")
                                 file))))))))
 
+         ;; If we got "//DIRED//" in the output, it means we got a real
+         ;; directory listing, even if `ls' returned nonzero.
+         ;; So ignore any errors.
+         (when (if (stringp switches)
+                   (string-match "--dired\\>" switches)
+                 (member "--dired" switches))
+           (save-excursion
+             (forward-line -2)
+             (when (looking-at "//SUBDIRED//")
+               (forward-line -1))
+             (if (looking-at "//DIRED//")
+                 (setq result 0))))
+
+         (when (and (not (eq 0 result))
+                    (eq insert-directory-ls-version 'unknown))
+           ;; The first time ls returns an error,
+           ;; find the version numbers of ls,
+           ;; and set insert-directory-ls-version
+           ;; to > if it is more than 5.2.1, < if it is less, nil if it
+           ;; is equal or if the info cannot be obtained.
+           ;; (That can mean it isn't GNU ls.)
+           (let ((version-out
+                  (with-temp-buffer
+                    (call-process "ls" nil t nil "--version")
+                    (buffer-string))))
+             (if (string-match "ls (.*utils) \\([0-9.]*\\)$" version-out)
+                 (let* ((version (match-string 1 version-out))
+                        (split (split-string version "[.]"))
+                        (numbers (mapcar 'string-to-number split))
+                        (min '(5 2 1))
+                        comparison)
+                   (while (and (not comparison) (or numbers min))
+                     (cond ((null min)
+                            (setq comparison '>))
+                           ((null numbers)
+                            (setq comparison '<))
+                           ((> (car numbers) (car min))
+                            (setq comparison '>))
+                           ((< (car numbers) (car min))
+                            (setq comparison '<))
+                           (t
+                            (setq numbers (cdr numbers)
+                                  min (cdr min)))))
+                   (setq insert-directory-ls-version (or comparison '=)))
+               (setq insert-directory-ls-version nil))))
+
+         ;; For GNU ls versions 5.2.2 and up, ignore minor errors.
+         (when (and (eq 1 result) (eq insert-directory-ls-version '>))
+           (setq result 0))
+
          ;; If `insert-directory-program' failed, signal an error.
          (unless (eq 0 result)
            ;; Delete the error message it may have output.
@@ -4340,18 +4618,40 @@ normally equivalent short `-D' option is just passed on to
          (when (if (stringp switches)
                    (string-match "--dired\\>" switches)
                  (member "--dired" switches))
+           ;; The following overshoots by one line for an empty
+           ;; directory listed with "--dired", but without "-a"
+           ;; switch, where the ls output contains a
+           ;; "//DIRED-OPTIONS//" line, but no "//DIRED//" line.
+           ;; We take care of that case later.
            (forward-line -2)
             (when (looking-at "//SUBDIRED//")
               (delete-region (point) (progn (forward-line 1) (point)))
               (forward-line -1))
            (if (looking-at "//DIRED//")
-               (let ((end (line-end-position)))
+               (let ((end (line-end-position))
+                     (linebeg (point))
+                     error-lines)
+                 ;; Find all the lines that are error messages,
+                 ;; and record the bounds of each one.
+                 (goto-char beg)
+                 (while (< (point) linebeg)
+                   (or (eql (following-char) ?\s)
+                       (push (list (point) (line-end-position)) error-lines))
+                   (forward-line 1))
+                 (setq error-lines (nreverse error-lines))
+                 ;; Now read the numeric positions of file names.
+                 (goto-char linebeg)
                  (forward-word 1)
                  (forward-char 3)
                  (while (< (point) end)
-                   (let ((start (+ beg (read (current-buffer))))
-                         (end (+ beg (read (current-buffer)))))
-                     (if (= (char-after end) ?\n)
+                   (let ((start (insert-directory-adj-pos
+                                 (+ beg (read (current-buffer)))
+                                 error-lines))
+                         (end (insert-directory-adj-pos
+                               (+ beg (read (current-buffer)))
+                               error-lines)))
+                     (if (memq (char-after end) '(?\n ?\s))
+                         ;; End is followed by \n or by " -> ".
                          (put-text-property start end 'dired-filename t)
                        ;; It seems that we can't trust ls's output as to
                        ;; byte positions of filenames.
@@ -4359,11 +4659,13 @@ normally equivalent short `-D' option is just passed on to
                        (end-of-line))))
                  (goto-char end)
                  (beginning-of-line)
-                 (delete-region (point) (progn (forward-line 2) (point))))
-             (forward-line 1)
-             (if (looking-at "//DIRED-OPTIONS//")
-                 (delete-region (point) (progn (forward-line 1) (point)))
-               (forward-line 1))))
+                 (delete-region (point) (progn (forward-line 1) (point))))
+             ;; Take care of the case where the ls output contains a
+             ;; "//DIRED-OPTIONS//"-line, but no "//DIRED//"-line
+             ;; and we went one line too far back (see above).
+             (forward-line 1))
+           (if (looking-at "//DIRED-OPTIONS//")
+               (delete-region (point) (progn (forward-line 1) (point)))))
 
          ;; Now decode what read if necessary.
          (let ((coding (or coding-system-for-read
@@ -4411,6 +4713,18 @@ normally equivalent short `-D' option is just passed on to
                      (end-of-line)
                      (insert " available " available)))))))))))
 
+(defun insert-directory-adj-pos (pos error-lines)
+  "Convert `ls --dired' file name position value POS to a buffer position.
+File name position values returned in ls --dired output
+count only stdout; they don't count the error messages sent to stderr.
+So this function converts to them to real buffer positions.
+ERROR-LINES is a list of buffer positions of error message lines,
+of the form (START END)."
+  (while (and error-lines (< (caar error-lines) pos))
+    (setq pos (+ pos (- (nth 1 (car error-lines)) (nth 0 (car error-lines)))))
+    (pop error-lines))
+  pos)
+
 (defun insert-directory-safely (file switches
                                     &optional wildcard full-directory-p)
   "Insert directory listing for FILE, formatted according to SWITCHES.
@@ -4542,7 +4856,8 @@ With prefix arg, silently save all file-visiting buffers, then kill."
          ((eq method 'add)
           (concat "/:" (apply operation arguments)))
          ((eq method 'quote)
-          (prog1 (apply operation arguments)
+          (unwind-protect
+              (apply operation arguments)
             (setq buffer-file-name (concat "/:" buffer-file-name))))
          ((eq method 'unquote-then-quote)
           (let (res)
@@ -4576,5 +4891,5 @@ With prefix arg, silently save all file-visiting buffers, then kill."
 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame)
 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame)
 
-;;; arch-tag: bc68d3ea-19ca-468b-aac6-3a4a7766101f
+;; arch-tag: bc68d3ea-19ca-468b-aac6-3a4a7766101f
 ;;; files.el ends here