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

From: Fadi Moukayed <[email-masked]>
Date: Thu, 15 Jun 2023 13:10:25 +0200

Hi Eli,

Thanks a lot. Just tested your patch a few moments ago (with
dired-kill-when-opening-new-dired-bufferset to t) and it fixes the issue.

I also tested the default behavior (with
dired-kill-when-opening-new-dired-buffer
set to nil), and everything works as expected in that case as well.

Cheers.

Am Do., 15. Juni 2023 um 12:48 Uhr schrieb Eli Zaretskii <[email-masked]>:
>
> > 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)