Merged in changes from CVS trunk.
[bpt/emacs.git] / lisp / files.el
index fc75668..138cced 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
@@ -42,7 +42,7 @@
 (defcustom delete-auto-save-files t
   "*Non-nil means delete auto-save file when a buffer is saved or killed.
 
-Note that auto-save file will not be deleted if the buffer is killed
+Note that the auto-save file will not be deleted if the buffer is killed
 when it has unsaved changes."
   :type 'boolean
   :group 'auto-save)
@@ -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))
@@ -355,10 +355,14 @@ The functions are called in the order given until one of them returns non-nil.")
 
 ;;;It is not useful to make this a local variable.
 ;;;(put 'find-file-hooks 'permanent-local t)
-(defvar find-file-hook nil
+(defcustom find-file-hook nil
   "List of functions to be called after a buffer is loaded from a file.
 The buffer's local variables (if any) will have been processed before the
-functions are called.")
+functions are called."
+  :group 'find-file
+  :type 'hook
+  :options '(auto-insert)
+  :version "21.4")
 (defvaralias 'find-file-hooks 'find-file-hook)
 (make-obsolete-variable 'find-file-hooks 'find-file-hook "21.4")
 
@@ -371,7 +375,12 @@ So any buffer-local binding of this variable is discarded if you change
 the visited file name with \\[set-visited-file-name], but not when you
 change the major mode.
 
-See also `write-contents-functions'.")
+This hook is not run if any of the functions in
+`write-contents-functions' returns non-nil.  Both hooks pertain
+to how to save a buffer to file, for instance, choosing a suitable
+coding system and setting mode bits.  (See Info
+node `(elisp)Saving Buffers'.)  To perform various checks or
+updates before the buffer is saved, use `before-save-hook' .")
 (put 'write-file-functions 'permanent-local t)
 (defvaralias 'write-file-hooks 'write-file-functions)
 (make-obsolete-variable 'write-file-hooks 'write-file-functions "21.4")
@@ -384,14 +393,19 @@ See also `write-contents-functions'.")
 (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,
 `set-visited-file-name' does not clear this variable; but changing the
 major mode does clear it.
 
-See also `write-file-functions'.")
+For hooks that _do_ pertain to the particular visited file, use
+`write-file-functions'.  Both this variable and
+`write-file-functions' relate to how a buffer is saved to file.
+To perform various checks or updates before the buffer is saved,
+use `before-save-hook'.")
 (make-variable-buffer-local 'write-contents-functions)
 (defvaralias 'write-contents-hooks 'write-contents-functions)
 (make-obsolete-variable 'write-contents-hooks 'write-contents-functions "21.4")
@@ -468,22 +482,32 @@ Runs the usual ange-ftp hook, but only for completion operations."
 
 (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."
+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')."
   filename)
 
 (defun read-directory-name (prompt &optional dir default-dirname mustmatch initial)
   "Read directory name, prompting with PROMPT and completing in directory DIR.
 Value is not expanded---you must call `expand-file-name' yourself.
-Default name to DEFAULT-DIRNAME if user enters a null string.
+Default name to DEFAULT-DIRNAME if user exits with the same
+non-empty string that was inserted by this function.
  (If DEFAULT-DIRNAME is omitted, the current buffer's directory is used,
   except that if INITIAL is specified, that combined with DIR is used.)
+If the user exits with an empty minibuffer, this function returns
+an empty string.  (This can only happen if the user erased the
+pre-inserted contents or if `insert-default-directory' is nil.)
 Fourth arg MUSTMATCH non-nil means require existing directory's name.
  Non-nil and non-t means also require confirmation after completion.
 Fifth arg INITIAL specifies text to start with.
-DIR defaults to current buffer's directory default."
+DIR should be an absolute directory name.  It defaults to
+the value of `default-directory'."
   (unless dir
     (setq dir default-directory))
   (unless default-dirname
@@ -503,8 +527,9 @@ DIR defaults to current buffer's directory default."
 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)
@@ -537,8 +562,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
@@ -598,6 +625,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
@@ -621,10 +650,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.
@@ -643,21 +679,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)"
+  ;; 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.
@@ -831,13 +869,22 @@ 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."
   (interactive "BSwitch to buffer in other window: ")
-  (let ((pop-up-windows t))
+  (let ((pop-up-windows t)
+       ;; Don't let these interfere.
+       same-window-buffer-names same-window-regexps)
     (pop-to-buffer buffer t norecord)))
 
 (defun switch-to-buffer-other-frame (buffer &optional norecord)
@@ -848,7 +895,8 @@ do not put this buffer at the front of the list of recently selected ones.
 This uses the function `display-buffer' as a subroutine; see its
 documentation for additional customization information."
   (interactive "BSwitch to buffer in other frame: ")
-  (let ((pop-up-frames t))
+  (let ((pop-up-frames t)
+       same-window-buffer-names same-window-regexps)
     (pop-to-buffer buffer t norecord)
     (raise-frame (window-frame (selected-window)))))
 
@@ -880,8 +928,11 @@ 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'."
+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)))
@@ -1332,21 +1383,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)))
@@ -1356,7 +1408,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)))
@@ -1601,7 +1654,7 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\.ltx\\'" . latex-mode)
      ("\\.dtx\\'" . doctex-mode)
      ("\\.el\\'" . emacs-lisp-mode)
-     ("\\.scm\\'" . scheme-mode)
+     ("\\.\\(scm\\|stk\\|ss\\|sch\\)\\'" . scheme-mode)
      ("\\.l\\'" . lisp-mode)
      ("\\.lisp\\'" . lisp-mode)
      ("\\.f\\'" . fortran-mode)
@@ -1717,6 +1770,8 @@ in that case, this function acts as if `enable-local-variables' were t."
      ("\\.[1-9]\\'" . nroff-mode)
      ("\\.g\\'" . antlr-mode)
      ("\\.ses\\'" . ses-mode)
+     ("\\.orig\\'" nil t)              ; from patch
+     ("\\.\\(soa\\|zone\\)\\'" . dns-mode)
      ("\\.in\\'" nil t)))
   "Alist of filename patterns vs corresponding major mode functions.
 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
@@ -2174,9 +2229,10 @@ is specified, returning t if it is specified."
 ;; This case is safe because the user gets to check it before it is used.
 (put 'compile-command 'safe-local-variable 'stringp)
 
-(defun risky-local-variable-p (sym val)
+(defun risky-local-variable-p (sym &optional val)
   "Non-nil if SYM could be dangerous as a file-local variable with value VAL.
-If VAL is nil, the question is whether any value might be dangerous."
+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)
@@ -2302,7 +2358,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.
 
@@ -2414,7 +2470,7 @@ This makes the buffer visit that file, and marks it as not modified.
 
 If you specify just a directory name as FILENAME, that means to use
 the default file name but in that directory.  You can also yank
-the default file name into the minibuffer to edit it, using M-n.
+the default file name into the minibuffer to edit it, using \\<minibuffer-local-map>\\[next-history-element].
 
 If the buffer is not already visiting a file, the default file name
 for the output file is the buffer name.
@@ -2546,7 +2602,8 @@ BACKUPNAME is the backup file name, which is the old file renamed."
              (not (file-writable-p to-name)))
         (delete-file to-name))
      (copy-file from-name to-name t t)))
-  (set-file-modes to-name (logand modes #o1777)))
+  (and modes
+       (set-file-modes to-name (logand modes #o1777))))
 
 (defun file-name-sans-versions (name &optional keep-backup-version)
   "Return file NAME sans backup versions or strings.
@@ -2873,49 +2930,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
@@ -2983,6 +3027,12 @@ the last real save, but optional arg FORCE non-nil means delete anyway."
 (defvar auto-save-hook nil
   "Normal hook run just before auto-saving.")
 
+(defcustom before-save-hook nil
+  "Normal hook that is run before a buffer is saved to its file."
+  :options '(copyright-update time-stamp)
+  :type 'hook
+  :group 'files)
+
 (defcustom after-save-hook nil
   "Normal hook that is run after a buffer is saved to its file."
   :options '(executable-make-buffer-file-executable-if-script-p)
@@ -3005,7 +3055,8 @@ in such cases.")
 The hooks `write-contents-functions' and `write-file-functions' get a chance
 to do the job of saving; if they do not, then the buffer is saved in
 the visited file file in the usual way.
-After saving the buffer, this function runs `after-save-hook'."
+Before and after saving the buffer, this function runs
+`before-save-hook' and `after-save-hook', respectively."
   (interactive)
   (save-current-buffer
     ;; In an indirect buffer, save its base buffer instead.
@@ -3061,6 +3112,7 @@ After saving the buffer, this function runs `after-save-hook'."
                     (insert ?\n))))
            ;; Support VC version backups.
            (vc-before-save)
+           (run-hooks 'before-save-hook)
            (or (run-hook-with-args-until-success 'write-contents-functions)
                (run-hook-with-args-until-success 'local-write-file-hooks)
                (run-hook-with-args-until-success 'write-file-functions)
@@ -3421,6 +3473,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
@@ -3721,8 +3790,9 @@ This command is used in the special Dired buffer created by
       (kill-buffer buffer))))
 
 (defun kill-some-buffers (&optional list)
-  "For each buffer in LIST, ask whether to kill it.
-LIST defaults to all existing live buffers."
+  "Kill some buffers.  Asks the user whether to kill each one of them.
+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)
       (setq list (buffer-list)))
@@ -3971,7 +4041,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
@@ -4129,10 +4199,10 @@ program specified by `directory-free-space-program' if that is non-nil."
     (save-match-data
       (with-temp-buffer
        (when (and directory-free-space-program
-                  (zerop (call-process directory-free-space-program
-                                       nil t nil
-                                       directory-free-space-args
-                                       dir)))
+                  (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))
@@ -4172,14 +4242,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)))
@@ -4252,42 +4328,51 @@ If WILDCARD, it also runs the shell specified by `shell-file-name'."
                                 file))))))))
 
          ;; If `insert-directory-program' failed, signal an error.
-         (if (/= result 0)
-             ;; 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
-             ;; command line so the user can try to guess what went wrong.
-             (if (and (file-directory-p file)
-                      (memq system-type '(ms-dos windows-nt)))
-                 (error
-                  "Reading directory: \"%s %s -- %s\" exited with status %s"
-                  insert-directory-program
-                  (if (listp switches) (concat switches) switches)
-                  file result)
-               ;; Unix.  Access the file to get a suitable error.
-               (access-file file "Reading directory")
-               (error "Listing directory failed but `access-file' worked")))
-
-         (when (string-match "--dired\\>" switches)
+         (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
+           ;; command line so the user can try to guess what went wrong.
+           (if (and (file-directory-p file)
+                    (memq system-type '(ms-dos windows-nt)))
+               (error
+                "Reading directory: \"%s %s -- %s\" exited with status %s"
+                insert-directory-program
+                (if (listp switches) (concat switches) switches)
+                file result)
+             ;; Unix.  Access the file to get a suitable error.
+             (access-file file "Reading directory")
+             (error "Listing directory failed but `access-file' worked")))
+
+         (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 (= (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))))
+             (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
@@ -4360,7 +4445,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)
@@ -4414,7 +4499,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.
@@ -4423,11 +4508,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)
@@ -4435,9 +4530,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)))
@@ -4448,9 +4546,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)
@@ -4461,7 +4571,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)