Merge from emacs-23
[bpt/emacs.git] / lisp / dired.el
index 0de8022..56030fe 100644 (file)
@@ -1,12 +1,13 @@
 ;;; dired.el --- directory-browsing commands
 
 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1997, 2000,
-;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
 ;;   Free Software Foundation, Inc.
 
 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
 ;; Maintainer: FSF
 ;; Keywords: files
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
@@ -50,7 +51,7 @@
 
 
 ;;;###autoload
-(defcustom dired-listing-switches "-al"
+(defcustom dired-listing-switches (purecopy "-al")
   "Switches passed to `ls' for Dired.  MUST contain the `l' option.
 May contain all other options that don't contradict `-l';
 may contain even `F', `b', `i' and `s'.  See also the variable
@@ -71,15 +72,19 @@ If nil, `dired-listing-switches' is used.")
 
 ;;;###autoload
 (defvar dired-chown-program
-  (if (memq system-type '(hpux usg-unix-v irix linux gnu/linux cygwin))
+  (purecopy
+  (if (memq system-type '(hpux usg-unix-v irix gnu/linux cygwin))
       "chown"
     (if (file-exists-p "/usr/sbin/chown")
        "/usr/sbin/chown"
-      "/etc/chown"))
+      "/etc/chown")))
   "Name of chown command (usually `chown' or `/etc/chown').")
 
-(defvar dired-use-ls-dired (not (not (string-match "gnu" system-configuration)))
-  "Non-nil means Dired should use `ls --dired'.")
+(defvar dired-use-ls-dired 'unspecified
+  "Non-nil means Dired should use \"ls --dired\".
+The special value of `unspecified' means to check explicitly, and
+save the result in this variable.  This is performed the first
+time `dired-insert-directory' is called.")
 
 (defvar dired-chmod-program "chmod"
   "Name of chmod command (usually `chmod').")
@@ -87,7 +92,6 @@ If nil, `dired-listing-switches' is used.")
 (defvar dired-touch-program "touch"
   "Name of touch command (usually `touch').")
 
-;;;###autoload
 (defcustom dired-ls-F-marks-symlinks nil
   "Informs Dired about how `ls -lF' marks symbolic links.
 Set this to t if `ls' (or whatever program is specified by
@@ -105,7 +109,7 @@ always set this variable to t."
   :group 'dired-mark)
 
 ;;;###autoload
-(defcustom dired-trivial-filenames "^\\.\\.?$\\|^#"
+(defcustom dired-trivial-filenames (purecopy "^\\.\\.?$\\|^#")
   "Regexp of files to skip when finding first file of a directory.
 A value of nil means move to the subdir line.
 A value of t means move to first file."
@@ -114,7 +118,6 @@ A value of t means move to first file."
                 regexp)
   :group 'dired)
 
-;;;###autoload
 (defcustom dired-keep-marker-rename t
   ;; Use t as default so that moved files "take their markers with them".
   "Controls marking of renamed files.
@@ -125,7 +128,6 @@ are afterward marked with that character."
                 (character :tag "Mark"))
   :group 'dired-mark)
 
-;;;###autoload
 (defcustom dired-keep-marker-copy ?C
   "Controls marking of copied files.
 If t, copied files are marked if and as the corresponding original files were.
@@ -134,7 +136,6 @@ If a character, copied files are unconditionally marked with that character."
                 (character :tag "Mark"))
   :group 'dired-mark)
 
-;;;###autoload
 (defcustom dired-keep-marker-hardlink ?H
   "Controls marking of newly made hard links.
 If t, they are marked if and as the files linked to were marked.
@@ -143,7 +144,6 @@ If a character, new links are unconditionally marked with that character."
                 (character :tag "Mark"))
   :group 'dired-mark)
 
-;;;###autoload
 (defcustom dired-keep-marker-symlink ?Y
   "Controls marking of newly made symbolic links.
 If t, they are marked if and as the files linked to were marked.
@@ -152,7 +152,6 @@ If a character, new links are unconditionally marked with that character."
                 (character :tag "Mark"))
   :group 'dired-mark)
 
-;;;###autoload
 (defcustom dired-dwim-target nil
   "If non-nil, Dired tries to guess a default target directory.
 This means: if there is a dired buffer displayed in the next window,
@@ -162,7 +161,6 @@ The target is used in the prompt for file copy, rename etc."
   :type 'boolean
   :group 'dired)
 
-;;;###autoload
 (defcustom dired-copy-preserve-time t
   "If non-nil, Dired preserves the last-modified time in a file copy.
 \(This works on only some systems.)"
@@ -605,12 +603,8 @@ Don't use that together with FILTER."
            (if (next-read-file-uses-dialog-p)
                (read-directory-name (format "Dired %s(directory): " str)
                                     nil default-directory nil)
-             (let ((default (and buffer-file-name
-                                 (abbreviate-file-name buffer-file-name))))
-               (minibuffer-with-setup-hook
-                   (lambda () (setq minibuffer-default default))
-                 (read-file-name (format "Dired %s(directory): " str)
-                                 nil default-directory nil)))))))
+             (read-file-name (format "Dired %s(directory): " str)
+                             nil default-directory nil)))))
 
 ;; We want to switch to a more sophisticated version of
 ;; dired-read-dir-and-switches like the following, if there is a way
@@ -665,6 +659,15 @@ Don't use that together with FILTER."
 ;;             (read-file-name (format "Dired %s(directory): " str)
 ;;                             nil default-directory nil))))))))
 
+(defun dired-file-name-at-point ()
+  "Try to get a file name at point in the current dired buffer.
+This hook is inteneded to be put in `file-name-at-point-functions'."
+  (let ((filename (dired-get-filename nil t)))
+    (when filename
+      (if (file-directory-p filename)
+         (file-name-as-directory (abbreviate-file-name filename))
+       (abbreviate-file-name filename)))))
+
 ;;;###autoload (define-key ctl-x-map "d" 'dired)
 ;;;###autoload
 (defun dired (dirname &optional switches)
@@ -753,6 +756,24 @@ for a remote directory.  This feature is used by Auto Revert Mode."
         buffer-read-only
         (dired-directory-changed-p dirname))))
 
+;;;###autoload
+(defcustom dired-auto-revert-buffer nil
+  "Automatically revert dired buffer on revisiting.
+If t, revisiting an existing dired buffer automatically reverts it.
+If its value is a function, call this function with the directory
+name as single argument and revert the buffer if it returns non-nil.
+Otherwise, a message offering to revert the changed dired buffer
+is displayed.
+Note that this is not the same as `auto-revert-mode' that
+periodically reverts at specified time intervals."
+  :type '(choice
+          (const :tag "Don't revert" nil)
+          (const :tag "Always revert visited dired buffer" t)
+          (const :tag "Revert changed dired buffer" dired-directory-changed-p)
+          (function :tag "Predicate function"))
+  :group 'dired
+  :version "23.2")
+
 (defun dired-internal-noselect (dir-or-list &optional switches mode)
   ;; If there is an existing dired buffer for DIRNAME, just leave
   ;; buffer as it is (don't even call dired-revert).
@@ -780,6 +801,14 @@ for a remote directory.  This feature is used by Auto Revert Mode."
               (setq dired-directory dir-or-list)
               ;; this calls dired-revert
               (dired-sort-other switches))
+             ;; Always revert regardless of whether it has changed or not.
+             ((eq dired-auto-revert-buffer t)
+              (revert-buffer))
+             ;; Revert when predicate function returns non-nil.
+             ((functionp dired-auto-revert-buffer)
+              (when (funcall dired-auto-revert-buffer dirname)
+                (revert-buffer)
+                (message "Changed directory automatically updated")))
              ;; If directory has changed on disk, offer to revert.
              ((when (dired-directory-changed-p dirname)
                 (message "%s"
@@ -825,8 +854,7 @@ for a remote directory.  This feature is used by Auto Revert Mode."
     (while blist
       (if (null (buffer-name (cdr (car blist))))
          (setq blist (cdr blist))
-       (save-excursion
-         (set-buffer (cdr (car blist)))
+       (with-current-buffer (cdr (car blist))
          (if (and (eq major-mode mode)
                   dired-directory  ;; nil during find-alternate-file
                   (equal dirname
@@ -937,7 +965,7 @@ BEG..END is the line where the file info is located."
   ;; spaces there (and within the filename as well, of course).
   (save-excursion
     (let (file file-col other other-col)
-      ;; Check the there is indeed a file, and that there is anoter adjacent
+      ;; Check that there is indeed a file, and that there is anoter adjacent
       ;; file with which to align, and that additional spaces are needed to
       ;; align the filenames.
       (when (and (setq file (progn (goto-char beg)
@@ -1032,7 +1060,14 @@ If HDR is non-nil, insert a header line with the directory name."
   (let ((opoint (point))
        (process-environment (copy-sequence process-environment))
        end)
-    (if (or dired-use-ls-dired (file-remote-p dir))
+    (if (or (if (eq dired-use-ls-dired 'unspecified)
+               ;; Check whether "ls --dired" gives exit code 0, and
+               ;; save the answer in `dired-use-ls-dired'.
+               (setq dired-use-ls-dired
+                     (eq (call-process insert-directory-program nil nil nil "--dired")
+                         0))
+             dired-use-ls-dired)
+           (file-remote-p dir))
        (setq switches (concat "--dired " switches)))
     ;; We used to specify the C locale here, to force English month names;
     ;; but this should not be necessary any more,
@@ -1110,8 +1145,7 @@ Should not fail even on completely garbaged buffers.
 Preserves old cursor, marks/flags, hidden-p."
   (widen)                              ; just in case user narrowed
   (let ((modflag (buffer-modified-p))
-       (opoint (point))
-       (ofile (dired-get-filename nil t))
+       (positions (dired-save-positions))
        (mark-alist nil)                ; save marked files
        (hidden-subdirs (dired-remember-hidden))
        (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
@@ -1121,8 +1155,12 @@ Preserves old cursor, marks/flags, hidden-p."
     (setq mark-alist;; only after dired-remember-hidden since this unhides:
          (dired-remember-marks (point-min) (point-max)))
     ;; treat top level dir extra (it may contain wildcards)
-    (dired-uncache
-     (if (consp dired-directory) (car dired-directory) dired-directory))
+    (if (not (consp dired-directory))
+       (dired-uncache dired-directory)
+      (dired-uncache (car dired-directory))
+      (dolist (dir (cdr dired-directory))
+       (if (file-name-absolute-p dir)
+           (dired-uncache dir))))
     ;; Run dired-after-readin-hook just once, below.
     (let ((dired-after-readin-hook nil))
       (dired-readin)
@@ -1131,9 +1169,7 @@ Preserves old cursor, marks/flags, hidden-p."
     ;; ... run the hook for the whole buffer, and only after markers
     ;; have been reinserted (else omitting in dired-x would omit marked files)
     (run-hooks 'dired-after-readin-hook)       ; no need to narrow
-    (or (and ofile (dired-goto-file ofile)) ; move cursor to where it
-       (goto-char opoint))             ; was before
-    (dired-move-to-filename)
+    (dired-restore-positions positions)
     (save-excursion                    ; hide subdirs that were hidden
       (dolist (dir hidden-subdirs)
        (if (dired-goto-subdir dir)
@@ -1147,6 +1183,42 @@ Preserves old cursor, marks/flags, hidden-p."
 ;; Subroutines of dired-revert
 ;; Some of these are also used when inserting subdirs.
 
+(defun dired-save-positions ()
+  "Return current positions in the buffer and all windows with this directory.
+The positions have the form (BUFFER-POSITION WINDOW-POSITIONS).
+
+BUFFER-POSITION is the point position in the current dired buffer.
+It has the form (BUFFER DIRED-FILENAME BUFFER-POINT).
+
+WINDOW-POSITIONS are current positions in all windows displaying
+this dired buffer.  The window positions have the form (WINDOW
+DIRED-FILENAME WINDOW-POINT)."
+  (list
+   (list (current-buffer) (dired-get-filename nil t) (point))
+   (mapcar (lambda (w)
+            (list w
+                  (with-selected-window w
+                    (dired-get-filename nil t))
+                  (window-point w)))
+          (get-buffer-window-list nil 0 t))))
+
+(defun dired-restore-positions (positions)
+  "Restore POSITIONS saved with `dired-save-positions'."
+  (let* ((buf-file-pos (nth 0 positions))
+        (buffer (nth 0 buf-file-pos)))
+    (unless (and (nth 1 buf-file-pos)
+                (dired-goto-file (nth 1 buf-file-pos)))
+      (goto-char (nth 2 buf-file-pos))
+      (dired-move-to-filename))
+    (dolist (win-file-pos (nth 1 positions))
+      ;; Ensure that window still displays the original buffer.
+      (when (eq (window-buffer (nth 0 win-file-pos)) buffer)
+       (with-selected-window (nth 0 win-file-pos)
+         (unless (and (nth 1 win-file-pos)
+                      (dired-goto-file (nth 1 win-file-pos)))
+           (goto-char (nth 2 win-file-pos))
+           (dired-move-to-filename)))))))
+
 (defun dired-remember-marks (beg end)
   "Return alist of files and their marks, from BEG to END."
   (if selective-display                        ; must unhide to make this work.
@@ -1319,10 +1391,8 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
     (define-key map ">" 'dired-next-dirline)
     (define-key map "^" 'dired-up-directory)
     (define-key map " "  'dired-next-line)
-    (define-key map "\C-n" 'dired-next-line)
-    (define-key map "\C-p" 'dired-previous-line)
-    (define-key map [down] 'dired-next-line)
-    (define-key map [up] 'dired-previous-line)
+    (define-key map [remap next-line] 'dired-next-line)
+    (define-key map [remap previous-line] 'dired-previous-line)
     ;; hiding
     (define-key map "$" 'dired-hide-subdir)
     (define-key map "\M-$" 'dired-hide-all)
@@ -1332,7 +1402,7 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
     (define-key map (kbd "M-s f C-s")   'dired-isearch-filenames)
     (define-key map (kbd "M-s f M-C-s") 'dired-isearch-filenames-regexp)
     ;; misc
-    (define-key map "\C-x\C-q" 'dired-toggle-read-only)
+    (define-key map [remap toggle-read-only] 'dired-toggle-read-only)
     (define-key map "?" 'dired-summary)
     (define-key map "\177" 'dired-unmark-backward)
     (define-key map [remap undo] 'dired-undo)
@@ -1348,7 +1418,7 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
     (define-key map "\C-t." 'image-dired-display-thumb)
     (define-key map "\C-tc" 'image-dired-dired-comment-files)
     (define-key map "\C-tf" 'image-dired-mark-tagged-files)
-    (define-key map "\C-t\C-t" 'image-dired-dired-insert-marked-thumbs)
+    (define-key map "\C-t\C-t" 'image-dired-dired-toggle-marked-thumbs)
     (define-key map "\C-te" 'image-dired-dired-edit-comment-and-tags)
     ;; encryption and decryption (epa-dired)
     (define-key map ":d" 'epa-dired-do-decrypt)
@@ -1395,34 +1465,12 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
                  :help "Move to previous directory-file line"))
     (define-key map [menu-bar subdir insert]
       '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
-                 :help "Insert contents of subdirectory"))
-
+                 :help "Insert contents of subdirectory"
+                 :enable (let ((f (dired-get-filename nil t)))
+                           (and f (file-directory-p f)))))
     (define-key map [menu-bar immediate]
       (cons "Immediate" (make-sparse-keymap "Immediate")))
 
-    (define-key map
-      [menu-bar immediate epa-dired-do-decrypt]
-      '(menu-item "Decrypt" epa-dired-do-decrypt
-                 :help "Decrypt file at cursor"))
-
-    (define-key map
-      [menu-bar immediate epa-dired-do-verify]
-      '(menu-item "Verify" epa-dired-do-verify
-                 :help "Verify digital signature of file at cursor"))
-
-    (define-key map
-      [menu-bar immediate epa-dired-do-sign]
-      '(menu-item "Sign" epa-dired-do-sign
-                 :help "Create digital signature of file at cursor"))
-
-    (define-key map
-      [menu-bar immediate epa-dired-do-encrypt]
-      '(menu-item "Encrypt" epa-dired-do-encrypt
-                 :help "Encrypt file at cursor"))
-
-    (define-key map [menu-bar immediate dashes-4]
-      '("--"))
-
     (define-key map
       [menu-bar immediate image-dired-dired-display-external]
       '(menu-item "Display Image Externally" image-dired-dired-display-external
@@ -1432,9 +1480,6 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
       '(menu-item "Display Image" image-dired-dired-display-image
                   :help "Display sized image in a separate window"))
 
-    (define-key map [menu-bar immediate dashes-4]
-      '("--"))
-
     (define-key map [menu-bar immediate revert-buffer]
       '(menu-item "Refresh" revert-buffer
                  :help "Update contents of shown directories"))
@@ -1593,6 +1638,29 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
       '(menu-item "Display image thumbnails" image-dired-display-thumbs
                   :help "Display image thumbnails for current or marked image files"))
 
+    (define-key map [menu-bar operate dashes-4]
+      '("--"))
+
+    (define-key map
+      [menu-bar operate epa-dired-do-decrypt]
+      '(menu-item "Decrypt" epa-dired-do-decrypt
+                 :help "Decrypt file at cursor"))
+
+    (define-key map
+      [menu-bar operate epa-dired-do-verify]
+      '(menu-item "Verify" epa-dired-do-verify
+                 :help "Verify digital signature of file at cursor"))
+
+    (define-key map
+      [menu-bar operate epa-dired-do-sign]
+      '(menu-item "Sign" epa-dired-do-sign
+                 :help "Create digital signature of file at cursor"))
+
+    (define-key map
+      [menu-bar operate epa-dired-do-encrypt]
+      '(menu-item "Encrypt" epa-dired-do-encrypt
+                 :help "Encrypt file at cursor"))
+
     (define-key map [menu-bar operate dashes-3]
       '("--"))
 
@@ -1750,10 +1818,10 @@ Keybindings:
   (set (make-local-variable 'dired-directory)
        (or dirname default-directory))
   ;; list-buffers uses this to display the dir being edited in this buffer.
-  (set (make-local-variable 'list-buffers-directory)
-       (expand-file-name (if (listp dired-directory)
-                            (car dired-directory)
-                          dired-directory)))
+  (setq list-buffers-directory
+       (expand-file-name (if (listp dired-directory)
+                             (car dired-directory)
+                           dired-directory)))
   (set (make-local-variable 'dired-actual-switches)
        (or switches dired-listing-switches))
   (set (make-local-variable 'font-lock-defaults)
@@ -1765,6 +1833,7 @@ Keybindings:
   (when (featurep 'dnd)
     (set (make-local-variable 'dnd-protocol-alist)
         (append dired-dnd-protocol-alist dnd-protocol-alist)))
+  (add-hook 'file-name-at-point-functions 'dired-file-name-at-point nil t)
   (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t)
   (run-mode-hooks 'dired-mode-hook))
 \f
@@ -1952,6 +2021,14 @@ Otherwise, an error occurs in these cases."
           ;; with quotation marks in their names.
          (while (string-match "\\(?:[^\\]\\|\\`\\)\\(\"\\)" file)
            (setq file (replace-match "\\\"" nil t file 1)))
+         
+         (when (eq system-type 'windows-nt)
+           (save-match-data
+             (let ((start 0))
+               (while (string-match "\\\\" file start)
+                 (aset file (match-beginning 0) ?/)
+                 (setq start (match-end 0))))))
+
           (setq file (read (concat "\"" file "\"")))
          ;; The above `read' will return a unibyte string if FILE
          ;; contains eight-bit-control/graphic characters.
@@ -2079,7 +2156,7 @@ Return the position of the beginning of the filename, or nil if none found."
       ;; case-fold-search is nil now, so we can test for capital F:
       (setq used-F (string-match "F" dired-actual-switches)
            opoint (point)
-           eol (save-excursion (end-of-line) (point))
+           eol (line-end-position)
            hidden (and selective-display
                        (save-excursion (search-forward "\r" eol t))))
       (if hidden
@@ -2167,31 +2244,33 @@ You can then feed the file name(s) to other commands with \\[yank]."
 ;; Keeping Dired buffers in sync with the filesystem and with each other
 
 (defun dired-buffers-for-dir (dir &optional file)
-;; Return a list of buffers that dired DIR (top level or in-situ subdir).
+;; Return a list of buffers for DIR (top level or in-situ subdir).
 ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
 ;; matches FILE.
 ;; The list is in reverse order of buffer creation, most recent last.
 ;; As a side effect, killed dired buffers for DIR are removed from
 ;; dired-buffers.
   (setq dir (file-name-as-directory dir))
-  (let ((alist dired-buffers) result elt buf)
-    (while alist
-      (setq elt (car alist)
-           buf (cdr elt))
-      (if (buffer-name buf)
-         (if (dired-in-this-tree dir (car elt))
-             (with-current-buffer buf
-               (and (assoc dir dired-subdir-alist)
-                    (or (null file)
-                        (let ((wildcards
-                               (file-name-nondirectory dired-directory)))
-                          (or (= 0 (length wildcards))
-                              (string-match (dired-glob-regexp wildcards)
-                                            file))))
-                    (setq result (cons buf result)))))
-       ;; else buffer is killed - clean up:
+  (let (result buf)
+    (dolist (elt dired-buffers)
+      (setq buf (cdr elt))
+      (cond
+       ((null (buffer-name buf))
+       ;; Buffer is killed - clean up:
        (setq dired-buffers (delq elt dired-buffers)))
-      (setq alist (cdr alist)))
+       ((dired-in-this-tree dir (car elt))
+       (with-current-buffer buf
+         (and (assoc dir dired-subdir-alist)
+              (or (null file)
+                  (if (stringp dired-directory)
+                      (let ((wildcards (file-name-nondirectory
+                                        dired-directory)))
+                        (or (= 0 (length wildcards))
+                            (string-match (dired-glob-regexp wildcards)
+                                          file)))
+                    (member (expand-file-name file dir)
+                            (cdr dired-directory))))
+              (setq result (cons buf result)))))))
     result))
 
 (defun dired-glob-regexp (pattern)
@@ -2373,7 +2452,7 @@ instead of `dired-actual-switches'."
                               (goto-char (match-beginning 0))
                               (beginning-of-line)
                               (point-marker)))))
-      (if (and (> count 1) (interactive-p))
+      (if (and (> count 1) (called-interactively-p 'interactive))
          (message "Buffer includes %d directories" count)))
     ;; We don't need to sort it because it is in buffer order per
     ;; constructionem.  Return new alist:
@@ -2519,30 +2598,30 @@ Anything else means ask for each directory."
 ;; Delete file, possibly delete a directory and all its files.
 ;; This function is usefull outside of dired.  One could change it's name
 ;; to e.g. recursive-delete-file and put it somewhere else.
-(defun dired-delete-file (file &optional recursive) "\
+(defun dired-delete-file (file &optional recursive trash) "\
 Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
 RECURSIVE determines what to do with a non-empty directory.  If RECURSIVE is:
 nil, do not delete.
 `always', delete recursively without asking.
 `top', ask for each directory at top level.
 Anything else, ask for each sub-directory."
-  (let (files)
-     ;; This test is equivalent to
-     ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
-     ;; but more efficient
-    (if (not (eq t (car (file-attributes file))))
-       (delete-file file)
-      (when (and recursive
-              (setq files
-                    (directory-files file t dired-re-no-dot)) ; Not empty.
-              (or (eq recursive 'always)
-                  (yes-or-no-p (format "Recursive delete of %s? "
-                                       (dired-make-relative file)))))
+  ;; This test is equivalent to
+  ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
+  ;; but more efficient
+  (if (not (eq t (car (file-attributes file))))
+      (delete-file file trash)
+    (if (and recursive
+            (directory-files file t dired-re-no-dot) ; Not empty.
+            (or (eq recursive 'always)
+                (yes-or-no-p (format "Recursively %s %s? "
+                                     (if (and trash
+                                              delete-by-moving-to-trash)
+                                         "trash"
+                                       "delete")
+                                     (dired-make-relative file)))))
        (if (eq recursive 'top) (setq recursive 'always)) ; Don't ask again.
-       (while files            ; Recursively delete (possibly asking).
-           (dired-delete-file (car files) recursive)
-           (setq files (cdr files))))
-      (delete-directory file))))
+      (setq recursive nil))
+    (delete-directory file recursive trash)))
 
 (defun dired-do-flagged-delete (&optional nomessage)
   "In Dired, delete the files flagged for deletion.
@@ -2560,7 +2639,7 @@ non-empty directories is allowed."
         ;; this can't move point since ARG is nil
         (dired-map-over-marks (cons (dired-get-filename) (point))
                               nil)
-        nil)
+        nil t)
       (or nomessage
          (message "(No deletions requested)")))))
 
@@ -2575,11 +2654,11 @@ non-empty directories is allowed."
    ;; this may move point if ARG is an integer
    (dired-map-over-marks (cons (dired-get-filename) (point))
                         arg)
-   arg))
+   arg t))
 
 (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
 
-(defun dired-internal-do-deletions (l arg)
+(defun dired-internal-do-deletions (l arg &optional trash)
   ;; L is an alist of files to delete, with their buffer positions.
   ;; ARG is the prefix arg.
   ;; Filenames are absolute.
@@ -2588,14 +2667,21 @@ non-empty directories is allowed."
   ;; lines still to be changed, so the (point) values in L stay valid.
   ;; Also, for subdirs in natural order, a subdir's files are deleted
   ;; before the subdir itself - the other way around would not work.
-  (let ((files (mapcar (function car) l))
-       (count (length l))
-       (succ 0))
+  (let* ((files (mapcar (function car) l))
+        (count (length l))
+        (succ 0)
+        (trashing (and trash delete-by-moving-to-trash))
+        (progress-reporter
+         (make-progress-reporter
+          (if trashing "Trashing..." "Deleting...")
+          succ count)))
     ;; canonicalize file list for pop up
     (setq files (nreverse (mapcar (function dired-make-relative) files)))
     (if (dired-mark-pop-up
         " *Deletions*" 'delete files dired-deletion-confirmer
-        (format "Delete %s " (dired-mark-prompt arg files)))
+        (format "%s %s "
+                (if trashing "Trash" "Delete")
+                (dired-mark-prompt arg files)))
        (save-excursion
          (let (failures);; files better be in reverse order for this loop!
            (while l
@@ -2603,10 +2689,10 @@ non-empty directories is allowed."
              (let ((inhibit-read-only t))
                (condition-case err
                    (let ((fn (car (car l))))
-                     (dired-delete-file fn dired-recursive-deletes)
+                     (dired-delete-file fn dired-recursive-deletes trash)
                      ;; if we get here, removing worked
                      (setq succ (1+ succ))
-                     (message "%s of %s deletions" succ count)
+                     (progress-reporter-update progress-reporter succ)
                      (dired-fun-in-all-buffers
                       (file-name-directory fn) (file-name-nondirectory fn)
                       (function dired-delete-entry) fn))
@@ -2615,7 +2701,7 @@ non-empty directories is allowed."
                   (setq failures (cons (car (car l)) failures)))))
              (setq l (cdr l)))
            (if (not failures)
-               (message "%d deletion%s done" count (dired-plural-s count))
+               (progress-reporter-done progress-reporter)
              (dired-log-summary
               (format "%d of %d deletion%s failed"
                       (length failures) count
@@ -2689,7 +2775,8 @@ name, or the marker and a count of marked files."
                    ;; that's possible.  (Bug#1806)
                    (split-window-vertically))
               ;; Otherwise, try to split WINDOW sensibly.
-              (split-window-sensibly window)))))
+              (split-window-sensibly window))))
+       pop-up-frames)
     (pop-to-buffer (get-buffer-create buf)))
   ;; If dired-shrink-to-fit is t, make its window fit its contents.
   (when dired-shrink-to-fit
@@ -2698,17 +2785,19 @@ name, or the marker and a count of marked files."
     (fit-window-to-buffer (get-buffer-window buf) nil 1)))
 
 (defcustom dired-no-confirm nil
-  "A list of symbols for commands Dired should not confirm.
+  "A list of symbols for commands Dired should not confirm, or t.
 Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
 `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink',
-`touch' and `uncompress'."
+`touch' and `uncompress'.
+If t, confirmation is never needed."
   :group 'dired
-  :type '(set (const byte-compile) (const chgrp)
-             (const chmod) (const chown) (const compress)
-             (const copy) (const delete) (const hardlink)
-             (const load) (const move) (const print)
-             (const shell) (const symlink) (const touch)
-             (const uncompress)))
+  :type '(choice (const :tag "Confirmation never needed" t)
+                (set (const byte-compile) (const chgrp)
+                     (const chmod) (const chown) (const compress)
+                     (const copy) (const delete) (const hardlink)
+                     (const load) (const move) (const print)
+                     (const shell) (const symlink) (const touch)
+                     (const uncompress))))
 
 (defun dired-mark-pop-up (bufname op-symbol files function &rest args)
   "Return FUNCTION's result on ARGS after showing which files are marked.
@@ -3179,12 +3268,16 @@ variable `dired-listing-switches'.  To temporarily override the listing
 format, use `\\[universal-argument] \\[dired]'.")
 
 (defvar dired-sort-by-date-regexp
-  (concat "^-[^" dired-ls-sorting-switches
-         "]*t[^" dired-ls-sorting-switches "]*$")
+  (concat "\\(\\`\\| \\)-[^- ]*t"
+         ;; `dired-ls-sorting-switches' after -t overrides -t.
+         "[^ " dired-ls-sorting-switches "]*"
+         "\\(\\(\\`\\| +\\)\\(--[^ ]+\\|-[^- t"
+         dired-ls-sorting-switches "]+\\)\\)* *$")
   "Regexp recognized by Dired to set `by date' mode.")
 
 (defvar dired-sort-by-name-regexp
-  (concat "^-[^t" dired-ls-sorting-switches "]+$")
+  (concat "\\`\\(\\(\\`\\| +\\)\\(--[^ ]+\\|"
+         "-[^- t" dired-ls-sorting-switches "]+\\)\\)* *$")
   "Regexp recognized by Dired to set `by name' mode.")
 
 (defvar dired-sort-inhibit nil
@@ -3210,8 +3303,8 @@ The idea is to set this buffer-locally in special dired buffers.")
     (force-mode-line-update)))
 
 (defun dired-sort-toggle-or-edit (&optional arg)
-  "Toggle between sort by date/name and refresh the dired buffer.
-With a prefix argument you can edit the current listing switches instead."
+  "Toggle sorting by date, and refresh the Dired buffer.
+With a prefix argument, edit the current listing switches instead."
   (interactive "P")
   (when dired-sort-inhibit
     (error "Cannot sort this dired buffer"))
@@ -3222,24 +3315,24 @@ With a prefix argument you can edit the current listing switches instead."
 
 (defun dired-sort-toggle ()
   ;; Toggle between sort by date/name.  Reverts the buffer.
-  (setq dired-actual-switches
-       (let (case-fold-search)
-         (if (string-match " " dired-actual-switches)
-             ;; New toggle scheme: add/remove a trailing " -t"
-             (if (string-match " -t\\'" dired-actual-switches)
-                 (substring dired-actual-switches 0 (match-beginning 0))
-               (concat dired-actual-switches " -t"))
-           ;; old toggle scheme: look for some 't' switch and add/remove it
-           (concat
-            "-l"
-            (dired-replace-in-string (concat "[-lt"
-                                             dired-ls-sorting-switches "]")
-                                     ""
-                                     dired-actual-switches)
-            (if (string-match (concat "[t" dired-ls-sorting-switches "]")
-                              dired-actual-switches)
-                ""
-              "t")))))
+  (let ((sorting-by-date (string-match dired-sort-by-date-regexp
+                                      dired-actual-switches))
+       ;; Regexp for finding (possibly embedded) -t switches.
+       (switch-regexp "\\(\\`\\| \\)-\\([a-su-zA-Z]*\\)\\(t\\)\\([^ ]*\\)")
+       case-fold-search)
+    ;; Remove the -t switch.
+    (while (string-match switch-regexp dired-actual-switches)
+      (if (and (equal (match-string 2 dired-actual-switches) "")
+              (equal (match-string 4 dired-actual-switches) ""))
+         ;; Remove a stand-alone -t switch.
+         (setq dired-actual-switches
+               (replace-match "" t t dired-actual-switches))
+       ;; Remove a switch of the form -XtY for some X and Y.
+       (setq dired-actual-switches
+             (replace-match "" t t dired-actual-switches 3))))
+    ;; Now, if we weren't sorting by date before, add the -t switch.
+    (unless sorting-by-date
+      (setq dired-actual-switches (concat dired-actual-switches " -t"))))
   (dired-sort-set-modeline)
   (revert-buffer))
 
@@ -3321,7 +3414,7 @@ Anything else means ask for each directory."
   (message-box
    "Dired recursive copies are currently disabled.\nSee the variable `dired-recursive-copies'."))
 
-(declare-function x-popup-menu "xmenu.c" (position menu))
+(declare-function x-popup-menu "menu.c" (position menu))
 
 (defun dired-dnd-do-ask-action (uri)
   ;; No need to get actions and descriptions from the source,
@@ -3465,7 +3558,7 @@ Ask means pop up a menu for the user to select one of copy, move or link."
 ;;;;;;  dired-run-shell-command dired-do-shell-command dired-do-async-shell-command
 ;;;;;;  dired-clean-directory dired-do-print dired-do-touch dired-do-chown
 ;;;;;;  dired-do-chgrp dired-do-chmod dired-compare-directories dired-backup-diff
-;;;;;;  dired-diff) "dired-aux" "dired-aux.el" "19cd0d559197e5587fe27e1a81fb2644")
+;;;;;;  dired-diff) "dired-aux" "dired-aux.el" "2e8658304f56098052e312d01c8763a2")
 ;;; Generated autoloads from dired-aux.el
 
 (autoload 'dired-diff "dired-aux" "\
@@ -3865,7 +3958,7 @@ Hide all subdirectories, leaving only their header lines.
 If there is already something hidden, make everything visible again.
 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory.
 
-\(fn ARG)" t nil)
+\(fn &optional IGNORED)" t nil)
 
 (autoload 'dired-isearch-filenames-setup "dired-aux" "\
 Set up isearch to search in Dired file names.
@@ -3918,7 +4011,7 @@ true then the type of the file linked to by FILE is printed instead.
 ;;;***
 \f
 ;;;### (autoloads (dired-do-relsymlink dired-jump) "dired-x" "dired-x.el"
-;;;;;;  "1a0298749959c80c24c73b8bec5f1f74")
+;;;;;;  "27c312d6d5d40d8cb4ef8d62e30d5f4a")
 ;;; Generated autoloads from dired-x.el
 
 (autoload 'dired-jump "dired-x" "\
@@ -3927,8 +4020,11 @@ If in a file, dired the current directory and move to file's line.
 If in Dired already, pop up a level and goto old directory's line.
 In case the proper dired file line cannot be found, refresh the dired
 buffer and try again.
+When OTHER-WINDOW is non-nil, jump to dired buffer in other window.
+Interactively with prefix argument, read FILE-NAME and
+move to its line in dired.
 
-\(fn &optional OTHER-WINDOW)" t nil)
+\(fn &optional OTHER-WINDOW FILE-NAME)" t nil)
 
 (autoload 'dired-do-relsymlink "dired-x" "\
 Relative symlink all marked (or next ARG) files into a directory.
@@ -3953,5 +4049,4 @@ For absolute symlinks, use \\[dired-do-symlink].
 
 (run-hooks 'dired-load-hook)           ; for your customizations
 
-;; arch-tag: e1af7a8f-691c-41a0-aac1-ddd4d3c87517
 ;;; dired.el ends here