]> git.eshelyaron.com Git - emacs.git/commitdiff
Add new user option to avoid piling on Dired buffers
authorLars Ingebrigtsen <larsi@gnus.org>
Tue, 13 Jul 2021 17:49:50 +0000 (19:49 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Tue, 13 Jul 2021 17:49:50 +0000 (19:49 +0200)
* 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.

doc/emacs/dired.texi
etc/NEWS
lisp/dired.el

index 36257030c88a862d9ac7211c1398873864dbe324..3fbaf8bab7a6648a5ebce78cd7a7eadd0bb7bd85 100644 (file)
@@ -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
 
index 923cfcc47228918c72fd38c049cf1caad13c84b4..75be112fd315120bf498ddd3bf380d7484708c8b 100644 (file)
--- 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
index fb353a92e4505de229e52f6a38df330948701c93..da803feaa1808e8192802584d66bb6af7facf096 100644 (file)
@@ -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")
+
 \f
 ;;; 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."