branch: elpa/gc-buffers commit ad617cfb6200f2da3879803e91be8165b55e30fc Author: Akib Azmain Turja <a...@disroot.org> Commit: Akib Azmain Turja <a...@disroot.org>
Add option to ignore certain buffers --- gc-buffers.el | 69 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/gc-buffers.el b/gc-buffers.el index 2995e1d6a4..37438628aa 100644 --- a/gc-buffers.el +++ b/gc-buffers.el @@ -44,6 +44,9 @@ ;;; Code: + +;;;; User Options: + (defgroup gc-buffers nil "Kill garbage buffers automatically." :group 'internal @@ -54,18 +57,31 @@ "Functions to find garbage buffers. Each function is called with the buffer to test, and if any of the -functions returns non-nil, kill that buffer. The functions should not have -any side-effect. +functions returns t, kill that buffer. The function may also return a +function, then that function is called with the buffer to kill or whatever +it wants. The functions should not have any side-effect. Warning: Putting wrong functions here may delete working buffers. For example, never put `always' here, that would delete all buffers." :type 'hook :options (list #'gc-buffers-elisp-flymake)) +(defcustom gc-buffers-ignore-functions (list #'gc-buffers-ignore-visible) + "Functions to ignore buffers while killing. + +Each function is called with the buffer to test, and if any of the +functions returns non-nil, don't kill that buffer. The functions should +not have any side-effect." + :type 'hook + :options (list #'gc-buffers-ignore-visible)) + (defcustom gc-buffers-delay 60 "Kill garbage buffers after idling for this many seconds." :type 'number) + +;;;; Core functions: + ;;;###autoload (defun gc-buffers () "Kill garbage buffers." @@ -75,25 +91,23 @@ example, never put `always' here, that would delete all buffers." (kill-buffer-query-functions nil) (count 0)) (dolist (buffer (buffer-list)) - (when (and (not (get-buffer-window buffer 'all-frames)) - (run-hook-with-args-until-success 'gc-buffers-functions - buffer)) - (kill-buffer buffer) - (setq count (1+ count)))) + (when (not (run-hook-with-args-until-success + 'gc-buffers-ignore-functions buffer)) + (let ((kill-fn (run-hook-with-args-until-success + 'gc-buffers-functions buffer))) + (when kill-fn + (funcall (if (eq kill-fn t) + #'kill-buffer + kill-fn) + buffer)) + (setq count (1+ count))))) (when (called-interactively-p 'interactive) (message "%s garbage buffer%s killed" (if (zerop count) "No" (number-to-string count)) (if (> count 1) "s" "")))))) -(defun gc-buffers-elisp-flymake (buffer) - "Kill garbage buffers generated by `elisp-flymake-byte-compile'. - -Test if BUFFER is a garbage generated by `elisp-flymake-byte-compile'." - (and (string-match-p (rx string-start " *elisp-flymake-byte-compile*" - (zero-or-one "-" (zero-or-more digit)) - string-end) - (buffer-name buffer)) - (not (get-buffer-process buffer)))) + +;;;; Mode: (defvar gc-buffers--idle-timer nil) @@ -109,5 +123,28 @@ Test if BUFFER is a garbage generated by `elisp-flymake-byte-compile'." (setq gc-buffers--idle-timer (run-with-idle-timer gc-buffers-delay t #'gc-buffers)))) + +;;;; Buffer test functions: + +(defun gc-buffers-elisp-flymake (buffer) + "Kill garbage buffers generated by `elisp-flymake-byte-compile'. + +Check if BUFFER is a garbage generated by `elisp-flymake-byte-compile'." + (and (string-match-p (rx string-start " *elisp-flymake-byte-compile*" + (zero-or-one "-" (zero-or-more digit)) + string-end) + (buffer-name buffer)) + (not (get-buffer-process buffer)))) + + +;;;; Ignore functions: + +(defun gc-buffers-ignore-visible (buffer) + "Ignore visible buffers. + +Check if BUFFER is visible." + (get-buffer-window buffer 'all-frames)) + + (provide 'gc-buffers) ;;; gc-buffers.el ends here