midnight.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ;;; midnight.el --- run something every midnight, e.g., kill old buffers -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1998, 2001-2015 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. (if midnight-mode (timer-activate midnight-timer)
  44. (cancel-timer midnight-timer)))
  45. ;;; time conversion
  46. (defun midnight-buffer-display-time (buffer)
  47. "Return the time-stamp of BUFFER, or current buffer, as float."
  48. (with-current-buffer buffer
  49. (when buffer-display-time (float-time buffer-display-time))))
  50. ;;; clean-buffer-list stuff
  51. (defcustom clean-buffer-list-delay-general 3
  52. "The number of days before any buffer becomes eligible for autokilling.
  53. The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
  54. Currently displayed and/or modified (unsaved) buffers, as well as buffers
  55. matching `clean-buffer-list-kill-never-buffer-names' and
  56. `clean-buffer-list-kill-never-regexps' are excluded."
  57. :type 'integer)
  58. (defcustom clean-buffer-list-delay-special 3600
  59. "The number of seconds before some buffers become eligible for autokilling.
  60. Buffers matched by `clean-buffer-list-kill-regexps' and
  61. `clean-buffer-list-kill-buffer-names' are killed if they were last
  62. displayed more than this many seconds ago."
  63. :type 'integer)
  64. (defcustom clean-buffer-list-kill-regexps '("\\`\\*Man ")
  65. "List of regexps saying which buffers will be killed at midnight.
  66. If buffer name matches a regexp in the list and the buffer was not displayed
  67. in the last `clean-buffer-list-delay-special' seconds, it is killed by
  68. `clean-buffer-list' when is it in `midnight-hook'.
  69. If a member of the list is a cons, its `car' is the regexp and its `cdr' is
  70. the number of seconds to use instead of `clean-buffer-list-delay-special'.
  71. See also `clean-buffer-list-kill-buffer-names',
  72. `clean-buffer-list-kill-never-regexps' and
  73. `clean-buffer-list-kill-never-buffer-names'.
  74. Each element can also be a function instead of a regexp, in which case
  75. it takes a single argument (a buffer name) and should return non-nil
  76. if the buffer should be killed by `clean-buffer-list'."
  77. :type '(repeat
  78. (choice (regexp :tag "Regexp matching Buffer Name")
  79. (function :tag "Predicate function"))))
  80. (defcustom clean-buffer-list-kill-buffer-names
  81. '("*Help*" "*Apropos*" "*Buffer List*" "*Compile-Log*" "*info*"
  82. "*vc*" "*vc-diff*" "*diff*")
  83. "List of strings saying which buffers will be killed at midnight.
  84. Buffers with names in this list, which were not displayed in the last
  85. `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list'
  86. when is it in `midnight-hook'.
  87. If a member of the list is a cons, its `car' is the name and its `cdr' is
  88. the number of seconds to use instead of `clean-buffer-list-delay-special'.
  89. See also `clean-buffer-list-kill-regexps',
  90. `clean-buffer-list-kill-never-regexps' and
  91. `clean-buffer-list-kill-never-buffer-names'."
  92. :type '(repeat (string :tag "Buffer Name")))
  93. (defcustom clean-buffer-list-kill-never-buffer-names
  94. '("*scratch*" "*Messages*")
  95. "List of buffer names which will never be killed by `clean-buffer-list'.
  96. See also `clean-buffer-list-kill-never-regexps'.
  97. Note that this does override `clean-buffer-list-kill-regexps' and
  98. `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
  99. two lists will NOT be killed if it is also present in this list."
  100. :type '(repeat (string :tag "Buffer Name")))
  101. (defcustom clean-buffer-list-kill-never-regexps '("\\` \\*Minibuf-.*\\*\\'")
  102. "List of regexp saying which buffers will never be killed at midnight.
  103. See also `clean-buffer-list-kill-never-buffer-names'.
  104. Killing is done by `clean-buffer-list'.
  105. Note that this does override `clean-buffer-list-kill-regexps' and
  106. `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
  107. two lists will NOT be killed if it also matches anything in this list.
  108. Each element can also be a function instead of a regexp, in which case
  109. it takes a single argument (a buffer name) and should return non-nil
  110. if the buffer should never be killed by `clean-buffer-list'."
  111. :type '(repeat
  112. (choice (regexp :tag "Regexp matching Buffer Name")
  113. (function :tag "Predicate function"))))
  114. (defun clean-buffer-list-delay (name)
  115. "Return the delay, in seconds, before killing a buffer named NAME.
  116. Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps'
  117. `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'.
  118. Autokilling is done by `clean-buffer-list'."
  119. (or (assoc-default name clean-buffer-list-kill-buffer-names #'string=
  120. clean-buffer-list-delay-special)
  121. (assoc-default name clean-buffer-list-kill-regexps
  122. (lambda (re str)
  123. (if (functionp re)
  124. (funcall re str) (string-match re str)))
  125. clean-buffer-list-delay-special)
  126. (* clean-buffer-list-delay-general 24 60 60)))
  127. ;;;###autoload
  128. (defun clean-buffer-list ()
  129. "Kill old buffers that have not been displayed recently.
  130. The relevant variables are `clean-buffer-list-delay-general',
  131. `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names',
  132. `clean-buffer-list-kill-never-buffer-names',
  133. `clean-buffer-list-kill-regexps' and
  134. `clean-buffer-list-kill-never-regexps'.
  135. While processing buffers, this procedure displays messages containing
  136. the current date/time, buffer name, how many seconds ago it was
  137. displayed (can be nil if the buffer was never displayed) and its
  138. lifetime, i.e., its \"age\" when it will be purged."
  139. (interactive)
  140. (let ((tm (float-time)) bts (ts (format-time-string "%Y-%m-%d %T"))
  141. delay cbld bn)
  142. (dolist (buf (buffer-list))
  143. (when (buffer-live-p buf)
  144. (setq bts (midnight-buffer-display-time buf) bn (buffer-name buf)
  145. delay (if bts (- tm bts) 0) cbld (clean-buffer-list-delay bn))
  146. (message "[%s] `%s' [%s %d]" ts bn (if bts (round delay)) cbld)
  147. (unless (or (cl-find bn clean-buffer-list-kill-never-regexps
  148. :test (lambda (bn re)
  149. (if (functionp re)
  150. (funcall re bn)
  151. (string-match re bn))))
  152. (cl-find bn clean-buffer-list-kill-never-buffer-names
  153. :test #'string-equal)
  154. (get-buffer-process buf)
  155. (and (buffer-file-name buf) (buffer-modified-p buf))
  156. (get-buffer-window buf 'visible) (< delay cbld))
  157. (message "[%s] killing `%s'" ts bn)
  158. (kill-buffer buf))))))
  159. ;;; midnight hook
  160. (defvar midnight-period (* 24 60 60)
  161. "The number of seconds in a day--the delta for `midnight-timer'.")
  162. (defcustom midnight-hook '(clean-buffer-list)
  163. "The hook run `midnight-delay' seconds after midnight every day.
  164. The default value is `clean-buffer-list'."
  165. :type 'hook)
  166. (defun midnight-next ()
  167. "Return the number of seconds till the next midnight."
  168. (pcase-let ((`(,sec ,min ,hrs) (decode-time)))
  169. (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec)))
  170. ;;;###autoload
  171. (defun midnight-delay-set (symb tm)
  172. "Modify `midnight-timer' according to `midnight-delay'.
  173. Sets the first argument SYMB (which must be symbol `midnight-delay')
  174. to its second argument TM."
  175. (cl-assert (eq symb 'midnight-delay) t
  176. "Invalid argument to `midnight-delay-set': `%s'")
  177. (set symb tm)
  178. (when (timerp midnight-timer) (cancel-timer midnight-timer))
  179. (setq midnight-timer
  180. (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm)
  181. midnight-period #'run-hooks 'midnight-hook)))
  182. (defcustom midnight-delay 3600
  183. "The number of seconds after the midnight when the `midnight-timer' is run.
  184. You should set this variable before loading midnight.el, or
  185. set it by calling `midnight-delay-set', or use `custom'.
  186. If you wish, you can use a string instead, it will be passed as the
  187. first argument to `run-at-time'."
  188. :type 'sexp
  189. :set #'midnight-delay-set)
  190. (provide 'midnight)
  191. ;;; midnight.el ends here