Merged in changes from CVS trunk.
[bpt/emacs.git] / lisp / files.el
index a63aea9..0c7a6ff 100644 (file)
@@ -1,6 +1,6 @@
 ;;; files.el --- file input and output commands for Emacs
 
-;; Copyright (C) 1985,86,87,92,93,94,95,96,97,98,99,2000,01,02,2003
+;; Copyright (C) 1985,86,87,92,93,94,95,96,97,98,99,2000,01,02,03,2004
 ;;;   Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
@@ -293,7 +293,7 @@ Normally auto-save files are written under other names."
   :group 'auto-save)
 
 (defcustom auto-save-file-name-transforms
-  `(("\\`/[^/]*:\\(.+/\\)*\\(.*\\)"
+  `(("\\`/[^/]*:\\([^/]*/\\)*\\([^/]*\\)\\'"
      ;; Don't put "\\2" inside expand-file-name, since it will be
      ;; transformed to "/2" on DOS/Windows.
      ,(concat temporary-file-directory "\\2") t))
@@ -393,7 +393,8 @@ updates before the buffer is saved, use `before-save-hook' .")
 (defvar write-contents-functions nil
   "List of functions to be called before writing out a buffer to a file.
 If one of them returns non-nil, the file is considered already written
-and the rest are not called.
+and the rest are not called and neither are the functions in
+`write-file-functions'.
 
 This variable is meant to be used for hooks that pertain to the
 buffer's contents, not to the particular visited file; thus,
@@ -438,10 +439,7 @@ specified in a -*- line.")
   "*Control processing of the \"variable\" `eval' in a file's local variables.
 The value can be t, nil or something else.
 A value of t means obey `eval' variables;
-nil means ignore them; anything else means query.
-
-The command \\[normal-mode] always obeys local-variables lists
-and ignores this variable."
+nil means ignore them; anything else means query."
   :type '(choice (const :tag "Obey" t)
                 (const :tag "Ignore" nil)
                 (other :tag "Query" other))
@@ -480,11 +478,18 @@ Runs the usual ange-ftp hook, but only for completion operations."
       (apply op args))))
 
 (defun convert-standard-filename (filename)
-  "Convert a standard file's name to something suitable for the current OS.
-This function's standard definition is trivial; it just returns the argument.
-However, on some systems, the function is redefined with a definition
-that really does change some file names to canonicalize certain
-patterns and to guarantee valid names."
+  "Convert a standard file's name to something suitable for the OS.
+This means to guarantee valid names and perhaps to canonicalize
+certain patterns.
+
+This function's standard definition is trivial; it just returns
+the argument.  However, on Windows and DOS, replace invalid
+characters.  On DOS, make sure to obey the 8.3 limitations.  On
+Windows, turn Cygwin names into native names, and also turn
+slashes into backslashes if the shell requires it (see
+`w32-shell-dos-semantics').
+
+See Info node `(elisp)Standard File Names' for more details."
   filename)
 
 (defun read-directory-name (prompt &optional dir default-dirname mustmatch initial)
@@ -521,8 +526,9 @@ the value of `default-directory'."
 Not actually set up until the first time you use it.")
 
 (defun parse-colon-path (cd-path)
-  "Explode a colon-separated search path into a list of directory names.
-\(For values of `colon' equal to `path-separator'.)"
+  "Explode a search path into a list of directory names.
+Directories are separated by occurrences of `path-separator'
+\(which is colon in GNU and GNU-like systems)."
   ;; We could use split-string here.
   (and cd-path
        (let (cd-list (cd-start 0) cd-colon)
@@ -555,8 +561,10 @@ Not actually set up until the first time you use it.")
 
 (defun cd (dir)
   "Make DIR become the current buffer's default directory.
-If your environment includes a `CDPATH' variable, try each one of that
-colon-separated list of directories when resolving a relative directory name."
+If your environment includes a `CDPATH' variable, try each one of
+that list of directories (separated by occurrences of
+`path-separator') when resolving a relative directory name.
+The path separator is colon in GNU and GNU-like systems."
   (interactive
    (list (read-directory-name "Change default directory: "
                         default-directory default-directory
@@ -616,6 +624,8 @@ PATH-AND-SUFFIXES is a pair of lists (DIRECTORIES . SUFFIXES)."
          (suffix (concat (regexp-opt (cdr path-and-suffixes) t) "\\'"))
          (string-dir (file-name-directory string)))
       (dolist (dir (car path-and-suffixes))
+       (unless dir
+         (setq dir default-directory))
        (if string-dir (setq dir (expand-file-name string-dir dir)))
        (when (file-directory-p dir)
          (dolist (file (file-name-all-completions
@@ -639,10 +649,17 @@ This is an interface to the function `load'."
   (load library))
 
 (defun file-remote-p (file)
-  "Test whether FILE specifies a location on a remote system."
-  (let ((handler (find-file-name-handler file 'file-local-copy)))
+  "Test whether FILE specifies a location on a remote system.
+Return an identification of the system if the location is indeed
+remote.  The identification of the system may comprise a method
+to access the system and its hostname, amongst other things.
+
+For example, the filename \"/user@host:/foo\" specifies a location
+on the system \"/user@host:\"."
+  (let ((handler (find-file-name-handler file 'file-remote-p)))
     (if handler
-       (get handler 'file-remote-p))))
+       (funcall handler 'file-remote-p file)
+      nil)))
 
 (defun file-local-copy (file)
   "Copy the file FILE into a temporary file on this machine.
@@ -661,21 +678,23 @@ The truename of a file name is found by chasing symbolic links
 both at the level of the file and at the level of the directories
 containing it, until no links are left at any level.
 
-The arguments COUNTER and PREV-DIRS are used only in recursive calls.
-Do not specify them in other calls."
-  ;; COUNTER can be a cons cell whose car is the count of how many more links
-  ;; to chase before getting an error.
+\(fn FILENAME)"  ;; Don't document the optional arguments.
+  ;; COUNTER and PREV-DIRS are only used in recursive calls.
+  ;; COUNTER can be a cons cell whose car is the count of how many
+  ;; more links to chase before getting an error.
   ;; PREV-DIRS can be a cons cell whose car is an alist
   ;; of truenames we've just recently computed.
+  (cond ((or (string= filename "") (string= filename "~"))
+        (setq filename (expand-file-name filename))
+        (if (string= filename "")
+            (setq filename "/")))
+       ((and (string= (substring filename 0 1) "~")
+             (string-match "~[^/]*/?" filename))
+        (let ((first-part
+               (substring filename 0 (match-end 0)))
+              (rest (substring filename (match-end 0))))
+          (setq filename (concat (expand-file-name first-part) rest)))))
 
-  ;; The last test looks dubious, maybe `+' is meant here?  --simon.
-  (if (or (string= filename "") (string= filename "~")
-         (and (string= (substring filename 0 1) "~")
-              (string-match "~[^/]*" filename)))
-      (progn
-       (setq filename (expand-file-name filename))
-       (if (string= filename "")
-           (setq filename "/"))))
   (or counter (setq counter (list 100)))
   (let (done
        ;; For speed, remove the ange-ftp completion handler from the list.
@@ -849,8 +868,15 @@ use with M-x."
 \f
 (defun switch-to-buffer-other-window (buffer &optional norecord)
   "Select buffer BUFFER in another window.
+If BUFFER does not identify an existing buffer, then this function
+creates a buffer with that name.
+
+When called from Lisp, BUFFER can be a buffer, a string \(a buffer name),
+or nil.  If BUFFER is nil, then this function chooses a buffer
+using `other-buffer'.
 Optional second arg NORECORD non-nil means
 do not put this buffer at the front of the list of recently selected ones.
+This function returns the buffer it switched to.
 
 This uses the function `display-buffer' as a subroutine; see its
 documentation for additional customization information."
@@ -901,10 +927,12 @@ but the visited file name is available through the minibuffer history:
 type M-n to pull it into the minibuffer.
 
 Interactively, or if WILDCARDS is non-nil in a call from Lisp,
-expand wildcards (if any) and visit multiple files.  Wildcard expansion
-can be suppressed by setting `find-file-wildcards'."
-  (interactive
-   (find-file-read-args "Find file: " nil))
+expand wildcards (if any) and visit multiple files.  You can
+suppress wildcard expansion by setting `find-file-wildcards'.
+
+To visit a file without any kind of conversion and without
+automatically choosing a major mode, use \\[find-file-literally]."
+  (interactive (find-file-read-args "Find file: " nil))
   (let ((value (find-file-noselect filename nil nil wildcards)))
     (if (listp value)
        (mapcar 'switch-to-buffer (nreverse value))
@@ -926,8 +954,8 @@ expand wildcards (if any) and visit multiple files."
     (if (listp value)
        (progn
          (setq value (nreverse value))
-         (switch-to-buffer-other-window (car value))
-         (mapcar 'switch-to-buffer (cdr value)))
+         (cons (switch-to-buffer-other-window (car value))
+               (mapcar 'switch-to-buffer (cdr value))))
       (switch-to-buffer-other-window value))))
 
 (defun find-file-other-frame (filename &optional wildcards)
@@ -946,43 +974,69 @@ expand wildcards (if any) and visit multiple files."
     (if (listp value)
        (progn
          (setq value (nreverse value))
-         (switch-to-buffer-other-frame (car value))
-         (mapcar 'switch-to-buffer (cdr value)))
+         (cons (switch-to-buffer-other-frame (car value))
+               (mapcar 'switch-to-buffer (cdr value))))
       (switch-to-buffer-other-frame value))))
 
+(defun find-file-existing (filename &optional wildcards)
+  "Edit the existing file FILENAME.
+Like \\[find-file] but only allow files that exists."
+  (interactive (find-file-read-args "Find existing file: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
+  (find-file filename wildcards)
+  (current-buffer))
+
 (defun find-file-read-only (filename &optional wildcards)
   "Edit file FILENAME but don't allow changes.
 Like \\[find-file] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
+  (interactive (find-file-read-args "Find file read-only: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
 
 (defun find-file-read-only-other-window (filename &optional wildcards)
   "Edit file FILENAME in another window but don't allow changes.
 Like \\[find-file-other-window] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only other window: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file-other-window filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
+  (interactive (find-file-read-args "Find file read-only other window: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file-other-window filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
 
 (defun find-file-read-only-other-frame (filename &optional wildcards)
   "Edit file FILENAME in another frame but don't allow changes.
 Like \\[find-file-other-frame] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
-  (interactive (find-file-read-args "Find file read-only other frame: " t))
-  (unless (file-exists-p filename) (error "%s does not exist" filename))
-  (find-file-other-frame filename wildcards)
-  (toggle-read-only 1)
-  (current-buffer))
-
-(defun find-alternate-file-other-window (filename)
+  (interactive (find-file-read-args "Find file read-only other frame: " nil))
+  (unless (or (and wildcards find-file-wildcards
+                  (not (string-match "\\`/:" filename))
+                  (string-match "[[*?]" filename))
+             (file-exists-p filename))
+    (error "%s does not exist" filename))
+  (let ((value (find-file-other-frame filename wildcards)))
+    (mapc (lambda (b) (with-current-buffer b (toggle-read-only 1)))
+         (if (listp value) value (list value)))
+    value))
+
+(defun find-alternate-file-other-window (filename &optional wildcards)
   "Find file FILENAME as a replacement for the file in the next window.
-This command does not select that window."
+This command does not select that window.
+
+Interactively, or if WILDCARDS is non-nil in a call from Lisp,
+expand wildcards (if any) and replace the file with multiple files."
   (interactive
    (save-selected-window
      (other-window 1)
@@ -993,17 +1047,21 @@ This command does not select that window."
            (setq file-name (file-name-nondirectory file)
                  file-dir (file-name-directory file)))
        (list (read-file-name
-             "Find alternate file: " file-dir nil nil file-name)))))
+             "Find alternate file: " file-dir nil nil file-name)
+            t))))
   (if (one-window-p)
-      (find-file-other-window filename)
+      (find-file-other-window filename wildcards)
     (save-selected-window
       (other-window 1)
-      (find-alternate-file filename))))
+      (find-alternate-file filename wildcards))))
 
-(defun find-alternate-file (filename)
+(defun find-alternate-file (filename &optional wildcards)
   "Find file FILENAME, select its buffer, kill previous buffer.
 If the current buffer now contains an empty file that you just visited
-\(presumably by mistake), use this command to visit the file you really want."
+\(presumably by mistake), use this command to visit the file you really want.
+
+Interactively, or if WILDCARDS is non-nil in a call from Lisp,
+expand wildcards (if any) and replace the file with multiple files."
   (interactive
    (let ((file buffer-file-name)
         (file-name nil)
@@ -1012,7 +1070,8 @@ If the current buffer now contains an empty file that you just visited
          (setq file-name (file-name-nondirectory file)
                file-dir (file-name-directory file)))
      (list (read-file-name
-           "Find alternate file: " file-dir nil nil file-name))))
+           "Find alternate file: " file-dir nil nil file-name)
+          t)))
   (unless (run-hook-with-args-until-failure 'kill-buffer-query-functions)
     (error "Aborted"))
   (when (and (buffer-modified-p) (buffer-file-name))
@@ -1040,7 +1099,7 @@ If the current buffer now contains an empty file that you just visited
          (setq buffer-file-truename nil)
          ;; Likewise for dired buffers.
          (setq dired-directory nil)
-         (find-file filename))
+         (find-file filename wildcards))
       (when (eq obuf (current-buffer))
        ;; This executes if find-file gets an error
        ;; and does not really find anything.
@@ -1198,6 +1257,7 @@ suppresses this warning."
 When nil, never request confirmation."
   :group 'files
   :group 'find-file
+  :version "21.4"
   :type '(choice integer (const :tag "Never request confirmation" nil)))
 
 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
@@ -1209,8 +1269,8 @@ Optional first arg NOWARN non-nil means suppress any warning messages.
 Optional second arg RAWFILE non-nil means the file is read literally.
 Optional third arg WILDCARDS non-nil means do wildcard processing
 and visit all the matching files.  When wildcards are actually
-used and expanded, the value is a list of buffers
-that are visiting the various files."
+used and expanded, return a list of buffers that are visiting
+the various files."
   (setq filename
        (abbreviate-file-name
         (expand-file-name filename)))
@@ -1353,21 +1413,22 @@ that are visiting the various files."
                                rawfile truename number))))))
 
 (defun find-file-noselect-1 (buf filename nowarn rawfile truename number)
-  (let ((inhibit-read-only t)
-       error)
+  (let (error)
     (with-current-buffer buf
       (kill-local-variable 'find-file-literally)
       ;; Needed in case we are re-visiting the file with a different
       ;; text representation.
       (kill-local-variable 'buffer-file-coding-system)
       (kill-local-variable 'cursor-type)
-      (erase-buffer)
+      (let ((inhibit-read-only t))
+       (erase-buffer))
       (and (default-value 'enable-multibyte-characters)
           (not rawfile)
           (set-buffer-multibyte t))
       (if rawfile
          (condition-case ()
-             (insert-file-contents-literally filename t)
+             (let ((inhibit-read-only t))
+               (insert-file-contents-literally filename t))
            (file-error
             (when (and (file-exists-p filename)
                        (not (file-readable-p filename)))
@@ -1377,7 +1438,8 @@ that are visiting the various files."
             ;; Unconditionally set error
             (setq error t)))
        (condition-case ()
-           (insert-file-contents filename t)
+           (let ((inhibit-read-only t))
+             (insert-file-contents filename t))
          (file-error
           (when (and (file-exists-p filename)
                      (not (file-readable-p filename)))
@@ -1616,41 +1678,35 @@ in that case, this function acts as if `enable-local-variables' were t."
   (mapc
    (lambda (elt)
      (cons (purecopy (car elt)) (cdr elt)))
-   '(("\\.te?xt\\'" . text-mode)
-     ("\\.tex\\'" . tex-mode)
+   '(;; do this first, so that .html.pl is Polish html, not Perl
+     ("\\.s?html?\\(\\.[a-zA-Z_]+\\)?\\'" . html-mode)
+     ("\\.te?xt\\'" . text-mode)
+     ("\\.[tT]e[xX]\\'" . tex-mode)
      ("\\.ins\\'" . tex-mode)          ;Installation files for TeX packages.
      ("\\.ltx\\'" . latex-mode)
      ("\\.dtx\\'" . doctex-mode)
      ("\\.el\\'" . emacs-lisp-mode)
      ("\\.\\(scm\\|stk\\|ss\\|sch\\)\\'" . scheme-mode)
      ("\\.l\\'" . lisp-mode)
-     ("\\.lisp\\'" . lisp-mode)
-     ("\\.f\\'" . fortran-mode)
-     ("\\.F\\'" . fortran-mode)
+     ("\\.li?sp\\'" . lisp-mode)
+     ("\\.[fF]\\'" . fortran-mode)
      ("\\.for\\'" . fortran-mode)
      ("\\.p\\'" . pascal-mode)
      ("\\.pas\\'" . pascal-mode)
      ("\\.ad[abs]\\'" . ada-mode)
      ("\\.ad[bs].dg\\'" . ada-mode)
-     ("\\.\\([pP]\\([Llm]\\|erl\\)\\|al\\)\\'" . perl-mode)
-     ("\\.s?html?\\'" . html-mode)
+     ("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)
      ("\\.mk\\'" . makefile-mode)
-     ("\\(M\\|m\\|GNUm\\)akefile\\'" . makefile-mode)
+     ("\\([Mm]\\|GNUm\\)akep*file\\'" . makefile-mode)
      ("\\.am\\'" . makefile-mode)      ;For Automake.
      ;; Less common extensions come here
      ;; so more common ones above are found faster.
      ("\\.texinfo\\'" . texinfo-mode)
      ("\\.te?xi\\'" . texinfo-mode)
-     ("\\.s\\'" . asm-mode)
-     ("\\.S\\'" . asm-mode)
+     ("\\.[sS]\\'" . asm-mode)
      ("\\.asm\\'" . asm-mode)
-     ("ChangeLog\\'" . change-log-mode)
-     ("change\\.log\\'" . change-log-mode)
-     ("changelo\\'" . change-log-mode)
-     ("ChangeLog\\.[0-9]+\\'" . change-log-mode)
-     ;; for MSDOS and MS-Windows (which are case-insensitive)
-     ("changelog\\'" . change-log-mode)
-     ("changelog\\.[0-9]+\\'" . change-log-mode)
+     ("[cC]hange\\.?[lL]og?\\'" . change-log-mode)
+     ("[cC]hange[lL]og\\.[0-9]+\\'" . change-log-mode)
      ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
      ("\\.scm\\.[0-9]*\\'" . scheme-mode)
      ("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode)
@@ -1659,36 +1715,28 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\(/\\|\\`\\)\\.\\(bash_logout\\|shrc\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode)
      ("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode)
      ("\\.m?spec\\'" . sh-mode)
-     ("\\.mm\\'" . nroff-mode)
-     ("\\.me\\'" . nroff-mode)
-     ("\\.ms\\'" . nroff-mode)
+     ("\\.m[mes]\\'" . nroff-mode)
      ("\\.man\\'" . nroff-mode)
-     ("\\.TeX\\'" . tex-mode)
      ("\\.sty\\'" . latex-mode)
-     ("\\.cls\\'" . latex-mode)                ;LaTeX 2e class
-     ("\\.clo\\'" . latex-mode)                ;LaTeX 2e class option
+     ("\\.cl[so]\\'" . latex-mode)             ;LaTeX 2e class option
      ("\\.bbl\\'" . latex-mode)
      ("\\.bib\\'" . bibtex-mode)
      ("\\.sql\\'" . sql-mode)
-     ("\\.m4\\'" . m4-mode)
-     ("\\.mc\\'" . m4-mode)
+     ("\\.m[4c]\\'" . m4-mode)
      ("\\.mf\\'" . metafont-mode)
      ("\\.mp\\'" . metapost-mode)
      ("\\.vhdl?\\'" . vhdl-mode)
      ("\\.article\\'" . text-mode)
      ("\\.letter\\'" . text-mode)
-     ("\\.tcl\\'" . tcl-mode)
+     ("\\.i?tcl\\'" . tcl-mode)
      ("\\.exp\\'" . tcl-mode)
-     ("\\.itcl\\'" . tcl-mode)
      ("\\.itk\\'" . tcl-mode)
      ("\\.icn\\'" . icon-mode)
      ("\\.sim\\'" . simula-mode)
      ("\\.mss\\'" . scribe-mode)
-     ("\\.f90\\'" . f90-mode)
-     ("\\.f95\\'" . f90-mode)
+     ("\\.f9[05]\\'" . f90-mode)
      ("\\.indent\\.pro\\'" . fundamental-mode) ; to avoid idlwave-mode
      ("\\.pro\\'" . idlwave-mode)
-     ("\\.lsp\\'" . lisp-mode)
      ("\\.prolog\\'" . prolog-mode)
      ("\\.tar\\'" . tar-mode)
      ("\\.\\(arc\\|zip\\|lzh\\|zoo\\|ear\\|jar\\|war\\)\\'" . archive-mode)
@@ -1704,10 +1752,11 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\`/tmp/fol/" . text-mode)
      ("\\.oak\\'" . scheme-mode)
      ("\\.sgml?\\'" . sgml-mode)
-     ("\\.xml\\'" . sgml-mode)
-     ("\\.xsl\\'" . sgml-mode)
+     ("\\.x[ms]l\\'" . xml-mode)
      ("\\.dtd\\'" . sgml-mode)
      ("\\.ds\\(ss\\)?l\\'" . dsssl-mode)
+     ("\\.js\\'" . java-mode)          ; javascript-mode would be better
+     ("\\.x[bp]m\\'" . c-mode)
      ;; .emacs or .gnus or .viper following a directory delimiter in
      ;; Unix, MSDOG or VMS syntax.
      ("[]>:/\\]\\..*\\(emacs\\|gnus\\|viper\\)\\'" . emacs-lisp-mode)
@@ -1726,20 +1775,43 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("BROWSE\\'" . ebrowse-tree-mode)
      ("\\.ebrowse\\'" . ebrowse-tree-mode)
      ("#\\*mail\\*" . mail-mode)
+     ("\\.g\\'" . antlr-mode)
+     ("\\.ses\\'" . ses-mode)
+     ("\\.\\(soa\\|zone\\)\\'" . dns-mode)
+     ("\\.docbook\\'" . sgml-mode)
+     ("\\.com\\'" . dcl-mode)
+     ("/config\\.\\(?:bat\\|log\\)\\'" . fundamental-mode)
+     ;; Windows candidates may be opened case sensitively on Unix
+     ("\\.\\(?:[iI][nN][iI]\\|[lL][sS][tT]\\|[rR][eE][gG]\\|[sS][yY][sS]\\)\\'" . conf-mode)
+     ("\\.\\(?:desktop\\|la\\)\\'" . conf-unix-mode)
+     ("\\.ppd\\'" . conf-ppd-mode)
+     ("java.+\\.conf\\'" . conf-javaprop-mode)
+     ("\\.properties\\(?:\\.[a-zA-Z0-9._-]+\\)?\\'" . conf-javaprop-mode)
+     ;; *.cf, *.cfg, *.conf, *.config[.local|.de_DE.UTF8|...], */config
+     ("[/.]c\\(?:on\\)?f\\(?:i?g\\)?\\(?:\\.[a-zA-Z0-9._-]+\\)?\\'" . conf-mode)
+     ("\\`/etc/\\(?:DIR_COLORS\\|ethers\\|.?fstab\\|.*hosts\\|lesskey\\|login\\.?de\\(?:fs\\|vperm\\)\\|magic\\|mtab\\|pam\\.d/.*\\|permissions\\|protocols\\|rpc\\|services\\)\\'" . conf-space-mode)
+     ("\\`/etc/\\(?:acpid?/.+\\|aliases\\|default/.+\\|group-?\\|hosts\\..+\\|inittab\\|ksysguarddrc\\|opera6rc\\|passwd-?\\|shadow-?\\)\\'" . conf-mode)
+     ;; either user's dot-files or under /etc or some such
+     ("/\\.?\\(?:gnokiirc\\|kde.*rc\\|mime\\.types\\|wgetrc\\)\\'" . conf-mode)
+     ;; alas not all ~/.*rc files are like this
+     ("/\\.\\(?:enigma\\|gltron\\|gtk\\|hxplayer\\|net\\|neverball\\|qt/.+\\|realplayer\\|scummvm\\|sversion\\|sylpheed/.+\\|xmp\\)rc\\'" . conf-mode)
+     ("/\\.\\(?:gdbtkinit\\|grip\\|orbital/.+txt\\|rhosts\\|tuxracer/options\\)\\'" . conf-mode)
+     ("/\\.?X\\(?:default\\|resource\\|re\\)s\\>" . conf-xdefaults-mode)
+     ("/X11.+app-defaults/" . conf-xdefaults-mode)
+     ("/X11.+locale/.+/Compose\\'" . conf-colon-mode)
+     ;; this contains everything twice, with space and with colon :-(
+     ("/X11.+locale/compose\\.dir\\'" . conf-javaprop-mode)
      ;; Get rid of any trailing .n.m and try again.
      ;; This is for files saved by cvs-merge that look like .#<file>.<rev>
      ;; or .#<file>.<rev>-<rev> or VC's <file>.~<rev>~.
      ;; Using mode nil rather than `ignore' would let the search continue
      ;; through this list (with the shortened name) rather than start over.
-     ("\\.~?[0-9]+\\.[0-9][-.0-9]*~?\\'" ignore t)
+     ("\\.~?[0-9]+\\.[0-9][-.0-9]*~?\\'" nil t)
      ;; The following should come after the ChangeLog pattern
      ;; for the sake of ChangeLog.1, etc.
      ;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too.
      ("\\.[1-9]\\'" . nroff-mode)
-     ("\\.g\\'" . antlr-mode)
-     ("\\.ses\\'" . ses-mode)
-     ("\\.orig\\'" nil t)              ; from patch
-     ("\\.in\\'" nil t)))
+     ("\\.\\(?:orig\\|in\\|[bB][aA][kK]\\)\\'" nil t)))
   "Alist of filename patterns vs corresponding major mode functions.
 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
 \(NON-NIL stands for anything that is not nil; the value does not matter.)
@@ -1819,12 +1891,33 @@ be interpreted by the interpreter matched by the second group of the
 regular expression.  The mode is then determined as the mode associated
 with that interpreter in `interpreter-mode-alist'.")
 
-(defun set-auto-mode (&optional just-from-file-name)
+(defvar magic-mode-alist
+  `(;; The < comes before the groups (but the first) to reduce backtracking.
+    ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff.
+    (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
+            (comment-re (concat "\\(?:!--" incomment-re "*-->\\s *<\\)")))
+       (concat "\\(?:<\\?xml\\s +[^>]*>\\)?\\s *<"
+               comment-re "*"
+               "\\(?:!DOCTYPE\\s +[^>]*>\\s *<\\s *" comment-re "*\\)?"
+               "[Hh][Tt][Mm][Ll]")) . html-mode)
+    ;; These two must come after html, because they are more general:
+    ("<\\?xml " . xml-mode)
+    (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
+            (comment-re (concat "\\(?:!--" incomment-re "*-->\\s *<\\)")))
+       (concat "\\s *<" comment-re "*!DOCTYPE ")) . sgml-mode)
+    ("%![^V]" . ps-mode)
+    ("# xmcd " . conf-unix-mode))
+  "Alist of buffer beginnings vs. corresponding major mode functions.
+Each element looks like (REGEXP . FUNCTION).  FUNCTION will be
+called, unless it is nil (to allow `auto-mode-alist' to override).")
+
+(defun set-auto-mode (&optional keep-mode-if-same)
   "Select major mode appropriate for current buffer.
-This checks for a -*- mode tag in the buffer's text,
-compares the filename against the entries in `auto-mode-alist',
-or checks the interpreter that runs this file against
-`interpreter-mode-alist'.
+
+This checks for a -*- mode tag in the buffer's text, checks the
+interpreter that runs this file against `interpreter-mode-alist',
+compares the buffer beginning against `magic-mode-alist', or
+compares the filename against the entries in `auto-mode-alist'.
 
 It does not check for the `mode:' local variable in the
 Local Variables section of the file; for that, use `hack-local-variables'.
@@ -1832,11 +1925,11 @@ Local Variables section of the file; for that, use `hack-local-variables'.
 If `enable-local-variables' is nil, this function does not check for a
 -*- mode tag.
 
-If the optional argument JUST-FROM-FILE-NAME is non-nil,
-then we do not set anything but the major mode,
-and we don't even do that unless it would come from the file name."
+If the optional argument KEEP-MODE-IF-SAME is non-nil, then we
+only set the major mode, if that would change it."
   ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
-  (let (end done modes)
+  (let (end done mode modes)
+    ;; Find a -*- mode tag
     (save-excursion
       (goto-char (point-min))
       (skip-chars-forward " \t\n")
@@ -1861,63 +1954,75 @@ and we don't even do that unless it would come from the file name."
             (push (intern (concat (downcase (buffer-substring (point) end))
                                   "-mode"))
                   modes))))
-    ;; If we found modes to use, invoke them now,
-    ;; outside the save-excursion.
-    (unless just-from-file-name
-      (dolist (mode (nreverse modes))
-       (if (not (functionp mode))
-           (message "Ignoring unknown mode `%s'" mode)
-         (setq done t)
-         (funcall mode))))
-    ;; If we didn't find a mode from a -*- line, try using the file name.
-    (if (and (not done) buffer-file-name)
-       (let ((name buffer-file-name)
-             (keep-going t))
-         ;; Remove backup-suffixes from file name.
-         (setq name (file-name-sans-versions name))
-         (while keep-going
-           (setq keep-going nil)
-           (let ((alist auto-mode-alist)
-                 (mode nil))
-             ;; Find first matching alist entry.
-             (let ((case-fold-search
-                    (memq system-type '(vax-vms windows-nt cygwin))))
-               (while (and (not mode) alist)
-                 (if (string-match (car (car alist)) name)
-                     (if (and (consp (cdr (car alist)))
-                              (nth 2 (car alist)))
-                         (setq mode (car (cdr (car alist)))
-                               name (substring name 0 (match-beginning 0))
-                               keep-going t)
-                       (setq mode (cdr (car alist))
-                             keep-going nil)))
-                 (setq alist (cdr alist))))
-             (if mode
-                 ;; When JUST-FROM-FILE-NAME is set,
-                 ;; we are working on behalf of set-visited-file-name.
-                 ;; In that case, if the major mode specified is the
-                 ;; same one we already have, don't actually reset it.
-                 ;; We don't want to lose minor modes such as Font Lock.
-                 (unless (and just-from-file-name (eq mode major-mode))
-                   (funcall mode))
-               ;; If we can't deduce a mode from the file name,
-               ;; look for an interpreter specified in the first line.
-               ;; As a special case, allow for things like "#!/bin/env perl",
-               ;; which finds the interpreter anywhere in $PATH.
-               (let ((interpreter
-                      (save-excursion
-                        (goto-char (point-min))
-                        (if (looking-at auto-mode-interpreter-regexp)
-                            (match-string 2)
-                          "")))
-                     elt)
-                 ;; Map interpreter name to a mode.
-                 (setq elt (assoc (file-name-nondirectory interpreter)
-                                  interpreter-mode-alist))
-                 (unless just-from-file-name
-                   (if elt
-                       (funcall (cdr elt))))))))))))
-
+    ;; If we found modes to use, invoke them now, outside the save-excursion.
+    (if modes
+       (catch 'nop
+         (dolist (mode (nreverse modes))
+           (if (not (functionp mode))
+               (message "Ignoring unknown mode `%s'" mode)
+             (setq done t)
+             (or (set-auto-mode-0 mode keep-mode-if-same)
+                 ;; continuing would call minor modes again, toggling them off
+                 (throw 'nop nil)))))
+      ;; If we didn't, look for an interpreter specified in the first line.
+      ;; As a special case, allow for things like "#!/bin/env perl", which
+      ;; finds the interpreter anywhere in $PATH.
+      (setq mode (save-excursion
+                  (goto-char (point-min))
+                  (if (looking-at auto-mode-interpreter-regexp)
+                      (match-string 2)
+                    ""))
+           ;; Map interpreter name to a mode, signalling we're done at the
+           ;; same time.
+           done (assoc (file-name-nondirectory mode)
+                       interpreter-mode-alist))
+      ;; If we found an interpreter mode to use, invoke it now.
+      (if done
+         (set-auto-mode-0 (cdr done) keep-mode-if-same)))
+    ;; If we didn't, match the buffer beginning against magic-mode-alist.
+    (unless done
+      (if (setq done (save-excursion
+                      (goto-char (point-min))
+                      (assoc-default nil magic-mode-alist
+                                     (lambda (re dummy)
+                                       (looking-at re)))))
+         (set-auto-mode-0 done keep-mode-if-same)
+       ;; Compare the filename against the entries in auto-mode-alist.
+       (if buffer-file-name
+           (let ((name buffer-file-name))
+             ;; Remove backup-suffixes from file name.
+             (setq name (file-name-sans-versions name))
+             (while name
+               ;; Find first matching alist entry.
+               (let ((case-fold-search
+                      (memq system-type '(vax-vms windows-nt cygwin))))
+                 (if (and (setq mode (assoc-default name auto-mode-alist
+                                                    'string-match))
+                          (consp mode)
+                          (cadr mode))
+                     (setq mode (car mode)
+                           name (substring name 0 (match-beginning 0)))
+                   (setq name)))
+               (when mode
+                 (set-auto-mode-0 mode keep-mode-if-same)))))))))
+
+;; When `keep-mode-if-same' is set, we are working on behalf of
+;; set-visited-file-name.  In that case, if the major mode specified is the
+;; same one we already have, don't actually reset it.  We don't want to lose
+;; minor modes such as Font Lock.
+(defun set-auto-mode-0 (mode &optional keep-mode-if-same)
+  "Apply MODE and return it.
+If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of
+any aliases and compared to current major mode.  If they are the
+same, do nothing and return nil."
+  (when keep-mode-if-same
+    (while (symbolp (symbol-function mode))
+      (setq mode (symbol-function mode)))
+    (if (eq mode major-mode)
+       (setq mode nil)))
+  (when mode
+    (funcall mode)
+    mode))
 
 (defun set-auto-mode-1 ()
   "Find the -*- spec in the buffer.
@@ -2077,9 +2182,7 @@ is specified, returning t if it is specified."
                                                   buffer-file-name)
                                                (concat "buffer "
                                                        (buffer-name))))))))))
-         (let ((continue t)
-               prefix prefixlen suffix beg
-               mode-specified
+         (let (prefix prefixlen suffix beg
                (enable-local-eval enable-local-eval))
            ;; The prefix is what comes before "local variables:" in its line.
            ;; The suffix is what comes after "local variables:" in its line.
@@ -2096,46 +2199,66 @@ is specified, returning t if it is specified."
            (if prefix (setq prefixlen (length prefix)
                             prefix (regexp-quote prefix)))
            (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
-           (while continue
-             ;; Look at next local variable spec.
-             (if selective-display (re-search-forward "[\n\C-m]")
-               (forward-line 1))
-             ;; Skip the prefix, if any.
-             (if prefix
-                 (if (looking-at prefix)
-                     (forward-char prefixlen)
-                   (error "Local variables entry is missing the prefix")))
-             ;; Find the variable name; strip whitespace.
-             (skip-chars-forward " \t")
-             (setq beg (point))
-             (skip-chars-forward "^:\n")
-             (if (eolp) (error "Missing colon in local variables entry"))
-             (skip-chars-backward " \t")
-             (let* ((str (buffer-substring beg (point)))
-                    (var (read str))
-                   val)
-               ;; Setting variable named "end" means end of list.
-               (if (string-equal (downcase str) "end")
-                   (setq continue nil)
-                 ;; Otherwise read the variable value.
-                 (skip-chars-forward "^:")
-                 (forward-char 1)
-                 (setq val (read (current-buffer)))
-                 (skip-chars-backward "\n")
+           (forward-line 1)
+           (let ((startpos (point))
+                 endpos
+                 (thisbuf (current-buffer)))
+             (save-excursion
+               (if (not (re-search-forward
+                         (concat (or prefix "")
+                                 "[ \t]*End:[ \t]*"
+                                 (or suffix ""))
+                         nil t))
+                   (error "Local variables list is not properly terminated"))
+               (beginning-of-line)
+               (setq endpos (point)))
+
+             (with-temp-buffer
+               (insert-buffer-substring thisbuf startpos endpos)
+               (goto-char (point-min))
+               (subst-char-in-region (point) (point-max)
+                                     ?\^m ?\n)
+               (while (not (eobp))
+                 ;; Discard the prefix, if any.
+                 (if prefix
+                     (if (looking-at prefix)
+                         (delete-region (point) (match-end 0))
+                       (error "Local variables entry is missing the prefix")))
+                 (end-of-line)
+                 ;; Discard the suffix, if any.
+                 (if suffix
+                     (if (looking-back suffix)
+                         (delete-region (match-beginning 0) (point))
+                       (error "Local variables entry is missing the suffix")))
+                 (forward-line 1))
+               (goto-char (point-min))
+
+               (while (not (eobp))
+                 ;; Find the variable name; strip whitespace.
                  (skip-chars-forward " \t")
-                 (or (if suffix (looking-at suffix) (eolp))
-                     (error "Local variables entry is terminated incorrectly"))
-                 (if mode-only
-                     (if (eq var 'mode)
-                         (setq mode-specified t))
-                   ;; Set the variable.  "Variables" mode and eval are funny.
-                   (hack-one-local-variable var val))))))))
+                 (setq beg (point))
+                 (skip-chars-forward "^:\n")
+                 (if (eolp) (error "Missing colon in local variables entry"))
+                 (skip-chars-backward " \t")
+                 (let* ((str (buffer-substring beg (point)))
+                        (var (read str))
+                        val)
+                   ;; Read the variable value.
+                   (skip-chars-forward "^:")
+                   (forward-char 1)
+                   (setq val (read (current-buffer)))
+                   (if mode-only
+                       (if (eq var 'mode)
+                           (setq mode-specified t))
+                     ;; Set the variable.  "Variables" mode and eval are funny.
+                     (with-current-buffer thisbuf
+                       (hack-one-local-variable var val))))
+                 (forward-line 1)))))))
     (unless mode-only
       (run-hooks 'hack-local-variables-hook))
     mode-specified))
 
-(defvar ignored-local-variables
-  '(enable-local-eval)
+(defvar ignored-local-variables ()
   "Variables to be ignored in a file's local variable spec.")
 
 ;; Get confirmation before setting these variables as locals in a file.
@@ -2201,8 +2324,7 @@ is specified, returning t if it is specified."
 If VAL is nil or omitted, the question is whether any value might be
 dangerous."
   (let ((safep (get sym 'safe-local-variable)))
-    (or (memq sym ignored-local-variables)
-       (get sym 'risky-local-variable)
+    (or (get sym 'risky-local-variable)
        (and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
                           (symbol-name sym))
             (not safep))
@@ -2276,6 +2398,8 @@ is considered risky."
        ((eq var 'coding)
         ;; We have already handled coding: tag in set-auto-coding.
         nil)
+       ((memq var ignored-local-variables)
+        nil)
        ;; "Setting" eval means either eval it or do nothing.
        ;; Likewise for setting hook variables.
        ((risky-local-variable-p var val)
@@ -2325,7 +2449,7 @@ However, the mode will not be changed if
 (defun set-visited-file-name (filename &optional no-query along-with-file)
   "Change name of file visited in current buffer to FILENAME.
 The next time the buffer is saved it will go in the newly specified file.
-nil or empty string as argument means make buffer not be visiting any file.
+FILENAME nil or an empty string means make buffer not be visiting any file.
 Remember to delete the initial contents of the minibuffer
 if you wish to pass an empty string as the argument.
 
@@ -2897,49 +3021,36 @@ on a DOS/Windows machine, it returns FILENAME on expanded form."
          (file-name-as-directory (expand-file-name (or directory
                                                        default-directory))))
     (setq filename (expand-file-name filename))
-    (let ((hf (find-file-name-handler filename 'file-local-copy))
-          (hd (find-file-name-handler directory 'file-local-copy)))
-      (when (and hf (not (get hf 'file-remote-p))) (setq hf nil))
-      (when (and hd (not (get hd 'file-remote-p))) (setq hd nil))
+    (let ((fremote (file-remote-p filename))
+          (dremote (file-remote-p directory)))
       (if ;; Conditions for separate trees
          (or
           ;; Test for different drives on DOS/Windows
           (and
+           ;; Should `cygwin' really be included here?  --stef
            (memq system-type '(ms-dos cygwin windows-nt))
-           (not (string-equal (substring filename  0 2)
-                              (substring directory 0 2))))
-          ;; Test for different remote file handlers
-          (not (eq hf hd))
+           (not (eq t (compare-strings filename 0 2 directory 0 2))))
           ;; Test for different remote file system identification
-          (and
-           hf
-           (let ((re (car (rassq hf file-name-handler-alist))))
-             (not
-              (equal
-               (and
-                (string-match re filename)
-                (substring filename 0 (match-end 0)))
-               (and
-                (string-match re directory)
-                (substring directory 0 (match-end 0))))))))
+          (not (equal fremote dremote)))
          filename
         (let ((ancestor ".")
              (filename-dir (file-name-as-directory filename)))
-          (while
-             (and
-              (not (string-match (concat "\\`" (regexp-quote directory))
-                                 filename-dir))
-              (not (string-match (concat "\\`" (regexp-quote directory))
-                                 filename)))
+          (while (not
+                 (or
+                  (eq t (compare-strings filename-dir nil (length directory)
+                                         directory nil nil case-fold-search))
+                  (eq t (compare-strings filename nil (length directory)
+                                         directory nil nil case-fold-search))))
             (setq directory (file-name-directory (substring directory 0 -1))
                  ancestor (if (equal ancestor ".")
                               ".."
                             (concat "../" ancestor))))
           ;; Now ancestor is empty, or .., or ../.., etc.
-          (if (string-match (concat "^" (regexp-quote directory)) filename)
+          (if (eq t (compare-strings filename nil (length directory)
+                                    directory nil nil case-fold-search))
              ;; We matched within FILENAME's directory part.
              ;; Add the rest of FILENAME onto ANCESTOR.
-             (let ((rest (substring filename (match-end 0))))
+             (let ((rest (substring filename (length directory))))
                (if (and (equal ancestor ".") (not (equal rest "")))
                    ;; But don't bother with ANCESTOR if it would give us `./'.
                    rest
@@ -3009,7 +3120,7 @@ the last real save, but optional arg FORCE non-nil means delete anyway."
 
 (defcustom before-save-hook nil
   "Normal hook that is run before a buffer is saved to its file."
-  :options '(copyright-update)
+  :options '(copyright-update time-stamp)
   :type 'hook
   :group 'files)
 
@@ -3453,6 +3564,23 @@ and second, t if reading the auto-save file.
 
 The function you specify is responsible for updating (or preserving) point.")
 
+(defvar buffer-stale-function nil
+  "Function to check whether a non-file buffer needs reverting.
+This should be a function with one optional argument NOCONFIRM.
+Auto Revert Mode sets NOCONFIRM to t.  The function should return
+non-nil if the buffer should be reverted.  A return value of
+`fast' means that the need for reverting was not checked, but
+that reverting the buffer is fast.  The buffer is current when
+this function is called.
+
+The idea behind the NOCONFIRM argument is that it should be
+non-nil if the buffer is going to be reverted without asking the
+user.  In such situations, one has to be careful with potentially
+time consuming operations.
+
+For more information on how this variable is used by Auto Revert mode,
+see Info node `(emacs-xtra)Supporting additional buffers'.")
+
 (defvar before-revert-hook nil
   "Normal hook for `revert-buffer' to run before reverting.
 If `revert-buffer-function' is used to override the normal revert
@@ -3754,7 +3882,7 @@ This command is used in the special Dired buffer created by
 
 (defun kill-some-buffers (&optional list)
   "Kill some buffers.  Asks the user whether to kill each one of them.
-Non-interactively, if optional argument LIST is non-`nil', it
+Non-interactively, if optional argument LIST is non-nil, it
 specifies the list of buffers to kill, asking for approval for each one."
   (interactive)
   (if (null list)
@@ -4004,7 +4132,7 @@ by `sh' are supported."
   "Expand wildcard pattern PATTERN.
 This returns a list of file names which match the pattern.
 
-If PATTERN is written as an absolute relative file name,
+If PATTERN is written as an absolute file name,
 the values are absolute also.
 
 If PATTERN is written as a relative file name, it is interpreted
@@ -4205,14 +4333,20 @@ program specified by `directory-free-space-program' if that is non-nil."
 (defun insert-directory (file switches &optional wildcard full-directory-p)
   "Insert directory listing for FILE, formatted according to SWITCHES.
 Leaves point after the inserted text.
-SWITCHES may be a string of options, or a list of strings.
+SWITCHES may be a string of options, or a list of strings
+representing individual options.
 Optional third arg WILDCARD means treat FILE as shell wildcard.
 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
 switches do not contain `d', so that a full listing is expected.
 
 This works by running a directory listing program
 whose name is in the variable `insert-directory-program'.
-If WILDCARD, it also runs the shell specified by `shell-file-name'."
+If WILDCARD, it also runs the shell specified by `shell-file-name'.
+
+When SWITCHES contains the long `--dired' option, this function
+treats it specially, for the sake of dired.  However, the
+normally equivalent short `-D' option is just passed on to
+`insert-directory-program', as any other option."
   ;; We need the directory in order to find the right handler.
   (let ((handler (find-file-name-handler (expand-file-name file)
                                         'insert-directory)))
@@ -4286,6 +4420,8 @@ If WILDCARD, it also runs the shell specified by `shell-file-name'."
 
          ;; If `insert-directory-program' failed, signal an error.
          (unless (eq 0 result)
+           ;; Delete the error message it may have output.
+           (delete-region beg (point))
            ;; On non-Posix systems, we cannot open a directory, so
            ;; don't even try, because that will always result in
            ;; the ubiquitous "Access denied".  Instead, show the
@@ -4301,26 +4437,34 @@ If WILDCARD, it also runs the shell specified by `shell-file-name'."
              (access-file file "Reading directory")
              (error "Listing directory failed but `access-file' worked")))
 
-         (when (string-match "--dired\\>" switches)
+         (when (if (stringp switches)
+                   (string-match "--dired\\>" switches)
+                 (member "--dired" switches))
            (forward-line -2)
             (when (looking-at "//SUBDIRED//")
               (delete-region (point) (progn (forward-line 1) (point)))
               (forward-line -1))
-           (let ((end (line-end-position)))
-             (forward-word 1)
-             (forward-char 3)
-             (while (< (point) end)
-               (let ((start (+ beg (read (current-buffer))))
-                     (end (+ beg (read (current-buffer)))))
-                 (if (= (char-after end) ?\n)
-                     (put-text-property start end 'dired-filename t)
-                   ;; It seems that we can't trust ls's output as to
-                   ;; byte positions of filenames.
-                   (put-text-property beg (point) 'dired-filename nil)
-                   (end-of-line))))
-             (goto-char end)
-             (beginning-of-line)
-             (delete-region (point) (progn (forward-line 2) (point)))))
+           (if (looking-at "//DIRED//")
+               (let ((end (line-end-position)))
+                 (forward-word 1)
+                 (forward-char 3)
+                 (while (< (point) end)
+                   (let ((start (+ beg (read (current-buffer))))
+                         (end (+ beg (read (current-buffer)))))
+                     (if (memq (char-after end) '(?\n ?\ ))
+                         ;; End is followed by \n or by " -> ".
+                         (put-text-property start end 'dired-filename t)
+                       ;; It seems that we can't trust ls's output as to
+                       ;; byte positions of filenames.
+                       (put-text-property beg (point) 'dired-filename nil)
+                       (end-of-line))))
+                 (goto-char end)
+                 (beginning-of-line)
+                 (delete-region (point) (progn (forward-line 2) (point))))
+             (forward-line 1)
+             (if (looking-at "//DIRED-OPTIONS//")
+                 (delete-region (point) (progn (forward-line 1) (point)))
+               (forward-line 1))))
 
          ;; Now decode what read if necessary.
          (let ((coding (or coding-system-for-read
@@ -4393,7 +4537,7 @@ be a predicate function such as `yes-or-no-p'."
   :type '(choice (const :tag "Ask with yes-or-no-p" yes-or-no-p)
                 (const :tag "Ask with y-or-n-p" y-or-n-p)
                 (const :tag "Don't confirm" nil))
-  :group 'emacs
+  :group 'convenience
   :version "21.1")
 
 (defun save-buffers-kill-emacs (&optional arg)
@@ -4447,7 +4591,7 @@ With prefix arg, silently save all file-visiting buffers, then kill."
        ;; Get a list of the indices of the args which are file names.
        (file-arg-indices
         (cdr (or (assq operation
-                       ;; The first five are special because they
+                       ;; The first six are special because they
                        ;; return a file name.  We want to include the /:
                        ;; in the return value.
                        ;; So just avoid stripping it in the first place.
@@ -4456,11 +4600,21 @@ With prefix arg, silently save all file-visiting buffers, then kill."
                          (file-name-as-directory . nil)
                          (directory-file-name . nil)
                          (file-name-sans-versions . nil)
+                         (find-backup-file-name . nil)
                          ;; `identity' means just return the first arg
-                         ;; as stripped of its quoting.
-                         (substitute-in-file-name . identity)
+                         ;; not stripped of its quoting.
+                         (substitute-in-file-name identity)
+                         ;; `add' means add "/:" to the result.
+                         (file-truename add 0)
+                         ;; `quote' means add "/:" to buffer-file-name.
+                         (insert-file-contents quote 0)
+                         ;; `unquote-then-quote' means set buffer-file-name
+                         ;; temporarily to unquoted filename.
+                         (verify-visited-file-modtime unquote-then-quote)
+                         ;; List the arguments which are filenames.
                          (file-name-completion 1)
                          (file-name-all-completions 1)
+                         (write-region 2 5)
                          (rename-file 0 1)
                          (copy-file 0 1)
                          (make-symbolic-link 0 1)
@@ -4468,9 +4622,12 @@ With prefix arg, silently save all file-visiting buffers, then kill."
                  ;; For all other operations, treat the first argument only
                  ;; as the file name.
                  '(nil 0))))
+       method
        ;; Copy ARGUMENTS so we can replace elements in it.
        (arguments (copy-sequence arguments)))
-    ;; Strip off the /: from the file names that have this handler.
+    (if (symbolp (car file-arg-indices))
+       (setq method (pop file-arg-indices)))
+    ;; Strip off the /: from the file names that have it.
     (save-match-data
       (while (consp file-arg-indices)
        (let ((pair (nthcdr (car file-arg-indices) arguments)))
@@ -4481,9 +4638,21 @@ With prefix arg, silently save all file-visiting buffers, then kill."
                           "/"
                         (substring (car pair) 2)))))
        (setq file-arg-indices (cdr file-arg-indices))))
-    (if (eq file-arg-indices 'identity)
-       (car arguments)
-      (apply operation arguments))))
+    (cond ((eq method 'identity)
+          (car arguments))
+         ((eq method 'add)
+          (concat "/:" (apply operation arguments)))
+         ((eq method 'quote)
+          (prog1 (apply operation arguments)
+            (setq buffer-file-name (concat "/:" buffer-file-name))))
+         ((eq method 'unquote-then-quote)
+          (let (res)
+            (setq buffer-file-name (substring buffer-file-name 2))
+            (setq res (apply operation arguments))
+            (setq buffer-file-name (concat "/:" buffer-file-name))
+            res))
+         (t
+          (apply operation arguments)))))
 \f
 (define-key ctl-x-map "\C-f" 'find-file)
 (define-key ctl-x-map "\C-r" 'find-file-read-only)
@@ -4494,7 +4663,7 @@ With prefix arg, silently save all file-visiting buffers, then kill."
 (define-key ctl-x-map "i" 'insert-file)
 (define-key esc-map "~" 'not-modified)
 (define-key ctl-x-map "\C-d" 'list-directory)
-(define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
+(define-key ctl-x-map "\C-c" 'server-save-buffers-kill-display)
 (define-key ctl-x-map "\C-q" 'toggle-read-only)
 
 (define-key ctl-x-4-map "f" 'find-file-other-window)