]> code.delx.au - gnu-emacs/blobdiff - lisp/files.el
New command kill-matching-buffers kills buffers whose name matches a regexp.
[gnu-emacs] / lisp / files.el
index 13fdf0d020baf74d33d5236be5480bbcaf67917d..fc8256ba36e9f27fbf21a3c4b2b027e7f1e12ef6 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
 
@@ -242,9 +242,9 @@ breaks any hard links between it and other files."
 
 (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."
+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))
@@ -287,7 +287,7 @@ 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'."
@@ -309,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)
@@ -459,7 +459,7 @@ not safe, Emacs queries you, once, whether to set them all.
 :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.)
-nil means always ignore the file local variables.
+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
@@ -491,7 +491,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."
+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))
@@ -514,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.
@@ -1117,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.
@@ -1218,11 +1223,11 @@ killed."
   (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)
@@ -1284,8 +1289,9 @@ 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.
   (save-match-data
     (if (and automount-dir-prefix
@@ -1468,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))
@@ -1509,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
@@ -1522,13 +1530,14 @@ 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
-                          (format 
+                          (format
                            (if rawfile
                                "The file %s is already visited normally,
 and you have edited the buffer.  Now you have asked to visit it literally,
@@ -1548,7 +1557,7 @@ Do you want to save the file, and visit it normally instead? ")
                            (find-file-noselect-1 buf filename nowarn
                                                  rawfile truename number))
                        (if (y-or-n-p
-                            (format 
+                            (format
                              (if rawfile
                                  "\
 Do you want to discard your changes, and visit the file literally now? "
@@ -1558,8 +1567,8 @@ Do you want to discard your changes, and visit the file normally now? ")))
                                                  rawfile truename number)
                          (error (if rawfile "File already visited non-literally"
                                   "File already visited literally"))))
-                   (if (y-or-n-p 
-                        (format 
+                   (if (y-or-n-p
+                        (format
                          (if rawfile
                              "The file %s is already visited normally.
 You have asked to visit it literally,
@@ -1645,7 +1654,8 @@ Do you want to revisit the file normally now? ")
       (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)))
@@ -1861,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
@@ -1904,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-9a-z]+\\'" . change-log-mode)
      ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
      ("\\.scm\\.[0-9]*\\'" . scheme-mode)
      ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode)
@@ -1965,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)
@@ -2099,44 +2121,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'.
@@ -2145,7 +2184,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
@@ -2183,10 +2223,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)
@@ -2199,7 +2239,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))
@@ -2209,26 +2249,58 @@ 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))
+           ;; 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
@@ -2239,14 +2311,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.
@@ -2395,15 +2465,16 @@ asking you for confirmation."
 ;;
 ;; For variables defined in the C source code the declaration should go here:
 
-;; FIXME: Some variables should be moved according to the rules above.
 (mapc (lambda (pair)
        (put (car pair) 'safe-local-variable (cdr pair)))
-      '((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
+      '((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)
 
@@ -2459,6 +2530,7 @@ n  -- to ignore the local variables list.")
                          ", 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)
@@ -2466,11 +2538,17 @@ n  -- to ignore the local variables list.")
              (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) exit-chars))))))
+                 (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 offer-save (= char ?!) unsafe-vars)
            (dolist (elt unsafe-vars)
@@ -2734,7 +2812,8 @@ It is dangerous if either of these conditions are met:
                    ;; During bootstrapping, edebug-basic-spec might not be
                    ;; defined yet.
                     (and (fboundp 'edebug-basic-spec)
-                         (edebug-basic-spec val))))))
+                        (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
@@ -2757,9 +2836,15 @@ It is dangerous if either of these conditions are met:
                      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
@@ -2776,7 +2861,7 @@ It is dangerous if either of these conditions are met:
 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)
@@ -2815,8 +2900,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
@@ -2860,6 +2945,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)
@@ -2911,7 +2997,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))
@@ -3011,7 +3097,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)
@@ -3207,16 +3293,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)
@@ -3247,9 +3341,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).
@@ -3536,10 +3628,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))
@@ -3613,7 +3716,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
@@ -3728,9 +3831,15 @@ 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'.")
 
 (defvar buffer-save-without-query nil
@@ -4070,6 +4179,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.
@@ -4083,6 +4197,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.
@@ -4265,6 +4389,14 @@ This command is used in the special Dired buffer created by
            (message "No files can be recovered from this session now")))
       (kill-buffer buffer))))
 
+(defun kill-buffer-ask (buffer)
+  "Kill buffer if confirmed."
+  (when (yes-or-no-p
+         (format "Buffer %s %s.  Kill? " (buffer-name buffer)
+                 (if (buffer-modified-p buffer)
+                     "HAS BEEN EDITED" "is unmodified")))
+    (kill-buffer buffer)))
+
 (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
@@ -4279,13 +4411,20 @@ specifies the list of buffers to kill, asking for approval for each one."
                                        ; if we killed the base buffer.
           (not (string-equal name ""))
           (/= (aref name 0) ?\s)
-          (yes-or-no-p
-           (format "Buffer %s %s.  Kill? "
-                   name
-                   (if (buffer-modified-p buffer)
-                       "HAS BEEN EDITED" "is unmodified")))
-          (kill-buffer buffer)))
+          (kill-buffer-ask buffer)))
     (setq list (cdr list))))
+
+(defun kill-matching-buffers (regexp &optional internal-too)
+  "Kill buffers whose name matches the specified regexp.
+The optional second argument indicates whether to kill internal buffers too."
+  (interactive "sKill buffers matching this regular expression: \nP")
+  (dolist (buffer (buffer-list))
+    (let ((name (buffer-name buffer)))
+      (when (and name (not (string-equal name ""))
+                 (or internal-too (/= (aref name 0) ?\s))
+                 (string-match regexp name))
+        (kill-buffer-ask buffer)))))
+
 \f
 (defun auto-save-mode (arg)
   "Toggle auto-saving of contents of current buffer.
@@ -4668,36 +4807,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