]> git.eshelyaron.com Git - emacs.git/commitdiff
dired-dwim-target uses most recently visited window instead of next window.
authorJuri Linkov <juri@linkov.net>
Sat, 26 Oct 2019 23:20:15 +0000 (02:20 +0300)
committerJuri Linkov <juri@linkov.net>
Sat, 26 Oct 2019 23:20:15 +0000 (02:20 +0300)
* doc/emacs/dired.texi (Operating on Files): Document behavior change.

* lisp/dired-aux.el (dired-dwim-target-directories): New function.
(dired-dwim-target-directory, dired-dwim-target-defaults): Use it
to get the most recently used window instead of the next window (bug#35385).

* lisp/dired.el (dired-dwim-target): Doc fix.

* test/lisp/dired-tests.el: Remove unnecessary require and pacify
byte-compiler.

doc/emacs/dired.texi
etc/NEWS
lisp/dired-aux.el
lisp/dired.el
test/lisp/dired-tests.el

index 4ada2a8df4f05b23823e6b0ee66a4086902c7129..c32255a86db95982578884ef0b187c9c716691de 100644 (file)
@@ -655,8 +655,9 @@ commands, use the same conventions to decide which files to work on.
 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 the
-next window, that other buffer's directory is suggested instead.
+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.
 
   Here are the file-manipulating Dired commands that operate on files.
 
index ba8749613298d859cdb38fce83dc4bf8c3edb9d8..bfcb7cf3255a49b5d14e221ff803850de78337a7 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -810,6 +810,15 @@ command itself, not how many files are marked in total.
 *** A new face, 'dired-special', is used to highlight sockets, named
 pipes, block devices and character devices.
 
++++
+*** The new user option 'dired-create-destination-dirs' controls whether
+'dired-do-copy' and 'dired-rename-file' should create non-existent
+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.
+
 ** Find-Dired
 
 *** New user option 'find-dired-refine-function'.
@@ -1367,13 +1376,6 @@ unescaping text.
 The maximum level is used by default; customize
 'font-lock-maximum-decoration' to tone down the decoration.
 
-** Dired
-
-+++
-*** The new user option 'dired-create-destination-dirs' controls whether
-'dired-do-copy' and 'dired-rename-file' should create non-existent
-directories in the destination.
-
 ** Help
 
 ---
index b3ff24436264b0d80aba1fe6c14365faaf810954..b1521ecf01871825656ef2936be29e5ebe9e3908 100644 (file)
@@ -1977,6 +1977,18 @@ Optional arg HOW-TO determines how to treat the target.
    #'read-file-name
    (format prompt (dired-mark-prompt arg files)) dir default))
 
+(defun dired-dwim-target-directories ()
+  ;; Return directories from all visible windows with dired-mode buffers
+  ;; ordered by most-recently-used.
+  (mapcar #'cdr (sort (mapcan (lambda (w)
+                                (with-current-buffer (window-buffer w)
+                                  (when (eq major-mode 'dired-mode)
+                                    (list (cons (window-use-time w)
+                                                (dired-current-directory))))))
+                              (delq (selected-window)
+                                    (window-list-1 nil 'nomini 'visible)))
+                      (lambda (a b) (> (car a) (car b))))))
+
 (defun dired-dwim-target-directory ()
   ;; Try to guess which target directory the user may want.
   ;; If there is a dired buffer displayed in one of the next windows,
@@ -1985,15 +1997,7 @@ Optional arg HOW-TO determines how to treat the target.
                       (dired-current-directory))))
     ;; non-dired buffer may want to profit from this function, e.g. vm-uudecode
     (if dired-dwim-target
-       (let* ((other-win (get-window-with-predicate
-                          (lambda (window)
-                            (with-current-buffer (window-buffer window)
-                              (eq major-mode 'dired-mode)))))
-              (other-dir (and other-win
-                              (with-current-buffer (window-buffer other-win)
-                                (and (eq major-mode 'dired-mode)
-                                     (dired-current-directory))))))
-         (or other-dir this-dir))
+       (or (car (dired-dwim-target-directories)) this-dir)
       this-dir)))
 
 (defun dired-dwim-target-defaults (fn-list target-dir)
@@ -2011,15 +2015,11 @@ Optional arg HOW-TO determines how to treat the target.
         (and (consp fn-list) (null (cdr fn-list)) (car fn-list)))
        (current-dir (and (eq major-mode 'dired-mode)
                          (dired-current-directory)))
-       dired-dirs)
-    ;; Get a list of directories of visible buffers in dired-mode.
-    (walk-windows (lambda (w)
-                   (with-current-buffer (window-buffer w)
-                     (and (eq major-mode 'dired-mode)
-                          (push (dired-current-directory) dired-dirs)))))
+       ;; Get a list of directories of visible buffers in dired-mode.
+       (dired-dirs (dired-dwim-target-directories)))
     ;; Force the current dir to be the first in the list.
     (setq dired-dirs
-         (delete-dups (delq nil (cons current-dir (nreverse dired-dirs)))))
+         (delete-dups (delq nil (cons current-dir dired-dirs))))
     ;; Remove the target dir (if specified) or the current dir from
     ;; default values, because it should be already in initial input.
     (setq dired-dirs (delete (or target-dir current-dir) dired-dirs))
index 1d085e010f63cd4dfd7ac4fdf7aaab8cc70cf59c..e50108fdb9e914ab456e36956e31384b0fbe3439 100644 (file)
@@ -185,9 +185,9 @@ If a character, new links are unconditionally marked with that character."
 
 (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, use its current directory, instead of this Dired buffer's
-current 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.
 
 The target is used in the prompt for file copy, rename etc."
   :type 'boolean
index 71ffcdd5458fca97fc02847776688019812490f3..c4728e7a067b317c73aa0543a535602ba33d8996 100644 (file)
@@ -20,7 +20,6 @@
 ;;; Code:
 (require 'ert)
 (require 'dired)
-(require 'nadvice)
 
 (ert-deftest dired-autoload ()
   "Tests to see whether dired-x has been autoloaded"
@@ -54,7 +53,7 @@
         (when (buffer-live-p buf) (kill-buffer buf)))
       (delete-directory dir 'recursive))))
 
-(defvar dired-dwim-target)
+(defvar dired-query)
 (ert-deftest dired-test-bug25609 ()
   "Test for https://debbugs.gnu.org/25609 ."
   (let* ((from (make-temp-file "foo" 'dir))