Replace `iff' in doc-strings and comments.
[bpt/emacs.git] / lisp / files.el
index 285cd50..4d952f3 100644 (file)
@@ -2,7 +2,7 @@
 
 ;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994, 1995, 1996,
 ;;   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-;;   2006 Free Software Foundation, Inc.
+;;   2006, 2007 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 
@@ -10,7 +10,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -44,7 +44,7 @@
 
 
 (defcustom delete-auto-save-files t
-  "*Non-nil means delete auto-save file when a buffer is saved or killed.
+  "Non-nil means delete auto-save file when a buffer is saved or killed.
 
 Note that the auto-save file will not be deleted if the buffer is killed
 when it has unsaved changes."
@@ -53,7 +53,7 @@ when it has unsaved changes."
 
 (defcustom directory-abbrev-alist
   nil
-  "*Alist of abbreviations for file directories.
+  "Alist of abbreviations for file directories.
 A list of elements of the form (FROM . TO), each meaning to replace
 FROM with TO when it appears in a directory name.  This replacement is
 done when setting up the default directory of a newly visited file.
@@ -74,7 +74,7 @@ the name it is linked to."
 
 ;; Turn off backup files on VMS since it has version numbers.
 (defcustom make-backup-files (not (eq system-type 'vax-vms))
-  "*Non-nil means make a backup of a file the first time it is saved.
+  "Non-nil means make a backup of a file the first time it is saved.
 This can be done by renaming the file or by copying.
 
 Renaming means that Emacs renames the existing file so that it is a
@@ -103,20 +103,20 @@ But it is local only if you make it local.")
 (put 'backup-inhibited 'permanent-local t)
 
 (defcustom backup-by-copying nil
- "*Non-nil means always use copying to create backup files.
+ "Non-nil means always use copying to create backup files.
 See documentation of variable `make-backup-files'."
  :type 'boolean
  :group 'backup)
 
 (defcustom backup-by-copying-when-linked nil
- "*Non-nil means use copying to create backups for files with multiple names.
+ "Non-nil means use copying to create backups for files with multiple names.
 This causes the alternate names to refer to the latest version as edited.
 This variable is relevant only if `backup-by-copying' is nil."
  :type 'boolean
  :group 'backup)
 
 (defcustom backup-by-copying-when-mismatch nil
-  "*Non-nil means create backups by copying if this preserves owner or group.
+  "Non-nil means create backups by copying if this preserves owner or group.
 Renaming may still be used (subject to control of other variables)
 when it would not result in changing the owner or group of the file;
 that is, for files which are owned by you and whose group matches
@@ -126,7 +126,7 @@ This variable is relevant only if `backup-by-copying' is nil."
   :group 'backup)
 
 (defcustom backup-by-copying-when-privileged-mismatch 200
-  "*Non-nil means create backups by copying to preserve a privileged owner.
+  "Non-nil means create backups by copying to preserve a privileged owner.
 Renaming may still be used (subject to control of other variables)
 when it would not result in changing the owner of the file or if the owner
 has a user id greater than the value of this variable.  This is useful
@@ -142,7 +142,7 @@ This variable is relevant only if `backup-by-copying' and
 Called with an absolute file name as argument, it returns t to enable backup.")
 
 (defcustom buffer-offer-save nil
-  "*Non-nil in a buffer means always offer to save buffer on exit.
+  "Non-nil in a buffer means always offer to save buffer on exit.
 Do so even if the buffer is not visiting a file.
 Automatically local in all buffers."
   :type 'boolean
@@ -150,7 +150,7 @@ Automatically local in all buffers."
 (make-variable-buffer-local 'buffer-offer-save)
 
 (defcustom find-file-existing-other-name t
-  "*Non-nil means find a file under alternative names, in existing buffers.
+  "Non-nil means find a file under alternative names, in existing buffers.
 This means if any existing buffer is visiting the file you want
 under another name, you get the existing buffer instead of a new buffer."
   :type 'boolean
@@ -162,9 +162,10 @@ The truename of a file is found by chasing all links
 both at the file level and at the levels of the containing directories."
   :type 'boolean
   :group 'find-file)
+(put 'find-file-visit-truename 'safe-local-variable 'booleanp)
 
 (defcustom revert-without-query nil
-  "*Specify which files should be reverted without query.
+  "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,
 then `revert-buffer' reverts the file without querying
@@ -225,7 +226,7 @@ have fast storage with limited space, such as a RAM disk."
   "Regexp recognizing file names which aren't allowed by the filesystem.")
 
 (defcustom file-precious-flag nil
-  "*Non-nil means protect against I/O errors while saving files.
+  "Non-nil means protect against I/O errors while saving files.
 Some modes set this non-nil in particular buffers.
 
 This feature works by writing the new contents into a temporary file
@@ -240,24 +241,26 @@ breaks any hard links between it and other files."
   :group 'backup)
 
 (defcustom version-control nil
-  "*Control use of version numbers for backup files.
-t means make numeric backup versions unconditionally.
-nil means make them for files that have some already.
-`never' means do not make them."
+  "Control use of version numbers for backup files.
+When t, make numeric backup versions unconditionally.
+When nil, make them for files that have some already.
+The value `never' means do not make them."
   :type '(choice (const :tag "Never" never)
                 (const :tag "If existing" nil)
                 (other :tag "Always" t))
   :group 'backup
   :group 'vc)
+(put 'version-control 'safe-local-variable
+     '(lambda (x) (or (booleanp x) (equal x 'never))))
 
 (defcustom dired-kept-versions 2
-  "*When cleaning directory, number of versions to keep."
+  "When cleaning directory, number of versions to keep."
   :type 'integer
   :group 'backup
   :group 'dired)
 
 (defcustom delete-old-versions nil
-  "*If t, delete excess backup versions silently.
+  "If t, delete excess backup versions silently.
 If nil, ask confirmation.  Any other value prevents any trimming."
   :type '(choice (const :tag "Delete" t)
                 (const :tag "Ask" nil)
@@ -265,24 +268,26 @@ If nil, ask confirmation.  Any other value prevents any trimming."
   :group 'backup)
 
 (defcustom kept-old-versions 2
-  "*Number of oldest versions to keep when a new numbered backup is made."
+  "Number of oldest versions to keep when a new numbered backup is made."
   :type 'integer
   :group 'backup)
+(put 'kept-old-versions 'safe-local-variable 'integerp)
 
 (defcustom kept-new-versions 2
-  "*Number of newest versions to keep when a new numbered backup is made.
+  "Number of newest versions to keep when a new numbered backup is made.
 Includes the new backup.  Must be > 0"
   :type 'integer
   :group 'backup)
+(put 'kept-new-versions 'safe-local-variable 'integerp)
 
 (defcustom require-final-newline nil
-  "*Whether to add a newline automatically at the end of the file.
+  "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.
+A value of nil means don't add newlines.
 
 Certain major modes set this locally to the value obtained
 from `mode-require-final-newline'."
@@ -294,7 +299,7 @@ from `mode-require-final-newline'."
   :group 'editing-basics)
 
 (defcustom mode-require-final-newline t
-  "*Whether to add a newline at end of file, in certain major modes.
+  "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.
@@ -304,10 +309,10 @@ 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."
+A value of 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)
@@ -317,12 +322,12 @@ a final newline, whenever you save a file that really needs one."
   :version "22.1")
 
 (defcustom auto-save-default t
-  "*Non-nil says by default do auto-saving of every file-visiting buffer."
+  "Non-nil says by default do auto-saving of every file-visiting buffer."
   :type 'boolean
   :group 'auto-save)
 
 (defcustom auto-save-visited-file-name nil
-  "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
+  "Non-nil says auto-save a buffer in the file it is visiting, when practical.
 Normally auto-save files are written under other names."
   :type 'boolean
   :group 'auto-save)
@@ -332,7 +337,7 @@ Normally auto-save files are written under other names."
      ;; Don't put "\\2" inside expand-file-name, since it will be
      ;; transformed to "/2" on DOS/Windows.
      ,(concat temporary-file-directory "\\2") t))
-  "*Transforms to apply to buffer file name before making auto-save file name.
+  "Transforms to apply to buffer file name before making auto-save file name.
 Each transform is a list (REGEXP REPLACEMENT UNIQUIFY):
 REGEXP is a regular expression to match against the file name.
 If it matches, `replace-match' is used to replace the
@@ -359,19 +364,19 @@ ignored."
   :version "21.1")
 
 (defcustom save-abbrevs t
-  "*Non-nil means save word abbrevs too when files are saved.
+  "Non-nil means save word abbrevs too when files are saved.
 If `silently', don't ask the user before saving."
   :type '(choice (const t) (const nil) (const silently))
   :group 'abbrev)
 
 (defcustom find-file-run-dired t
-  "*Non-nil means allow `find-file' to visit directories.
+  "Non-nil means allow `find-file' to visit directories.
 To visit the directory, `find-file' runs `find-directory-functions'."
   :type 'boolean
   :group 'find-file)
 
 (defcustom find-directory-functions '(cvs-dired-noselect dired-noselect)
-  "*List of functions to try in sequence to visit a directory.
+  "List of functions to try in sequence to visit a directory.
 Each function is called with the directory name as the sole argument
 and should return either a buffer or nil."
   :type '(hook :options (cvs-dired-noselect dired-noselect))
@@ -443,14 +448,22 @@ use `before-save-hook'.")
     'write-contents-functions "22.1")
 
 (defcustom enable-local-variables t
-  "*Control use of local variables in files you visit.
-The value can be t, nil or something else.
+  "Control use of local variables in files you visit.
+The value can be t, nil, :safe, or something else.
 
 A value of t means file local variables specifications are obeyed
-if all the specified variables are safe.  If any variables are
-not safe, you will be queries before setting them.
-A value of nil means file local variables are ignored.
-Any other value means to always query.
+if all the specified variable values are safe; if any values are
+not safe, Emacs queries you, once, whether to set them all.
+\(When you say yes to certain values, they are remembered as safe.)
+
+:safe means set the safe variables, and ignore the rest.
+:all means set all variables, whether safe or not.
+ (Don't set it permanently to :all.)
+A value of nil means always ignore the file local variables.
+
+Any other value means always query you once whether to set them all.
+\(When you say yes to certain values, they are remembered as safe, but
+this has no effect when `enable-local-variables' is \"something else\".)
 
 This variable also controls use of major modes specified in
 a -*- line.
@@ -458,7 +471,9 @@ a -*- line.
 The command \\[normal-mode], when used interactively,
 always obeys file local variable specifications and the -*- line,
 and ignores this variable."
-  :type '(choice (const :tag "Obey" t)
+  :type '(choice (const :tag "Query Unsafe" t)
+                (const :tag "Safe Only" :safe)
+                (const :tag "Do all" :all)
                 (const :tag "Ignore" nil)
                 (other :tag "Query" other))
   :group 'find-file)
@@ -473,10 +488,10 @@ This variable does not affect the use of major modes
 specified in a -*- line.")
 
 (defcustom enable-local-eval 'maybe
-  "*Control processing of the \"variable\" `eval' in a file's local variables.
+  "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."
+A value of nil means ignore them; anything else means query."
   :type '(choice (const :tag "Obey" t)
                 (const :tag "Ignore" nil)
                 (other :tag "Query" other))
@@ -491,7 +506,7 @@ nil means ignore them; anything else means query."
     (defalias 'file-locked-p 'ignore))
 
 (defcustom view-read-only nil
-  "*Non-nil means buffers visiting files read-only do so in view mode.
+  "Non-nil means buffers visiting files read-only do so in view mode.
 In fact, this means that all read-only buffers normally have
 View mode enabled, including buffers that are read-only because
 you visit a file you cannot alter, and buffers you make read-only
@@ -499,6 +514,9 @@ using \\[toggle-read-only]."
   :type 'boolean
   :group 'view)
 
+(defvar file-name-history nil
+  "History list of file names entered in the minibuffer.")
+\f
 (put 'ange-ftp-completion-hook-function 'safe-magic t)
 (defun ange-ftp-completion-hook-function (op &rest args)
   "Provides support for ange-ftp host name completion.
@@ -525,13 +543,21 @@ 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
+characters.  On DOS, make sure to obey the 8.3 limitations.
+In the native Windows build, turn Cygwin names into native names,
+and also turn slashes into backslashes if the shell requires it (see
 `w32-shell-dos-semantics').
 
 See Info node `(elisp)Standard File Names' for more details."
-  filename)
+  (if (eq system-type 'cygwin)
+      (let ((name (copy-sequence filename))
+           (start 0))
+       ;; Replace invalid filename characters with !
+       (while (string-match "[?*:<>|\"\000-\037]" name start)
+         (aset name (match-beginning 0) ?!)
+         (setq start (match-end 0)))
+       name)
+    filename))
 
 (defun read-directory-name (prompt &optional dir default-dirname mustmatch initial)
   "Read directory name, prompting with PROMPT and completing in directory DIR.
@@ -676,7 +702,7 @@ PATH-AND-SUFFIXES is a pair of lists, (DIRECTORIES . SUFFIXES)."
        (when (file-directory-p dir)
          (dolist (file (file-name-all-completions
                         (file-name-nondirectory string) dir))
-           (push (if string-dir (concat string-dir file) file) names)
+           (add-to-list 'names (if string-dir (concat string-dir file) file))
            (when (string-match suffix file)
              (setq file (substring file 0 (match-beginning 0)))
              (push (if string-dir (concat string-dir file) file) names)))))
@@ -698,7 +724,7 @@ This is an interface to the function `load'."
   (interactive
    (list (completing-read "Load library: "
                          'locate-file-completion
-                         (cons load-path load-suffixes))))
+                         (cons load-path (get-load-suffixes)))))
   (load library))
 
 (defun file-remote-p (file)
@@ -989,6 +1015,20 @@ documentation for additional customization information."
     (pop-to-buffer buffer t norecord)
     (raise-frame (window-frame (selected-window)))))
 
+(defun display-buffer-other-frame (buffer)
+  "Switch to buffer BUFFER in another frame.
+This uses the function `display-buffer' as a subroutine; see its
+documentation for additional customization information."
+  (interactive "BDisplay buffer in other frame: ")
+  (let ((pop-up-frames t)
+       same-window-buffer-names same-window-regexps
+        (old-window (selected-window))
+       new-window)
+    (setq new-window (display-buffer buffer t))
+    (lower-frame (window-frame new-window))
+    (make-frame-invisible (window-frame old-window))
+    (make-frame-visible (window-frame old-window))))
+
 (defvar find-file-default nil
   "Used within `find-file-read-args'.")
 
@@ -1080,13 +1120,15 @@ expand wildcards (if any) and visit multiple files."
                (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 a file 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-existing (filename)
+   "Edit the existing file FILENAME.
+Like \\[find-file] but only allow a file that exists, and do not allow
+file names with wildcards."
+   (interactive (nbutlast (find-file-read-args "Find existing file: " t)))
+   (if (and (not (interactive-p)) (not (file-exists-p filename)))
+       (error "%s does not exist" filename)
+     (find-file filename)
+     (current-buffer)))
 
 (defun find-file-read-only (filename &optional wildcards)
   "Edit file FILENAME but don't allow changes.
@@ -1163,7 +1205,11 @@ 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.
 
 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
-expand wildcards (if any) and replace the file with multiple files."
+expand wildcards (if any) and replace the file with multiple files.
+
+If the current buffer is an indirect buffer, or the base buffer
+for one or more indirect buffers, the other buffer(s) are not
+killed."
   (interactive
    (let ((file buffer-file-name)
         (file-name nil)
@@ -1177,11 +1223,11 @@ expand wildcards (if any) and replace the file with multiple files."
   (unless (run-hook-with-args-until-failure 'kill-buffer-query-functions)
     (error "Aborted"))
   (when (and (buffer-modified-p) (buffer-file-name))
-    (if (yes-or-no-p (format "Buffer %s is modified; save it first? "
+    (if (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
                             (buffer-name)))
-       (save-buffer)
-      (unless (yes-or-no-p "Kill and replace the buffer without saving it? ")
-       (error "Aborted"))))
+       (unless (yes-or-no-p "Kill and replace the buffer without saving it? ")
+         (error "Aborted"))
+      (save-buffer)))
   (let ((obuf (current-buffer))
        (ofile buffer-file-name)
        (onum buffer-file-number)
@@ -1243,54 +1289,56 @@ Choose the buffer's name using `generate-new-buffer-name'."
 
 (defun abbreviate-file-name (filename)
   "Return a version of FILENAME shortened using `directory-abbrev-alist'.
-This also substitutes \"~\" for the user's home directory and
-removes automounter prefixes (see the variable `automount-dir-prefix')."
+This also substitutes \"~\" for the user's home directory (unless the
+home directory is a root directory) and removes automounter prefixes
+\(see the variable `automount-dir-prefix')."
   ;; Get rid of the prefixes added by the automounter.
-  (if (and automount-dir-prefix
-          (string-match automount-dir-prefix filename)
-          (file-exists-p (file-name-directory
-                          (substring filename (1- (match-end 0))))))
-      (setq filename (substring filename (1- (match-end 0)))))
-  (let ((tail directory-abbrev-alist))
-    ;; If any elt of directory-abbrev-alist matches this name,
-    ;; abbreviate accordingly.
-    (while tail
-      (if (string-match (car (car tail)) filename)
+  (save-match-data
+    (if (and automount-dir-prefix
+            (string-match automount-dir-prefix filename)
+            (file-exists-p (file-name-directory
+                            (substring filename (1- (match-end 0))))))
+       (setq filename (substring filename (1- (match-end 0)))))
+    (let ((tail directory-abbrev-alist))
+      ;; If any elt of directory-abbrev-alist matches this name,
+      ;; abbreviate accordingly.
+      (while tail
+       (if (string-match (car (car tail)) filename)
+           (setq filename
+                 (concat (cdr (car tail)) (substring filename (match-end 0)))))
+       (setq tail (cdr tail)))
+      ;; Compute and save the abbreviated homedir name.
+      ;; We defer computing this until the first time it's needed, to
+      ;; give time for directory-abbrev-alist to be set properly.
+      ;; We include a slash at the end, to avoid spurious matches
+      ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
+      (or abbreviated-home-dir
+         (setq abbreviated-home-dir
+               (let ((abbreviated-home-dir "$foo"))
+                 (concat "^" (abbreviate-file-name (expand-file-name "~"))
+                         "\\(/\\|\\'\\)"))))
+
+      ;; If FILENAME starts with the abbreviated homedir,
+      ;; make it start with `~' instead.
+      (if (and (string-match abbreviated-home-dir filename)
+              ;; If the home dir is just /, don't change it.
+              (not (and (= (match-end 0) 1)
+                        (= (aref filename 0) ?/)))
+              ;; MS-DOS root directories can come with a drive letter;
+              ;; Novell Netware allows drive letters beyond `Z:'.
+              (not (and (or (eq system-type 'ms-dos)
+                            (eq system-type 'cygwin)
+                            (eq system-type 'windows-nt))
+                        (save-match-data
+                          (string-match "^[a-zA-`]:/$" filename)))))
          (setq filename
-               (concat (cdr (car tail)) (substring filename (match-end 0)))))
-      (setq tail (cdr tail)))
-    ;; Compute and save the abbreviated homedir name.
-    ;; We defer computing this until the first time it's needed, to
-    ;; give time for directory-abbrev-alist to be set properly.
-    ;; We include a slash at the end, to avoid spurious matches
-    ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
-    (or abbreviated-home-dir
-       (setq abbreviated-home-dir
-             (let ((abbreviated-home-dir "$foo"))
-               (concat "^" (abbreviate-file-name (expand-file-name "~"))
-                       "\\(/\\|$\\)"))))
-
-    ;; If FILENAME starts with the abbreviated homedir,
-    ;; make it start with `~' instead.
-    (if (and (string-match abbreviated-home-dir filename)
-            ;; If the home dir is just /, don't change it.
-            (not (and (= (match-end 0) 1)
-                      (= (aref filename 0) ?/)))
-            ;; MS-DOS root directories can come with a drive letter;
-            ;; Novell Netware allows drive letters beyond `Z:'.
-            (not (and (or (eq system-type 'ms-dos)
-                          (eq system-type 'cygwin)
-                          (eq system-type 'windows-nt))
-                      (save-match-data
-                        (string-match "^[a-zA-`]:/$" filename)))))
-       (setq filename
-             (concat "~"
-                     (substring filename (match-beginning 1) (match-end 1))
-                     (substring filename (match-end 0)))))
-    filename))
+               (concat "~"
+                       (match-string 1 filename)
+                       (substring filename (match-end 0)))))
+      filename)))
 
 (defcustom find-file-not-true-dirname-list nil
-  "*List of logical names for which visiting shouldn't save the true dirname.
+  "List of logical names for which visiting shouldn't save the true dirname.
 On VMS, when you visit a file using a logical name that searches a path,
 you may or may not want the visited file name to record the specific
 directory where the file was found.  If you *do not* want that, add the logical
@@ -1323,7 +1371,7 @@ If there is no such live buffer, return nil."
                (number (nthcdr 10 attributes))
                (list (buffer-list)) found)
           (and buffer-file-numbers-unique
-               number
+               (car-safe number)       ;Make sure the inode is not just nil.
                (while (and (not found) list)
                  (with-current-buffer (car list)
                    (if (and buffer-file-name
@@ -1339,7 +1387,7 @@ If there is no such live buffer, return nil."
           found))))
 \f
 (defcustom find-file-wildcards t
-  "*Non-nil means file-visiting commands should handle wildcards.
+  "Non-nil means file-visiting commands should handle wildcards.
 For example, if you specify `*.c', that would visit all the files
 whose names match the pattern."
   :group 'files
@@ -1347,7 +1395,7 @@ whose names match the pattern."
   :type 'boolean)
 
 (defcustom find-file-suppress-same-file-warnings nil
-  "*Non-nil means suppress warning messages for symlinked files.
+  "Non-nil means suppress warning messages for symlinked files.
 When nil, Emacs prints a warning when visiting a file that is already
 visited, but with a different name.  Setting this option to t
 suppresses this warning."
@@ -1426,11 +1474,12 @@ the various files."
          (error "Aborted"))
        (if buf
            ;; We are using an existing buffer.
-           (progn
+           (let (nonexistent)
              (or nowarn
                  (verify-visited-file-modtime buf)
                  (cond ((not (file-exists-p filename))
-                        (error "File %s no longer exists!" filename))
+                        (setq nonexistent t)
+                        (message "File %s no longer exists!" filename))
                        ;; Certain files should be reverted automatically
                        ;; if they have changed on disk and not in the buffer.
                        ((and (not (buffer-modified-p buf))
@@ -1467,7 +1516,8 @@ the various files."
                ;; writable and vice versa, but if the buffer agrees
                ;; with the new state of the file, that is ok too.
                (let ((read-only (not (file-writable-p buffer-file-name))))
-                 (unless (or (eq read-only buffer-file-read-only)
+                 (unless (or nonexistent
+                             (eq read-only buffer-file-read-only)
                              (eq read-only buffer-read-only))
                    (when (or nowarn
                              (let ((question
@@ -1480,28 +1530,59 @@ the various files."
 
                (when (and (not (eq (not (null rawfile))
                                    (not (null find-file-literally))))
+                          (not nonexistent)
                           ;; It is confusing to ask whether to visit
                           ;; non-literally if they have the file in
                           ;; hexl-mode.
                           (not (eq major-mode 'hexl-mode)))
                  (if (buffer-modified-p)
-                     (if (y-or-n-p (if rawfile
-                                       "Save file and revisit literally? "
-                                     "Save file and revisit non-literally? "))
+                     (if (y-or-n-p
+                          (format
+                           (if rawfile
+                               "The file %s is already visited normally,
+and you have edited the buffer.  Now you have asked to visit it literally,
+meaning no coding system handling, format conversion, or local variables.
+Emacs can only visit a file in one way at a time.
+
+Do you want to save the file, and visit it literally instead? "
+                               "The file %s is already visited literally,
+meaning no coding system handling, format conversion, or local variables.
+You have edited the buffer.  Now you have asked to visit the file normally,
+but Emacs can only visit a file in one way at a time.
+
+Do you want to save the file, and visit it normally instead? ")
+                           (file-name-nondirectory filename)))
                          (progn
                            (save-buffer)
                            (find-file-noselect-1 buf filename nowarn
                                                  rawfile truename number))
-                       (if (y-or-n-p (if rawfile
-                                         "Discard your edits and revisit file literally? "
-                                       "Discard your edits and revisit file non-literally? "))
+                       (if (y-or-n-p
+                            (format
+                             (if rawfile
+                                 "\
+Do you want to discard your changes, and visit the file literally now? "
+                               "\
+Do you want to discard your changes, and visit the file normally now? ")))
                            (find-file-noselect-1 buf filename nowarn
                                                  rawfile truename number)
                          (error (if rawfile "File already visited non-literally"
                                   "File already visited literally"))))
-                   (if (y-or-n-p (if rawfile
-                                     "Revisit file literally? "
-                                   "Revisit file non-literally? "))
+                   (if (y-or-n-p
+                        (format
+                         (if rawfile
+                             "The file %s is already visited normally.
+You have asked to visit it literally,
+meaning no coding system decoding, format conversion, or local variables.
+But Emacs can only visit a file in one way at a time.
+
+Do you want to revisit the file literally now? "
+                           "The file %s is already visited literally,
+meaning no coding system decoding, format conversion, or local variables.
+You have asked to visit it normally,
+but Emacs can only visit a file in one way at a time.
+
+Do you want to revisit the file normally now? ")
+                         (file-name-nondirectory filename)))
                        (find-file-noselect-1 buf filename nowarn
                                              rawfile truename number)
                      (error (if rawfile "File already visited non-literally"
@@ -1548,7 +1629,7 @@ the various files."
             (kill-buffer buf)
             (signal 'file-error (list "File is not readable"
                                       filename)))
-          ;; Run find-file-not-found-hooks until one returns non-nil.
+          ;; Run find-file-not-found-functions until one returns non-nil.
           (or (run-hook-with-args-until-success 'find-file-not-found-functions)
               ;; If they fail too, set error.
               (setq error t)))))
@@ -1568,14 +1649,13 @@ the various files."
             (not (member logical find-file-not-true-dirname-list)))
           (setq buffer-file-name buffer-file-truename))
       (if find-file-visit-truename
-         (setq buffer-file-name
-               (setq filename
-                     (expand-file-name buffer-file-truename))))
+         (setq buffer-file-name (expand-file-name buffer-file-truename)))
       ;; Set buffer's default directory to that of the file.
       (setq default-directory (file-name-directory buffer-file-name))
       ;; Turn off backup files for certain file names.  Since
       ;; this is a permanent local, the major mode won't eliminate it.
-      (and (not (funcall backup-enable-predicate buffer-file-name))
+      (and backup-enable-predicate
+          (not (funcall backup-enable-predicate buffer-file-name))
           (progn
             (make-local-variable 'backup-inhibited)
             (setq backup-inhibited t)))
@@ -1764,8 +1844,7 @@ Uses the visited file name, the -*- line, and the local variables spec.
 
 This function is called automatically from `find-file'.  In that case,
 we may set up the file-specified mode and local variables,
-depending on the value of `enable-local-variables': if it is t, we do;
-if it is nil, we don't; otherwise, we query.
+depending on the value of `enable-local-variables'.
 In addition, if `local-enable-local-variables' is nil, we do
 not set local variables (though we do notice a mode specified with -*-.)
 
@@ -1792,6 +1871,16 @@ in that case, this function acts as if `enable-local-variables' were t."
   (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
       (ucs-set-table-for-input)))
 
+(defcustom auto-mode-case-fold nil
+  "Non-nil means to try second pass through `auto-mode-alist'.
+This means that if the first case-sensitive search through the alist fails
+to find a matching major mode, a second case-insensitive search is made.
+On systems with case-insensitive file names, this variable is ignored,
+since only a single case-insensitive search through the alist is made."
+  :group 'files
+  :version "22.1"
+  :type 'boolean)
+
 (defvar auto-mode-alist
   ;; Note: The entries for the modes defined in cc-mode.el (c-mode,
   ;; c++-mode, java-mode and more) are added through autoload
@@ -1819,13 +1908,14 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\.ad[bs].dg\\'" . ada-mode)
      ("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)
      ("Imakefile\\'" . makefile-imake-mode)
+     ("Makeppfile\\(?:\\.mk\\)?\\'" . makefile-makepp-mode) ; Put this before .mk
+     ("\\.makepp\\'" . makefile-makepp-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.
@@ -1834,7 +1924,7 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\.[sS]\\'" . asm-mode)
      ("\\.asm\\'" . asm-mode)
      ("[cC]hange\\.?[lL]og?\\'" . change-log-mode)
-     ("[cC]hange[lL]og\\.[0-9]+\\'" . 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)
@@ -1869,9 +1959,9 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\.tar\\'" . tar-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
+     ("\\.\\(arc\\|zip\\|lzh\\|lha\\|zoo\\|[jew]ar\\|xpi\\)\\'" . archive-mode)
+     ("\\.\\(ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\)\\'" . archive-mode)
+     ("\\.\\(sx[dmicw]\\|odt\\)\\'" . archive-mode)    ; OpenOffice.org
      ;; Mailer puts message to be edited in
      ;; /tmp/Re.... or Message
      ("\\`/tmp/Re" . text-mode)
@@ -1895,6 +1985,8 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("[:/]_emacs\\'" . emacs-lisp-mode)
      ("/crontab\\.X*[0-9]+\\'" . shell-script-mode)
      ("\\.ml\\'" . lisp-mode)
+     ;; Common Lisp ASDF package system.
+     ("\\.asd\\'" . lisp-mode)
      ("\\.\\(asn\\|mib\\|smi\\)\\'" . snmp-mode)
      ("\\.\\(as\\|mi\\|sm\\)2\\'" . snmpv2-mode)
      ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode)
@@ -1920,6 +2012,9 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("[/.]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)
+     ;; ChangeLog.old etc.  Other change-log-mode entries are above;
+     ;; this has lower priority to avoid matching changelog.sgml etc.
+     ("[cC]hange[lL]og[-.][-0-9a-z]+\\'" . change-log-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
@@ -1954,6 +2049,9 @@ 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.
 
+The extensions whose FUNCTION is `archive-mode' should also
+appear in `auto-coding-alist' with `no-conversion' coding system.
+
 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.")
@@ -2026,44 +2124,61 @@ 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.
+(defvar magic-mode-alist nil
+  "Alist of buffer beginnings vs. corresponding major mode functions.
+Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION).
+After visiting a file, if REGEXP matches the text at the beginning of the
+buffer, or calling MATCH-FUNCTION returns non-nil, `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.\")")
+(put 'magic-mode-alist 'risky-local-variable t)
+
+(defvar magic-fallback-mode-alist
+  `((image-type-auto-detected-p . image-mode)
+    ;; 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.
-    ;; We use [ \t\n] instead of `\\s ' to make regex overflow less likely.
+    ;; We use [ \t\r\n] instead of `\\s ' to make regex overflow less likely.
     (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
-            (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\n]*<\\)")))
-       (concat "\\(?:<\\?xml[ \t\n]+[^>]*>\\)?[ \t\n]*<"
+            (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
+       (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
                comment-re "*"
-               "\\(?:!DOCTYPE[ \t\n]+[^>]*>[ \t\n]*<[ \t\n]*" comment-re "*\\)?"
+               "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
                "[Hh][Tt][Mm][Ll]"))
      . html-mode)
+    ("<!DOCTYPE[ \t\r\n]+[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 "*-->[ \t\n]*<\\)")))
-       (concat "[ \t\n]*<" comment-re "*!DOCTYPE "))
+            (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
+       (concat "[ \t\r\n]*<" comment-re "*!DOCTYPE "))
      . sgml-mode)
-    ("%![^V]" . ps-mode)
+    ("%!PS" . 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.
+  "Like `magic-mode-alist' but has lower priority than `auto-mode-alist'.
+Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION).
+After visiting a file, if REGEXP matches the text at the beginning of the
+buffer, or calling MATCH-FUNCTION returns non-nil, `normal-mode' will
+call FUNCTION, provided that `magic-mode-alist' and `auto-mode-alist'
+have not specified a mode for this file.
 
-If FUNCTION is nil, then it is not called.  (That is a way of saying
-\"allow `auto-mode-alist' to decide for these files.\")")
+If FUNCTION is nil, then it is not called.")
+(put 'magic-fallback-mode-alist 'risky-local-variable t)
 
 (defvar magic-mode-regexp-match-limit 4000
-  "Upper limit on `magic-mode-alist' regexp matches.")
+  "Upper limit on `magic-mode-alist' regexp matches.
+Also applies to `magic-fallback-mode-alist'.")
 
 (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, 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'.
+To find the right major mode, this function checks for a -*- mode tag,
+checks if it uses an interpreter listed in `interpreter-mode-alist',
+matches the buffer beginning against `magic-mode-alist',
+compares the filename against the entries in `auto-mode-alist',
+then matches the buffer beginning against `magic-fallback-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'.
@@ -2072,7 +2187,8 @@ If `enable-local-variables' is nil, this function does not check for a
 -*- mode tag.
 
 If the optional argument KEEP-MODE-IF-SAME is non-nil, then we
-only set the major mode, if that would change it."
+set the major mode only if that would change it.  In other words
+we don't actually set it to the same mode the buffer already has."
   ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
   (let (end done mode modes)
     ;; Find a -*- mode tag
@@ -2110,10 +2226,10 @@ only set the major mode, if that would change it."
              (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.
     (unless done
-      ;; 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)
@@ -2126,7 +2242,7 @@ only set the major mode, if that would change it."
       ;; 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.
+    ;; Next try matching the buffer beginning against magic-mode-alist.
     (unless done
       (if (setq done (save-excursion
                       (goto-char (point-min))
@@ -2136,26 +2252,63 @@ only set the major mode, if that would change it."
                                                (+ (point-min) magic-mode-regexp-match-limit)))
                         (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)))))))))
+                                         (if (functionp re)
+                                             (funcall re)
+                                           (looking-at re)))))))
+         (set-auto-mode-0 done keep-mode-if-same)))
+    ;; Next compare the filename against the entries in auto-mode-alist.
+    (unless done
+      (if buffer-file-name
+         (let ((name buffer-file-name)
+               (remote-id (file-remote-p buffer-file-name)))
+           ;; Remove remote file name identification.
+           (when (and (stringp remote-id)
+                      (string-match (regexp-quote remote-id) name))
+             (setq name (substring name (match-end 0))))
+           ;; Remove backup-suffixes from file name.
+           (setq name (file-name-sans-versions name))
+           (while name
+             ;; Find first matching alist entry.
+             (setq mode
+                   (if (memq system-type '(vax-vms windows-nt cygwin))
+                       ;; System is case-insensitive.
+                       (let ((case-fold-search t))
+                         (assoc-default name auto-mode-alist
+                                        'string-match))
+                     ;; System is case-sensitive.
+                     (or
+                      ;; First match case-sensitively.
+                      (let ((case-fold-search nil))
+                        (assoc-default name auto-mode-alist
+                                       'string-match))
+                      ;; Fallback to case-insensitive match.
+                      (and auto-mode-case-fold
+                           (let ((case-fold-search t))
+                             (assoc-default name auto-mode-alist
+                                            'string-match))))))
+             (if (and mode
+                      (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)
+               (setq done t))))))
+    ;; Next try matching the buffer beginning against magic-fallback-mode-alist.
+    (unless done
+      (if (setq done (save-excursion
+                      (goto-char (point-min))
+                      (save-restriction
+                        (narrow-to-region (point-min)
+                                          (min (point-max)
+                                               (+ (point-min) magic-mode-regexp-match-limit)))
+                        (assoc-default nil magic-fallback-mode-alist
+                                       (lambda (re dummy)
+                                         (if (functionp re)
+                                             (funcall re)
+                                           (looking-at re)))))))
+         (set-auto-mode-0 done 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
@@ -2166,14 +2319,12 @@ only set the major mode, if that would change 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))
+  (unless (and keep-mode-if-same
+              (eq (indirect-function mode)
+                  (indirect-function major-mode)))
+    (when mode
+      (funcall mode)
+      mode)))
 
 (defun set-auto-mode-1 ()
   "Find the -*- spec in the buffer.
@@ -2218,6 +2369,122 @@ Otherwise, return nil; point may be changed."
        (setq end (point))
        (goto-char beg)
        end))))
+\f
+;;; Handling file local variables
+
+(defvar ignored-local-variables
+  '(ignored-local-variables safe-local-variable-values)
+  "Variables to be ignored in a file's local variable spec.")
+
+(defvar hack-local-variables-hook nil
+  "Normal hook run after processing a file's local variables specs.
+Major modes can use this to examine user-specified local variables
+in order to initialize other data structure based on them.")
+
+(defcustom safe-local-variable-values nil
+  "List variable-value pairs that are considered safe.
+Each element is a cons cell (VAR . VAL), where VAR is a variable
+symbol and VAL is a value that is considered safe."
+  :group 'find-file
+  :type  'alist)
+
+(defcustom safe-local-eval-forms nil
+  "Expressions that are considered safe in an `eval:' local variable.
+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 "22.1"
+  :type '(repeat sexp))
+
+;; Risky local variables:
+(mapc (lambda (var) (put var 'risky-local-variable t))
+      '(after-load-alist
+       auto-mode-alist
+       buffer-auto-save-file-name
+       buffer-file-name
+       buffer-file-truename
+       buffer-undo-list
+       dabbrev-case-fold-search
+       dabbrev-case-replace
+       debugger
+       default-text-properties
+       display-time-string
+       enable-local-eval
+       enable-local-variables
+       eval
+       exec-directory
+       exec-path
+       file-name-handler-alist
+       font-lock-defaults
+       format-alist
+       frame-title-format
+       global-mode-string
+       header-line-format
+       icon-title-format
+       ignored-local-variables
+       imenu--index-alist
+       imenu-generic-expression
+       inhibit-quit
+       input-method-alist
+       load-path
+       max-lisp-eval-depth
+       max-specpdl-size
+       minor-mode-alist
+       minor-mode-map-alist
+       minor-mode-overriding-map-alist
+       mode-line-buffer-identification
+       mode-line-format
+       mode-line-modes
+       mode-line-modified
+       mode-line-mule-info
+       mode-line-position
+       mode-line-process
+       mode-name
+       outline-level
+       overriding-local-map
+       overriding-terminal-local-map
+       parse-time-rules
+       process-environment
+       rmail-output-file-alist
+       safe-local-variable-values
+       safe-local-eval-forms
+       save-some-buffers-action-alist
+       special-display-buffer-names
+       standard-input
+       standard-output
+       unread-command-events
+       vc-mode))
+
+;; Safe local variables:
+;;
+;; For variables defined by major modes, the safety declarations can go into
+;; the major mode's file, since that will be loaded before file variables are
+;; processed.
+;;
+;; For variables defined by minor modes, put the safety declarations in the
+;; file defining the minor mode after the defcustom/defvar using an autoload
+;; cookie, e.g.:
+;;
+;;   ;;;###autoload(put 'variable 'safe-local-variable 'stringp)
+;;
+;; Otherwise, when Emacs visits a file specifying that local variable, the
+;; minor mode file may not be loaded yet.
+;;
+;; For variables defined in the C source code the declaration should go here:
+
+(mapc (lambda (pair)
+       (put (car pair) 'safe-local-variable (cdr pair)))
+      '((buffer-read-only     . booleanp)   ;; C source code
+       (default-directory    . stringp)    ;; C source code
+       (fill-column          . integerp)   ;; C source code
+       (indent-tabs-mode     . booleanp)   ;; C source code
+       (left-margin          . integerp)   ;; C source code
+       (no-update-autoloads  . booleanp)
+       (tab-width            . integerp)   ;; C source code
+       (truncate-lines       . booleanp))) ;; C source code
+
+(put 'c-set-style 'safe-local-eval-function t)
 
 (defun hack-local-variables-confirm (vars unsafe-vars risky-vars)
   (if noninteractive
@@ -2225,6 +2492,7 @@ Otherwise, return nil; point may be changed."
     (let ((name (if buffer-file-name
                    (file-name-nondirectory buffer-file-name)
                  (concat "buffer " (buffer-name))))
+         (offer-save (and (eq enable-local-variables t) unsafe-vars))
          prompt char)
       (save-window-excursion
        (let ((buf (get-buffer-create "*Local Variables*")))
@@ -2243,9 +2511,12 @@ Otherwise, return nil; point may be changed."
              (insert "A local variables list is specified in " name ".")))
          (insert "\n\nDo you want to apply it?  You can type
 y  -- to apply the local variables list.
-n  -- to ignore the local variables list.
-!  -- to apply the local variables list, and mark these values (*) as
-      safe (in the future, they can be set automatically.)\n\n")
+n  -- to ignore the local variables list.")
+         (if offer-save
+             (insert "
+!  -- to apply the local variables list, and permanently mark these
+      values (*) as safe (in the future, they will be set automatically.)\n\n")
+           (insert "\n\n"))
          (dolist (elt vars)
            (cond ((member elt unsafe-vars)
                   (insert "  * "))
@@ -2255,34 +2526,49 @@ n  -- to ignore the local variables list.
                   (insert "    ")))
            (princ (car elt) buf)
            (insert " : ")
-           (princ (cdr elt) buf)
+            ;; Make strings with embedded whitespace easier to read.
+            (let ((print-escape-newlines t))
+              (prin1 (cdr elt) buf))
            (insert "\n"))
-         (if (< (line-number-at-pos) (window-body-height))
-             (setq prompt "Please type y, n, or !: ")
-           (goto-char (point-min))
-           (setq prompt "Please type y, n, or !, or C-v to scroll: "))
-         (let ((inhibit-quit t)
-               (cursor-in-echo-area t)
+         (setq prompt
+               (format "Please type %s%s: "
+                       (if offer-save "y, n, or !" "y or n")
+                       (if (< (line-number-at-pos) (window-body-height))
+                           ""
+                         ", or C-v to scroll")))
+         (goto-char (point-min))
+         (let ((cursor-in-echo-area t)
+               (executing-kbd-macro executing-kbd-macro)
+               (exit-chars
+                (if offer-save '(?! ?y ?n ?\s ?\C-g) '(?y ?n ?\s ?\C-g)))
                done)
            (while (not done)
              (message prompt)
              (setq char (read-event))
              (if (numberp char)
-                 (if (eq char ?\C-v)
-                     (condition-case nil
-                         (scroll-up)
-                       (error (goto-char (point-min))))
-                   (setq done (memq (downcase char)
-                                    '(?! ?y ?n ?\s ?\C-g))))))
-           (if (= char ?\C-g)
-               (setq quit-flag nil)))
+                 (cond ((eq char ?\C-v)
+                        (condition-case nil
+                            (scroll-up)
+                          (error (goto-char (point-min)))))
+                       ;; read-event returns -1 if we are in a kbd
+                       ;; macro and there are no more events in the
+                       ;; macro.  In that case, attempt to get an
+                       ;; event interactively.
+                       ((and executing-kbd-macro (= char -1))
+                        (setq executing-kbd-macro nil))
+                       (t (setq done (memq (downcase char) exit-chars)))))))
          (setq char (downcase char))
-         (when (and (= char ?!) unsafe-vars)
+         (when (and offer-save (= char ?!) unsafe-vars)
            (dolist (elt unsafe-vars)
              (add-to-list 'safe-local-variable-values elt))
-           (customize-save-variable
-            'safe-local-variable-values
-            safe-local-variable-values))
+           ;; When this is called from desktop-restore-file-buffer,
+           ;; coding-system-for-read may be non-nil.  Reset it before
+           ;; writing to .emacs.
+           (if (or custom-file user-init-file)
+               (let ((coding-system-for-read nil))
+                 (customize-save-variable
+                  'safe-local-variable-values
+                  safe-local-variable-values))))
          (kill-buffer buf)
          (or (= char ?!)
              (= char ?\s)
@@ -2320,9 +2606,7 @@ and VAL is the specified value."
               ;; There used to be a downcase here,
               ;; but the manual didn't say so,
               ;; and people want to set var names that aren't all lc.
-              (let ((key (intern (buffer-substring
-                                  (match-beginning 1)
-                                  (match-end 1))))
+              (let ((key (intern (match-string 1)))
                     (val (save-restriction
                            (narrow-to-region (point) end)
                            (read (current-buffer)))))
@@ -2346,18 +2630,6 @@ and VAL is the specified value."
          mode-specified
        result))))
 
-(defvar hack-local-variables-hook nil
-  "Normal hook run after processing a file's local variables specs.
-Major modes can use this to examine user-specified local variables
-in order to initialize other data structure based on them.")
-
-(defcustom safe-local-variable-values nil
-  "List variable-value pairs that are considered safe.
-Each element is a cons cell (VAR . VAL), where VAR is a variable
-symbol and VAL is a value that is considered safe."
-  :group 'find-file
-  :type  'alist)
-
 (defun hack-local-variables (&optional mode-only)
   "Parse and put into effect this buffer's local variables spec.
 If MODE-ONLY is non-nil, all we do is check whether the major mode
@@ -2470,100 +2742,24 @@ is specified, returning t if it is specified."
                    (and (risky-local-variable-p var val)
                         (push elt risky-vars))
                    (push elt unsafe-vars))))
-           (if (or (and (eq enable-local-variables t)
-                        (null unsafe-vars)
-                        (null risky-vars))
-                   (hack-local-variables-confirm
-                    result unsafe-vars risky-vars))
+           (if (eq enable-local-variables :safe)
+               ;; If caller wants only the safe variables,
+               ;; install only them.
                (dolist (elt result)
-                 (hack-one-local-variable (car elt) (cdr elt)))))
-         (run-hooks 'hack-local-variables-hook))))))
-
-(defvar ignored-local-variables
-  '(ignored-local-variables safe-local-variable-values)
-  "Variables to be ignored in a file's local variable spec.")
-
-;; Get confirmation before setting these variables as locals in a file.
-(put 'debugger 'risky-local-variable t)
-(put 'enable-local-eval 'risky-local-variable t)
-(put 'ignored-local-variables 'risky-local-variable t)
-(put 'ignored-local-variables 'safe-local-variable-values t)
-(put 'eval 'risky-local-variable t)
-(put 'file-name-handler-alist 'risky-local-variable t)
-(put 'inhibit-quit 'risky-local-variable t)
-(put 'minor-mode-alist 'risky-local-variable t)
-(put 'minor-mode-map-alist 'risky-local-variable t)
-(put 'minor-mode-overriding-map-alist 'risky-local-variable t)
-(put 'overriding-local-map 'risky-local-variable t)
-(put 'overriding-terminal-local-map 'risky-local-variable t)
-(put 'auto-mode-alist 'risky-local-variable t)
-(put 'after-load-alist 'risky-local-variable t)
-(put 'buffer-file-name 'risky-local-variable t)
-(put 'buffer-undo-list 'risky-local-variable t)
-(put 'buffer-auto-save-file-name 'risky-local-variable t)
-(put 'buffer-file-truename 'risky-local-variable t)
-(put 'default-text-properties 'risky-local-variable t)
-(put 'exec-path 'risky-local-variable t)
-(put 'load-path 'risky-local-variable t)
-(put 'exec-directory 'risky-local-variable t)
-(put 'process-environment 'risky-local-variable t)
-(put 'dabbrev-case-fold-search 'risky-local-variable t)
-(put 'dabbrev-case-replace 'risky-local-variable t)
-;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
-(put 'outline-level 'risky-local-variable t)
-(put 'rmail-output-file-alist 'risky-local-variable t)
-(put 'font-lock-defaults 'risky-local-variable t)
-(put 'special-display-buffer-names 'risky-local-variable t)
-(put 'frame-title-format 'risky-local-variable t)
-(put 'global-mode-string 'risky-local-variable t)
-(put 'header-line-format 'risky-local-variable t)
-(put 'icon-title-format 'risky-local-variable t)
-(put 'input-method-alist 'risky-local-variable t)
-(put 'format-alist 'risky-local-variable t)
-(put 'vc-mode 'risky-local-variable t)
-(put 'imenu-generic-expression 'risky-local-variable t)
-(put 'imenu--index-alist 'risky-local-variable t)
-(put 'standard-input 'risky-local-variable t)
-(put 'standard-output 'risky-local-variable t)
-(put 'unread-command-events 'risky-local-variable t)
-(put 'max-lisp-eval-depth 'risky-local-variable t)
-(put 'max-specpdl-size 'risky-local-variable t)
-(put 'mode-line-format 'risky-local-variable t)
-(put 'mode-line-modified 'risky-local-variable t)
-(put 'mode-line-mule-info 'risky-local-variable t)
-(put 'mode-line-buffer-identification 'risky-local-variable t)
-(put 'mode-line-modes 'risky-local-variable t)
-(put 'mode-line-position 'risky-local-variable t)
-(put 'mode-line-process 'risky-local-variable t)
-(put 'mode-name 'risky-local-variable t)
-(put 'display-time-string 'risky-local-variable t)
-(put 'parse-time-rules 'risky-local-variable t)
-
-;; Commonly-encountered local variables that are safe:
-(let ((string-or-null (lambda (a) (or (stringp a) (null a)))))
-  (eval
-   `(mapc (lambda (pair)
-           (put (car pair) 'safe-local-variable (cdr pair)))
-         '((byte-compile-dynamic . t)
-           (c-basic-offset     .  integerp)
-           (c-file-style       .  stringp)
-           (c-indent-level     .  integerp)
-           (comment-column     .  integerp)
-           (compile-command    . ,string-or-null)
-           (fill-column        .  integerp)
-           (fill-prefix        . ,string-or-null)
-           (indent-tabs-mode   .  t)
-           (kept-new-versions  .  integerp)
-           (no-byte-compile    .  t)
-           (no-update-autoloads . t)
-           (outline-regexp     . ,string-or-null)
-           (page-delimiter     . ,string-or-null)
-           (paragraph-start    . ,string-or-null)
-           (paragraph-separate . ,string-or-null)
-           (sentence-end       . ,string-or-null)
-           (sentence-end-double-space . t)
-           (tab-width          .  integerp)
-           (version-control    .  t)))))
+                 (unless (or (memq (car elt) unsafe-vars)
+                             (memq (car elt) risky-vars))
+                   (hack-one-local-variable (car elt) (cdr elt))))
+             ;; Query, except in the case where all are known safe
+             ;; if the user wants no quuery in that case.
+             (if (or (and (eq enable-local-variables t)
+                          (null unsafe-vars)
+                          (null risky-vars))
+                     (eq enable-local-variables :all)
+                     (hack-local-variables-confirm
+                      result unsafe-vars risky-vars))
+                 (dolist (elt result)
+                   (hack-one-local-variable (car elt) (cdr elt)))))))
+       (run-hooks 'hack-local-variables-hook)))))
 
 (defun safe-local-variable-p (sym val)
   "Non-nil if SYM is safe as a file-local variable with value VAL.
@@ -2572,15 +2768,11 @@ It is safe if any of these conditions are met:
  * There is a matching entry (SYM . VAL) in the
    `safe-local-variable-values' user option.
 
- * The `safe-local-variable' property of SYM is t.
-
  * The `safe-local-variable' property of SYM is a function that
    evaluates to a non-nil value with VAL as an argument."
   (or (member (cons sym val) safe-local-variable-values)
       (let ((safep (get sym 'safe-local-variable)))
-       (or (eq safep t)
-           (and (functionp safep)
-                (funcall safep val))))))
+        (and (functionp safep) (funcall safep val)))))
 
 (defun risky-local-variable-p (sym &optional ignored)
   "Non-nil if SYM could be dangerous as a file-local variable.
@@ -2590,8 +2782,8 @@ It is dangerous if either of these conditions are met:
 
  * Its name ends with \"hook(s)\", \"function(s)\", \"form(s)\", \"map\",
    \"program\", \"command(s)\", \"predicate(s)\", \"frame-alist\",
-   \"mode-alist\", \"font-lock-(syntactic-)keyword*\", or
-   \"map-alist\"."
+   \"mode-alist\", \"font-lock-(syntactic-)keyword*\",
+   \"map-alist\", or \"bindat-spec\"."
   ;; If this is an alias, check the base name.
   (condition-case nil
       (setq sym (indirect-variable sym))
@@ -2600,18 +2792,7 @@ It is dangerous if either of these conditions are met:
       (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))))
-
-(defcustom safe-local-eval-forms nil
-  "*Expressions that are considered \"safe\" in an `eval:' local variable.
-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 "22.1"
-  :type '(repeat sexp))
-
-(put 'c-set-style 'safe-local-eval-function t)
+-map$\\|-map-alist$\\|-bindat-spec$" (symbol-name sym))))
 
 (defun hack-one-local-variable-quotep (exp)
   (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp))))
@@ -2629,12 +2810,18 @@ asking you for confirmation."
       (and (eq (car exp) 'put)
           (hack-one-local-variable-quotep (nth 1 exp))
           (hack-one-local-variable-quotep (nth 2 exp))
-          (memq (nth 1 (nth 2 exp))
-                '(lisp-indent-hook))
-          ;; Only allow safe values of lisp-indent-hook;
-          ;; not functions.
-          (or (numberp (nth 3 exp))
-              (equal (nth 3 exp) ''defun)))
+          (let ((prop (nth 1 (nth 2 exp))) (val (nth 3 exp)))
+            (cond ((eq prop 'lisp-indent-hook)
+                   ;; Only allow safe values of lisp-indent-hook;
+                   ;; not functions.
+                   (or (numberp val) (equal val ''defun)))
+                  ((eq prop 'edebug-form-spec)
+                   ;; Only allow indirect form specs.
+                   ;; During bootstrapping, edebug-basic-spec might not be
+                   ;; defined yet.
+                    (and (fboundp 'edebug-basic-spec)
+                        (hack-one-local-variable-quotep val)
+                         (edebug-basic-spec (nth 1 val)))))))
       ;; Allow expressions that the user requested.
       (member exp safe-local-eval-forms)
       ;; Certain functions can be allowed with safe arguments
@@ -2657,27 +2844,32 @@ asking you for confirmation."
                      ok)))))))
 
 (defun hack-one-local-variable (var val)
-  "Set local variable VAR with value VAL."
+  "Set local variable VAR with value VAL.
+If VAR is `mode', call `VAL-mode' as a function unless it's
+already the major mode."
   (cond ((eq var 'mode)
-        (funcall (intern (concat (downcase (symbol-name val))
-                                 "-mode"))))
+        (let ((mode (intern (concat (downcase (symbol-name val))
+                                    "-mode"))))
+          (unless (eq (indirect-function mode)
+                      (indirect-function major-mode))
+            (funcall mode))))
        ((eq var 'eval)
         (save-excursion (eval val)))
-       (t (make-local-variable var)
-          ;; Make sure the string has no text properties.
-          ;; Some text properties can get evaluated in various ways,
-          ;; so it is risky to put them on with a local variable list.
-          (if (stringp val)
-              (set-text-properties 0 (length val) nil val))
-          (set var val))))
+       (t
+         ;; Make sure the string has no text properties.
+         ;; Some text properties can get evaluated in various ways,
+         ;; so it is risky to put them on with a local variable list.
+         (if (stringp val)
+             (set-text-properties 0 (length val) nil val))
+         (set (make-local-variable var) val))))
 
 \f
 (defcustom change-major-mode-with-file-name t
-  "*Non-nil means \\[write-file] should set the major mode from the file name.
+  "Non-nil means \\[write-file] should set the major mode from the file name.
 However, the mode will not be changed if
 \(1) a local variables list or the `-*-' line specifies a major mode, or
 \(2) the current major mode is a \"special\" mode,
-\    not suitable for ordinary files, or
+\     not suitable for ordinary files, or
 \(3) the new file name does not particularly specify any mode."
   :type 'boolean
   :group 'editing-basics)
@@ -2716,8 +2908,8 @@ the old visited file has been renamed to the new name FILENAME."
     (let ((buffer (and filename (find-buffer-visiting filename))))
       (and buffer (not (eq buffer (current-buffer)))
           (not no-query)
-          (not (y-or-n-p (message "A buffer is visiting %s; proceed? "
-                                  filename)))
+          (not (y-or-n-p (format "A buffer is visiting %s; proceed? "
+                                  filename)))
           (error "Aborted")))
     (or (equal filename buffer-file-name)
        (progn
@@ -2761,6 +2953,7 @@ the old visited file has been renamed to the new name FILENAME."
   ;; Turn off backup files for certain file names.
   ;; Since this is a permanent local, the major mode won't eliminate it.
   (and buffer-file-name
+       backup-enable-predicate
        (not (funcall backup-enable-predicate buffer-file-name))
        (progn
         (make-local-variable 'backup-inhibited)
@@ -2812,7 +3005,7 @@ Interactively, confirmation is required unless you supply a prefix argument."
   (interactive
    (list (if buffer-file-name
             (read-file-name "Write file: "
-                                nil nil nil nil)
+                            nil nil nil nil)
           (read-file-name "Write file: " default-directory
                           (expand-file-name
                            (file-name-nondirectory (buffer-name))
@@ -2912,7 +3105,7 @@ BACKUPNAME is the backup file name, which is the old file renamed."
                                       (convert-standard-filename
                                        "~/%backup%~")))
                     (message "Cannot write backup file; backing up in %s"
-                             (file-name-nondirectory backupname))
+                             backupname)
                     (sleep-for 1)
                     (backup-buffer-copy real-file-name backupname modes)))
                  (setq buffer-backed-up t)
@@ -2939,7 +3132,7 @@ BACKUPNAME is the backup file name, which is the old file renamed."
                       (condition-case nil
                           (delete-file to-name)
                         (file-error nil))
-                      (copy-file from-name to-name t t 'excl)
+                      (copy-file from-name to-name nil t)
                       nil)
                   (file-already-exists t))
            ;; The file was somehow created by someone else between
@@ -3007,6 +3200,7 @@ except that a leading `.', if any, doesn't count."
 (defun file-name-extension (filename &optional period)
   "Return FILENAME's final \"extension\".
 The extension, in a file name, is the part that follows the last `.',
+excluding version numbers and backup suffixes,
 except that a leading `.', if any, doesn't count.
 Return nil for extensionless file names such as `foo'.
 Return the empty string for file names such as `foo.'.
@@ -3107,16 +3301,24 @@ doesn't exist, it is created."
 (defun make-backup-file-name-1 (file)
   "Subroutine of `make-backup-file-name' and `find-backup-file-name'."
   (let ((alist backup-directory-alist)
-       elt backup-directory)
+       elt backup-directory abs-backup-directory)
     (while alist
       (setq elt (pop alist))
       (if (string-match (car elt) file)
          (setq backup-directory (cdr elt)
                alist nil)))
-    (if (and backup-directory (not (file-exists-p backup-directory)))
+    ;; If backup-directory is relative, it should be relative to the
+    ;; file's directory.  By expanding explicitly here, we avoid
+    ;; depending on default-directory.
+    (if backup-directory
+       (setq abs-backup-directory
+             (expand-file-name backup-directory
+                               (file-name-directory file))))
+    (if (and abs-backup-directory (not (file-exists-p abs-backup-directory)))
        (condition-case nil
-           (make-directory backup-directory 'parents)
-         (file-error (setq backup-directory nil))))
+           (make-directory abs-backup-directory 'parents)
+         (file-error (setq backup-directory nil
+                           abs-backup-directory nil))))
     (if (null backup-directory)
        file
       (if (file-name-absolute-p backup-directory)
@@ -3147,9 +3349,7 @@ doesn't exist, it is created."
              (replace-regexp-in-string "!" "!!" file))
             backup-directory))
        (expand-file-name (file-name-nondirectory file)
-                         (file-name-as-directory
-                          (expand-file-name backup-directory
-                                            (file-name-directory file))))))))
+                         (file-name-as-directory abs-backup-directory))))))
 
 (defun backup-file-name-p (file)
   "Return non-nil if FILE is a backup file name (numeric or not).
@@ -3436,10 +3636,21 @@ Before and after saving the buffer, this function runs
              (let ((filename
                     (expand-file-name
                      (read-file-name "File to save in: ") nil)))
-               (and (file-exists-p filename)
-                    (or (y-or-n-p (format "File `%s' exists; overwrite? "
-                                          filename))
-                        (error "Canceled")))
+               (if (file-exists-p filename)
+                   (if (file-directory-p filename)
+                       ;; Signal an error if the user specified the name of an
+                       ;; existing directory.
+                       (error "%s is a directory" filename)
+                     (unless (y-or-n-p (format "File `%s' exists; overwrite? "
+                                               filename))
+                       (error "Canceled")))
+                 ;; Signal an error if the specified name refers to a
+                 ;; non-existing directory.
+                 (let ((dir (file-name-directory filename)))
+                   (unless (file-directory-p dir)
+                     (if (file-exists-p dir)
+                         (error "%s is not a directory" dir)
+                       (error "%s: no such directory" dir)))))
                (set-visited-file-name filename)))
          (or (verify-visited-file-modtime (current-buffer))
              (not (file-exists-p buffer-file-name))
@@ -3513,7 +3724,7 @@ Before and after saving the buffer, this function runs
          (if (not (file-directory-p dir))
              (if (file-exists-p dir)
                  (error "%s is not a directory" dir)
-               (error "%s: no such directory" buffer-file-name))
+               (error "%s: no such directory" dir))
            (if (not (file-exists-p buffer-file-name))
                (error "Directory %s write-protected" dir)
              (if (yes-or-no-p
@@ -3566,8 +3777,10 @@ Before and after saving the buffer, this function runs
                (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)))
+           (setq setmodes (or setmodes
+                              (cons (or (file-modes buffer-file-name)
+                                        (logand ?\666 umask))
+                                    buffer-file-name)))
            ;; We succeeded in writing the temp file,
            ;; so rename it.
            (rename-file tempname buffer-file-name t))
@@ -3626,11 +3839,16 @@ This requires the external program `diff' to be in your `exec-path'."
        (recursive-edit)
        ;; Return nil to ask about BUF again.
        nil)
-     "view this file")
-    (?d diff-buffer-with-file
-       "view changes in file"))
+     "view this buffer")
+    (?d (lambda (buf)
+         (save-window-excursion
+           (diff-buffer-with-file buf))
+         (view-buffer (get-buffer-create "*Diff*")
+                      (lambda (ignore) (exit-recursive-edit)))
+         (recursive-edit)
+         nil)
+       "view changes in this buffer"))
   "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.")
@@ -3723,8 +3941,9 @@ prints a message in the minibuffer.  Instead, use `set-buffer-modified-p'."
 
 (defun toggle-read-only (&optional arg)
   "Change whether this buffer is visiting its file read-only.
-With arg, set read-only iff arg is positive.
-If visiting file read-only and `view-read-only' is non-nil, enter view mode."
+With prefix argument ARG, make the buffer read-only if ARG is
+positive, otherwise make it writable.  If visiting file read-only
+and `view-read-only' is non-nil, enter view mode."
   (interactive "P")
   (if (and arg
            (if (> (prefix-numeric-value arg) 0) buffer-read-only
@@ -3865,7 +4084,7 @@ user.  In such situations, one has to be careful with potentially
 time consuming operations.
 
 For more information on how this variable is used by Auto Revert mode,
-see Info node `(emacs-xtra)Supporting additional buffers'.")
+see Info node `(emacs)Supporting additional buffers'.")
 
 (defvar before-revert-hook nil
   "Normal hook for `revert-buffer' to run before reverting.
@@ -3969,6 +4188,11 @@ non-nil, it is called instead of rereading visited file contents."
                              "File %s no longer exists!"
                            "Cannot revert nonexistent file %s")
                          file-name))
+                 ((not (file-readable-p file-name))
+                  (error (if buffer-file-number
+                             "File %s no longer readable!"
+                           "Cannot revert unreadable file %s")
+                         file-name))
                  (t
                   ;; Bind buffer-file-name to nil
                   ;; so that we don't try to lock the file.
@@ -3982,6 +4206,16 @@ non-nil, it is called instead of rereading visited file contents."
                          (if auto-save-p 'auto-save-coding
                            (or coding-system-for-read
                                buffer-file-coding-system-explicit))))
+                    (if (and (not enable-multibyte-characters)
+                             coding-system-for-read
+                             (not (memq (coding-system-base
+                                         coding-system-for-read)
+                                        '(no-conversion raw-text))))
+                        ;; As a coding system suitable for multibyte
+                        ;; buffer is specified, make the current
+                        ;; buffer multibyte.
+                        (set-buffer-multibyte t))
+
                     ;; This force after-insert-file-set-coding
                     ;; (called from insert-file-contents) to set
                     ;; buffer-file-coding-system to a proper value.
@@ -4050,7 +4284,7 @@ non-nil, it is called instead of rereading visited file contents."
           (let ((inhibit-read-only t)
                 ;; Keep the current buffer-file-coding-system.
                 (coding-system buffer-file-coding-system)
-                ;; Auto-saved file shoule be read with special coding.
+                ;; Auto-saved file should be read with special coding.
                 (coding-system-for-read 'auto-save-coding))
             (erase-buffer)
             (insert-file-contents file-name nil)
@@ -4125,9 +4359,7 @@ This command is used in the special Dired buffer created by
                      (setq autofile
                            (buffer-substring-no-properties
                             (point)
-                            (save-excursion
-                              (end-of-line)
-                              (point))))
+                            (line-end-position)))
                      (setq thisfile
                            (expand-file-name
                             (substring
@@ -4277,7 +4509,7 @@ See also `auto-save-file-name-p'."
                            "#")))
            ;; Make sure auto-save file names don't contain characters
            ;; invalid for the underlying filesystem.
-           (if (and (memq system-type '(ms-dos windows-nt))
+           (if (and (memq system-type '(ms-dos windows-nt cygwin))
                     ;; Don't modify remote (ange-ftp) filenames
                     (not (string-match "^/\\w+@[-A-Za-z0-9._]+:" result)))
                (convert-standard-filename result)
@@ -4312,7 +4544,7 @@ See also `auto-save-file-name-p'."
                      ((file-writable-p default-directory) default-directory)
                      ((file-writable-p "/var/tmp/") "/var/tmp/")
                      ("~/")))))
-              (if (and (memq system-type '(ms-dos windows-nt))
+              (if (and (memq system-type '(ms-dos windows-nt cygwin))
                        ;; Don't modify remote (ange-ftp) filenames
                        (not (string-match "^/\\w+@[-A-Za-z0-9._]+:" fname)))
                   ;; The call to convert-standard-filename is in case
@@ -4337,7 +4569,7 @@ FILENAME should lack slashes.  You can redefine this for customization."
 \f
 (defun wildcard-to-regexp (wildcard)
   "Given a shell file name pattern WILDCARD, return an equivalent regexp.
-The generated regexp will match a filename iff the filename
+The generated regexp will match a filename only if the filename
 matches that wildcard according to shell rules.  Only wildcards known
 by `sh' are supported."
   (let* ((i (string-match "[[.*+\\^$?]" wildcard))
@@ -4408,7 +4640,7 @@ by `sh' are supported."
 \f
 (defcustom list-directory-brief-switches
   (if (eq system-type 'vax-vms) "" "-CF")
-  "*Switches for `list-directory' to pass to `ls' for brief listing."
+  "Switches for `list-directory' to pass to `ls' for brief listing."
   :type 'string
   :group 'dired)
 
@@ -4416,7 +4648,7 @@ by `sh' are supported."
   (if (eq system-type 'vax-vms)
       "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
     "-l")
-  "*Switches for `list-directory' to pass to `ls' for verbose listing."
+  "Switches for `list-directory' to pass to `ls' for verbose listing."
   :type 'string
   :group 'dired)
 
@@ -4547,7 +4779,7 @@ PATTERN that already quotes some of the special characters."
   "Absolute or relative name of the `ls' program used by `insert-directory'.")
 
 (defcustom directory-free-space-program "df"
-  "*Program to get the amount of free space on a file system.
+  "Program to get the amount of free space on a file system.
 We assume the output has the format of `df'.
 The value of this variable must be just a command name or file name;
 if you want to specify options, use `directory-free-space-args'.
@@ -4561,7 +4793,7 @@ preference to the program given by this variable."
 
 (defcustom directory-free-space-args
   (if (eq system-type 'darwin) "-k" "-Pk")
-  "*Options to use when running `directory-free-space-program'."
+  "Options to use when running `directory-free-space-program'."
   :type 'string
   :group 'dired)
 
@@ -4569,36 +4801,37 @@ preference to the program given by this variable."
   "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 information
-fail.
+fail.  It returns also nil when DIR is a remote directory.
 
 This function calls `file-system-info' if it is available, or invokes the
 program specified by `directory-free-space-program' if that is non-nil."
-  ;; Try to find the number of free blocks.  Non-Posix systems don't
-  ;; always have df, but might have an equivalent system call.
-  (if (fboundp 'file-system-info)
-      (let ((fsinfo (file-system-info dir)))
-       (if fsinfo
-           (format "%.0f" (/ (nth 2 fsinfo) 1024))))
-    (save-match-data
-      (with-temp-buffer
-       (when (and directory-free-space-program
-                  (eq 0 (call-process directory-free-space-program
-                                      nil t nil
-                                      directory-free-space-args
-                                      dir)))
-         ;; Usual format is a header line followed by a line of
-         ;; numbers.
-         (goto-char (point-min))
-         (forward-line 1)
-         (if (not (eobp))
-             (progn
-               ;; Move to the end of the "available blocks" number.
-               (skip-chars-forward "^ \t")
-               (forward-word 3)
-               ;; Copy it into AVAILABLE.
-               (let ((end (point)))
-                 (forward-word -1)
-                 (buffer-substring (point) end)))))))))
+  (when (not (file-remote-p dir))
+    ;; Try to find the number of free blocks.  Non-Posix systems don't
+    ;; always have df, but might have an equivalent system call.
+    (if (fboundp 'file-system-info)
+       (let ((fsinfo (file-system-info dir)))
+         (if fsinfo
+             (format "%.0f" (/ (nth 2 fsinfo) 1024))))
+      (save-match-data
+       (with-temp-buffer
+         (when (and directory-free-space-program
+                    (eq 0 (call-process directory-free-space-program
+                                        nil t nil
+                                        directory-free-space-args
+                                        dir)))
+           ;; Usual format is a header line followed by a line of
+           ;; numbers.
+           (goto-char (point-min))
+           (forward-line 1)
+           (if (not (eobp))
+               (progn
+                 ;; Move to the end of the "available blocks" number.
+                 (skip-chars-forward "^ \t")
+                 (forward-word 3)
+                 ;; Copy it into AVAILABLE.
+                 (let ((end (point)))
+                   (forward-word -1)
+                   (buffer-substring (point) end))))))))))
 
 ;; The following expression replaces `dired-move-to-filename-regexp'.
 (defvar directory-listing-before-filename-regexp
@@ -5107,6 +5340,7 @@ With prefix arg, silently save all file-visiting buffers, then kill."
 (define-key ctl-x-5-map "f" 'find-file-other-frame)
 (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)
+(define-key ctl-x-5-map "\C-o" 'display-buffer-other-frame)
 
 ;; arch-tag: bc68d3ea-19ca-468b-aac6-3a4a7766101f
 ;;; files.el ends here