Ask if dir and subdir dired buffers be killed when deleting dir

Previously, when you've had dired buffers

  ~/foo/
  ~/foo/bar/
  ~/foo/bar/baz/

and then deleted ~/foo/, dired (with dired-clean-up-buffers-too set to
non-nil) would only ask to delete the dired buffer of ~/foo/.  Now it
will offer to delete all three buffers.

* lisp/dired.el (dired-buffers-for-dir): Add optional argument SUBDIRS
which makes the function return also dired buffers showing a subdir of
DIR.
(dired-in-this-tree-p): Make obsolete in favor of file-in-directory-p
which actually does what the name suggest whereas dired-in-this-tree-p
is just string-matching on filenames which will fail with symlinks
filenames including ./ or ../.
This commit is contained in:
Tassilo Horn 2021-06-04 21:58:11 +02:00
parent 4c362b4ef7
commit 7b50ed553f

View file

@ -2820,10 +2820,12 @@ You can then feed the file name(s) to other commands with \\[yank]."
;; Keeping Dired buffers in sync with the filesystem and with each other ;; Keeping Dired buffers in sync with the filesystem and with each other
(defun dired-buffers-for-dir (dir &optional file) (defun dired-buffers-for-dir (dir &optional file subdirs)
"Return a list of buffers for DIR (top level or in-situ subdir). "Return a list of buffers for DIR (top level or in-situ subdir).
If FILE is non-nil, include only those whose wildcard pattern (if any) If FILE is non-nil, include only those whose wildcard pattern (if any)
matches FILE. matches FILE.
If SUBDIRS is non-nil, also include the dired buffers of
directories below DIR.
The list is in reverse order of buffer creation, most recent last. The list is in reverse order of buffer creation, most recent last.
As a side effect, killed dired buffers for DIR are removed from As a side effect, killed dired buffers for DIR are removed from
dired-buffers." dired-buffers."
@ -2835,19 +2837,20 @@ dired-buffers."
((null (buffer-name buf)) ((null (buffer-name buf))
;; Buffer is killed - clean up: ;; Buffer is killed - clean up:
(setq dired-buffers (delq elt dired-buffers))) (setq dired-buffers (delq elt dired-buffers)))
((dired-in-this-tree-p dir (car elt)) ((file-in-directory-p (car elt) dir)
(with-current-buffer buf (with-current-buffer buf
(and (assoc dir dired-subdir-alist) (when (and (or subdirs
(or (null file) (assoc dir dired-subdir-alist))
(if (stringp dired-directory) (or (null file)
(let ((wildcards (file-name-nondirectory (if (stringp dired-directory)
dired-directory))) (let ((wildcards (file-name-nondirectory
(or (zerop (length wildcards)) dired-directory)))
(string-match-p (dired-glob-regexp wildcards) (or (zerop (length wildcards))
file))) (string-match-p (dired-glob-regexp wildcards)
(member (expand-file-name file dir) file)))
(cdr dired-directory)))) (member (expand-file-name file dir)
(setq result (cons buf result))))))) (cdr dired-directory)))))
(setq result (cons buf result)))))))
result)) result))
(defun dired-glob-regexp (pattern) (defun dired-glob-regexp (pattern)
@ -2912,6 +2915,7 @@ dired-buffers."
;;"Is FILE part of the directory tree starting at DIR?" ;;"Is FILE part of the directory tree starting at DIR?"
(let (case-fold-search) (let (case-fold-search)
(string-match-p (concat "^" (regexp-quote dir)) file))) (string-match-p (concat "^" (regexp-quote dir)) file)))
(make-obsolete 'dired-in-this-tree-p 'file-in-directory-p "28.1")
(define-obsolete-function-alias 'dired-in-this-tree (define-obsolete-function-alias 'dired-in-this-tree
'dired-in-this-tree-p "27.1") 'dired-in-this-tree-p "27.1")
@ -3427,7 +3431,8 @@ confirmation. To disable the confirmation, see
(file-name-nondirectory fn)))) (file-name-nondirectory fn))))
(not dired-clean-confirm-killing-deleted-buffers)) (not dired-clean-confirm-killing-deleted-buffers))
(kill-buffer buf))) (kill-buffer buf)))
(let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))) (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)
nil 'subdirs)))
(and buf-list (and buf-list
(or (and dired-clean-confirm-killing-deleted-buffers (or (and dired-clean-confirm-killing-deleted-buffers
(y-or-n-p (y-or-n-p