midnight.el 9.3 KB

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