Skip to content

Commit

Permalink
✨ Add nuke-buffers-from-directory
Browse files Browse the repository at this point in the history
Adds a command that lets me nuke all saved buffers that are visiting a file
within and below a given directory.
  • Loading branch information
davep committed Mar 4, 2019
1 parent 21b0c4d commit 037005b
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion nuke-buffers.el
@@ -1,5 +1,5 @@
;;; nuke-buffers.el --- Safely kill as many buffers as possible
;; Copyright 2017 by Dave Pearson <davep@davep.org>
;; Copyright 2017-2019 by Dave Pearson <davep@davep.org>

;; Author: Dave Pearson <davep@davep.org>
;; Version: 1.4
Expand Down Expand Up @@ -72,6 +72,32 @@
(not (eq buffer (current-buffer)))))
(buffer-list)))

(defun nuke-buffers-get-candidates-from (directory)
"Get a list of buffers that can be nuked below DIRECTORY.
This function looks for buffers that are visiting files, and only
files which have been saved. At the moment it *doesn't* return a
buffers visiting an actual directory (so it doesn't return dired
buffers). I'd like to change this at some point."
(let* ((directory (file-truename directory))
(directory-len (length directory)))
(seq-filter (lambda (buffer)
(let* ((file (buffer-file-name buffer))
(file-len (length file)))
(and
;; If it's a buffer that's associated with a file...
file
;; ...and if it relates to a saved file...
(not (nuke-buffers-unsaved-file-buffer-p buffer))
;; ...and if it's within the given directory...
(string=
directory
(substring
(file-truename file)
0
(min file-len directory-len))))))
(buffer-list))))

;;;###autoload
(defun nuke-buffers ()
"Kill as many buffers as possible, without losing important things."
Expand All @@ -80,6 +106,18 @@
(length
(mapc #'kill-buffer (nuke-buffers-get-candidates)))))

;;;###autoload
(defun nuke-buffers-from-directory (directory)
"Nuke all buffers relating to files in DIRECTORY.
Note that `nuke-buffers-ignore' is itself ignored here. Unsaved
buffers are, however, bypassed."
(interactive "DNuke all within: ")
(message "Nuked %d buffer(s) related to %s."
(length
(mapc #'kill-buffer (nuke-buffers-get-candidates-from directory)))
directory))

(provide 'nuke-buffers)

;;; nuke-buffers.el ends here

0 comments on commit 037005b

Please sign in to comment.