Subject: Re: bug#64079: 28.2; dired-mouse-find-file does not respect dired-kill-when-opening-new-dired-buffer

From: Eli Zaretskii <[email-masked]>
Date: Thu, 15 Jun 2023 13:48:45 +0300

> From: Fadi Moukayed <[email-masked]>
> Date: Thu, 15 Jun 2023 10:54:27 +0200
> 
> 1. Start Emacs
> 2. Open a `dired' buffer, e.g. using C-x d
> 3. Use M-: and evaluate the expression `(define-key dired-mode-map [mouse-3]
> 'dired-mouse-find-file)'
> 4. Navigate a nested directory tree (e.g. a/b/c/d) in dired by right-clicking
> on the directory names.
> 
> Expected behavior:
> 
> Only a dired buffer for d is retained.
> 
> Observed behavior:
> 
> dired buffers a, b, c are created and retained in addition to d.
> 
> Hints:
> 
> `dired-mouse-find-file' needs to contain similar logic to
> `dired--find-possibly-alternative-file' for checking the customization
> variable, or needs to delegate to the latter function.

Thanks.  Does the patch below give good results?

diff --git a/lisp/dired.el b/lisp/dired.el
index 4a4ecc9..de87530 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2765,10 +2765,11 @@ dired-mouse-find-file
 The optional arguments FIND-FILE-FUNC and FIND-DIR-FUNC specify
 functions to visit the file and directory, respectively.  If
 omitted or nil, these arguments default to `find-file' and `dired',
-respectively."
+respectively.  If `dired-kill-when-opening-new-dired-buffer' is
+non-nil, FIND-DIR-FUNC defaults to `find-alternate-file' instead,
+so that the original Dired buffer is not kept."
   (interactive "e")
   (or find-file-func (setq find-file-func 'find-file))
-  (or find-dir-func (setq find-dir-func 'dired))
   (let (window pos file)
     (save-excursion
       (setq window (posn-window (event-end event))
@@ -2776,6 +2777,12 @@ dired-mouse-find-file
       (if (not (windowp window))
 	  (error "No file chosen"))
       (set-buffer (window-buffer window))
+      (unless find-dir-func
+        (setq find-dir-func
+              (if (and dired-kill-when-opening-new-dired-buffer
+                       (< (length (get-buffer-window-list)) 2))
+                  'find-alternate-file
+                'dired)))
       (goto-char pos)
       (setq file (dired-get-file-for-visit)))
     (if (file-directory-p file)