From: Lars Ingebrigtsen Date: Tue, 13 Jul 2021 17:49:50 +0000 (+0200) Subject: Add new user option to avoid piling on Dired buffers X-Git-Tag: emacs-28.0.90~1888^2 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=5afe27624f7168713611dc9c24043091f8f820b6;p=emacs.git Add new user option to avoid piling on Dired buffers * doc/emacs/dired.texi (Dired Visiting): Document it. * lisp/dired.el (dired-kill-when-opening-new-dired-buffer): New user option (bug#20598). (dired-up-directory, dired-find-file): Use it. (dired--find-possibly-alternative-file): New convenience command to respect the user option. --- diff --git a/doc/emacs/dired.texi b/doc/emacs/dired.texi index 36257030c88..3fbaf8bab7a 100644 --- a/doc/emacs/dired.texi +++ b/doc/emacs/dired.texi @@ -457,6 +457,15 @@ Visit the parent directory of the current directory for @file{..} and typing @kbd{f} there. @end table +@defopt dired-kill-when-opening-new-dired-buffer + When visiting a new sub-directory in Dired, Emacs will (by default) +open a new buffer to display this new directory, and leave the old +Dired buffer as is. If this user option is non-@code{nil}, the old +Dired buffer will be killed after selecting the new directory. This +means that if you're traversing a directory structure in Dired, you +won't end up with more than a single Dired buffer. +@end defopt + @node Marks vs Flags @section Dired Marks vs.@: Flags diff --git a/etc/NEWS b/etc/NEWS index 923cfcc4722..75be112fd31 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -778,6 +778,11 @@ time zones will use a form like "+0100" instead of "CET". ** Dired ++++ +*** New user option 'dired-kill-when-opening-new-dired-buffer'. +If non-nil, Dired will kill the current buffer when selecting a new +directory to display. + --- *** Behavior change on 'dired-clean-confirm-killing-deleted-buffers'. Previously, if 'dired-clean-up-buffers-too' was non-nil, and diff --git a/lisp/dired.el b/lisp/dired.el index fb353a92e45..da803feaa18 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -356,6 +356,11 @@ is anywhere on its Dired line, except the beginning of the line." :group 'dired :version "28.1") +(defcustom dired-kill-when-opening-new-dired-buffer nil + "If non-nil, kill the current buffer when selecting a new directory." + :type 'boolean + :version "28.1") + ;;; Internal variables @@ -2379,7 +2384,7 @@ directory in another window." (progn (if other-window (dired-other-window up) - (dired up)) + (dired--find-possibly-alternative-file up)) (dired-goto-file dir))))) (defun dired-get-file-for-visit () @@ -2403,7 +2408,16 @@ directory in another window." (defun dired-find-file () "In Dired, visit the file or directory named on this line." (interactive) - (dired--find-file #'find-file (dired-get-file-for-visit))) + (dired--find-possibly-alternative-file (dired-get-file-for-visit))) + +(defun dired--find-possibly-alternative-file (file) + "Find FILE, but respect `dired-kill-when-opening-new-dired-buffer'." + (if (and dired-kill-when-opening-new-dired-buffer + (file-directory-p file)) + (progn + (set-buffer-modified-p nil) + (dired--find-file #'find-alternate-file file)) + (dired--find-file #'find-file file))) (defun dired--find-file (find-file-function file) "Call FIND-FILE-FUNCTION on FILE, but bind some relevant variables."