Commands which ask for a destination directory, such as those which
copy and rename files or create links for them, try to guess the default
target directory for the operation. Normally, they suggest the Dired
-buffer's default directory, but if the variable @code{dired-dwim-target}
-is non-@code{nil}, and if there is another Dired buffer displayed in one
-of the most recently used windows, that other buffer's directory is
-suggested instead.
+buffer's default directory, but if the option @code{dired-dwim-target}
+is non-@code{nil}, and if there is another Dired buffer displayed in
+some window, that other buffer's directory is suggested instead.
+You can customize @code{dired-dwim-target} to prefer either the next
+window with a Dired buffer, or the most recently used window with
+a Dired buffer.
Here are the file-manipulating Dired commands that operate on files.
directories in the destination.
+++
-*** The non-nil value of 'dired-dwim-target' uses one of the most recently
-visited windows with a Dired buffer instead of the next window.
+*** 'dired-dwim-target' can be customized to prefer either the next window,
+or one of the most recently visited windows with a Dired buffer.
*** When the new user option 'dired-vc-rename-file' is non-nil,
Dired performs file renaming using underlying version control system.
(format prompt (dired-mark-prompt arg files)) dir default))
(defun dired-dwim-target-directories ()
+ (cond ((functionp dired-dwim-target)
+ (funcall dired-dwim-target))
+ (dired-dwim-target
+ (dired-dwim-target-next))))
+
+(defun dired-dwim-target-next ()
+ ;; Return directories from all next visible windows with dired-mode buffers.
+ (mapcan (lambda (w)
+ (with-current-buffer (window-buffer w)
+ (when (eq major-mode 'dired-mode)
+ (list (dired-current-directory)))))
+ (delq (selected-window) (window-list-1
+ (next-window nil 'nomini 'visible)
+ 'nomini 'visible))))
+
+(defun dired-dwim-target-recent ()
;; Return directories from all visible windows with dired-mode buffers
;; ordered by most-recently-used.
(mapcar #'cdr (sort (mapcan (lambda (w)
(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 one of the most
-recently selected windows, use its current directory, instead of this
-Dired buffer's current directory.
+This means: if there is a Dired buffer displayed in some window,
+use its current directory, instead of this Dired buffer's
+current directory.
+
+You can customize it to prefer either the next window with a Dired buffer,
+or the most recently used window with a Dired buffer.
The target is used in the prompt for file copy, rename etc."
- :type 'boolean
+ :type '(choice
+ (const :tag "No guess" nil)
+ (function-item :tag "Prefer next windows"
+ dired-dwim-target-next)
+ (function-item :tag "Prefer most recently used windows"
+ dired-dwim-target-recent)
+ (function :tag "Your function")
+ (other :tag "Try to guess" t))
:group 'dired)
(defcustom dired-copy-preserve-time t