midnight.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; midnight.el --- run something every midnight, e.g., kill old buffers -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1998, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Sam Steingold <sds@gnu.org>
  4. ;; Maintainer: Sam Steingold <sds@gnu.org>
  5. ;; Created: 1998-05-18
  6. ;; Keywords: utilities
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; To use the file, put (require 'midnight) into your .emacs. Then, at
  20. ;; midnight, Emacs will run the normal hook `midnight-hook'. You can
  21. ;; put whatever you like there, say, `calendar'; by default there is
  22. ;; only one function there - `clean-buffer-list'. It will kill the
  23. ;; buffers matching `clean-buffer-list-kill-buffer-names' and
  24. ;; `clean-buffer-list-kill-regexps' and the buffers which where last
  25. ;; displayed more than `clean-buffer-list-delay-general' days ago,
  26. ;; keeping `clean-buffer-list-kill-never-buffer-names' and
  27. ;; `clean-buffer-list-kill-never-regexps'.
  28. ;;; Code:
  29. (require 'cl-lib)
  30. (defgroup midnight nil
  31. "Run something every day at midnight."
  32. :group 'calendar
  33. :version "20.3")
  34. (defvar midnight-timer nil
  35. "Timer running the `midnight-hook' `midnight-delay' seconds after midnight.
  36. Use `cancel-timer' to stop it and `midnight-delay-set' to change
  37. the time when it is run.")
  38. ;;;###autoload
  39. (define-minor-mode midnight-mode
  40. "Non-nil means run `midnight-hook' at midnight."
  41. :global t
  42. :initialize #'custom-initialize-default
  43. ;; Disable first, since the ':initialize' function above already
  44. ;; starts the timer when the mode is turned on for the first time,
  45. ;; via setting 'midnight-delay', which calls 'midnight-delay-set',
  46. ;; which starts the timer.
  47. (when (timerp midnight-timer) (cancel-timer midnight-timer))
  48. (if midnight-mode (timer-activate midnight-timer)))
  49. ;;; clean-buffer-list stuff
  50. (defcustom clean-buffer-list-delay-general 3
  51. "The number of days before any buffer becomes eligible for autokilling.
  52. The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
  53. Currently displayed and/or modified (unsaved) buffers, as well as buffers
  54. matching `clean-buffer-list-kill-never-buffer-names' and
  55. `clean-buffer-list-kill-never-regexps' are excluded."
  56. :type 'integer)
  57. (defcustom clean-buffer-list-delay-special 3600
  58. "The number of seconds before some buffers become eligible for autokilling.
  59. Buffers matched by `clean-buffer-list-kill-regexps' and
  60. `clean-buffer-list-kill-buffer-names' are killed if they were last
  61. displayed more than this many seconds ago."
  62. :type 'integer)
  63. (defcustom clean-buffer-list-kill-regexps '("\\`\\*Man ")
  64. "List of regexps saying which buffers will be killed at midnight.
  65. If buffer name matches a regexp in the list and the buffer was not displayed
  66. in the last `clean-buffer-list-delay-special' seconds, it is killed by
  67. `clean-buffer-list' when is it in `midnight-hook'.
  68. If a member of the list is a cons, its `car' is the regexp and its `cdr' is
  69. the number of seconds to use instead of `clean-buffer-list-delay-special'.
  70. See also `clean-buffer-list-kill-buffer-names',
  71. `clean-buffer-list-kill-never-regexps' and
  72. `clean-buffer-list-kill-never-buffer-names'.
  73. Each element can also be a function instead of a regexp, in which case
  74. it takes a single argument (a buffer name) and should return non-nil
  75. if the buffer should be killed by `clean-buffer-list'."
  76. :type '(repeat
  77. (choice (regexp :tag "Regexp matching Buffer Name")
  78. (function :tag "Predicate function"))))
  79. (defcustom clean-buffer-list-kill-buffer-names
  80. '("*Help*" "*Apropos*" "*Buffer List*" "*Compile-Log*" "*info*"
  81. "*vc*" "*vc-diff*" "*diff*")
  82. "List of strings saying which buffers will be killed at midnight.
  83. Buffers with names in this list, which were not displayed in the last
  84. `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list'
  85. when is it in `midnight-hook'.
  86. If a member of the list is a cons, its `car' is the name and its `cdr' is
  87. the number of seconds to use instead of `clean-buffer-list-delay-special'.
  88. See also `clean-buffer-list-kill-regexps',
  89. `clean-buffer-list-kill-never-regexps' and
  90. `clean-buffer-list-kill-never-buffer-names'."
  91. :type '(repeat (string :tag "Buffer Name")))
  92. (defcustom clean-buffer-list-kill-never-buffer-names
  93. '("*scratch*" "*Messages*")
  94. "List of buffer names which will never be killed by `clean-buffer-list'.
  95. See also `clean-buffer-list-kill-never-regexps'.
  96. Note that this does override `clean-buffer-list-kill-regexps' and
  97. `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
  98. two lists will NOT be killed if it is also present in this list."
  99. :type '(repeat (string :tag "Buffer Name")))
  100. (defcustom clean-buffer-list-kill-never-regexps '("\\` \\*Minibuf-.*\\*\\'")
  101. "List of regexp saying which buffers will never be killed at midnight.
  102. See also `clean-buffer-list-kill-never-buffer-names'.
  103. Killing is done by `clean-buffer-list'.
  104. Note that this does override `clean-buffer-list-kill-regexps' and
  105. `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
  106. two lists will NOT be killed if it also matches anything in this list.
  107. Each element can also be a function instead of a regexp, in which case
  108. it takes a single argument (a buffer name) and should return non-nil
  109. if the buffer should never be killed by `clean-buffer-list'."
  110. :type '(repeat
  111. (choice (regexp :tag "Regexp matching Buffer Name")
  112. (function :tag "Predicate function"))))
  113. (defun clean-buffer-list-delay (name)
  114. "Return the delay, in seconds, before killing a buffer named NAME.
  115. Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps'
  116. `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'.
  117. Autokilling is done by `clean-buffer-list'."
  118. (or (assoc-default name clean-buffer-list-kill-buffer-names #'string=
  119. clean-buffer-list-delay-special)
  120. (assoc-default name clean-buffer-list-kill-regexps
  121. (lambda (re str)
  122. (if (functionp re)
  123. (funcall re str) (string-match re str)))
  124. clean-buffer-list-delay-special)
  125. (* clean-buffer-list-delay-general 24 60 60)))
  126. ;;;###autoload
  127. (defun clean-buffer-list ()
  128. "Kill old buffers that have not been displayed recently.
  129. The relevant variables are `clean-buffer-list-delay-general',
  130. `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names',
  131. `clean-buffer-list-kill-never-buffer-names',
  132. `clean-buffer-list-kill-regexps' and
  133. `clean-buffer-list-kill-never-regexps'.
  134. While processing buffers, this procedure displays messages containing
  135. the current date/time, buffer name, how many seconds ago it was
  136. displayed (can be nil if the buffer was never displayed) and its
  137. lifetime, i.e., its \"age\" when it will be purged."
  138. (interactive)
  139. (let ((tm (current-time)) bts (ts (format-time-string "%Y-%m-%d %T"))
  140. delay cbld bn)
  141. (dolist (buf (buffer-list))
  142. (when (buffer-live-p buf)
  143. (setq bts (with-current-buffer buf buffer-display-time)
  144. bn (buffer-name buf)
  145. delay (if bts (round (float-time (time-subtract tm bts))) 0)
  146. cbld (clean-buffer-list-delay bn))
  147. (message "[%s] `%s' [%s %d]" ts bn delay cbld)
  148. (unless (or (cl-find bn clean-buffer-list-kill-never-regexps
  149. :test (lambda (bn re)
  150. (if (functionp re)
  151. (funcall re bn)
  152. (string-match re bn))))
  153. (cl-find bn clean-buffer-list-kill-never-buffer-names
  154. :test #'string-equal)
  155. (get-buffer-process buf)
  156. (and (buffer-file-name buf) (buffer-modified-p buf))
  157. (get-buffer-window buf 'visible)
  158. (< delay cbld))
  159. (message "[%s] killing `%s'" ts bn)
  160. (kill-buffer buf))))))
  161. ;;; midnight hook
  162. (defvar midnight-period (* 24 60 60)
  163. "The number of seconds in a day--the delta for `midnight-timer'.")
  164. (defcustom midnight-hook '(clean-buffer-list)
  165. "The hook run `midnight-delay' seconds after midnight every day.
  166. The default value is `clean-buffer-list'."
  167. :type 'hook)
  168. (defun midnight-next ()
  169. "Return the number of seconds till the next midnight."
  170. (pcase-let ((`(,sec ,min ,hrs) (decode-time)))
  171. (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec)))
  172. ;;;###autoload
  173. (defun midnight-delay-set (symb tm)
  174. "Modify `midnight-timer' according to `midnight-delay'.
  175. Sets the first argument SYMB (which must be symbol `midnight-delay')
  176. to its second argument TM."
  177. (cl-assert (eq symb 'midnight-delay) t
  178. "Invalid argument to `midnight-delay-set': `%s'")
  179. (set symb tm)
  180. (when (timerp midnight-timer) (cancel-timer midnight-timer))
  181. (setq midnight-timer
  182. (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm)
  183. midnight-period #'run-hooks 'midnight-hook)))
  184. (defcustom midnight-delay 3600
  185. "The number of seconds after the midnight when the `midnight-timer' is run.
  186. You should set this variable before loading midnight.el, or
  187. set it by calling `midnight-delay-set', or use `custom'.
  188. If you wish, you can use a string instead, it will be passed as the
  189. first argument to `run-at-time'."
  190. :type '(choice integer string)
  191. :set #'midnight-delay-set)
  192. (provide 'midnight)
  193. ;;; midnight.el ends here