1 ;;; midnight.el --- run something every midnight, e.g., kill old buffers
3 ;; Copyright (C) 1998, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Sam Steingold <sds@gnu.org>
6 ;; Maintainer: Sam Steingold <sds@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; To use the file, put (require 'midnight) into your .emacs. Then, at
28 ;; midnight, Emacs will run the normal hook `midnight-hook'. You can
29 ;; put whatever you like there, say, `calendar'; by default there is
30 ;; only one function there - `clean-buffer-list'. It will kill the
31 ;; buffers matching `clean-buffer-list-kill-buffer-names' and
32 ;; `clean-buffer-list-kill-regexps' and the buffers which where last
33 ;; displayed more than `clean-buffer-list-delay-general' days ago,
34 ;; keeping `clean-buffer-list-kill-never-buffer-names' and
35 ;; `clean-buffer-list-kill-never-regexps'.
44 (defgroup midnight nil
45 "Run something every day at midnight."
49 (defvar midnight-timer nil
50 "Timer running the `midnight-hook' `midnight-delay' seconds after midnight.
51 Use `cancel-timer' to stop it and `midnight-delay-set' to change
52 the time when it is run.")
54 (defcustom midnight-mode nil
55 "Non-nil means run `midnight-hook' at midnight.
56 Setting this variable outside customize has no effect;
57 call `cancel-timer' or `timer-activate' on `midnight-timer' instead."
61 :initialize
'custom-initialize-default
62 :set
(lambda (symb val
)
63 (set symb val
) (require 'midnight
)
64 (if val
(timer-activate midnight-timer
)
65 (cancel-timer midnight-timer
))))
69 (defun midnight-buffer-display-time (&optional buffer
)
70 "Return the time-stamp of BUFFER, or current buffer, as float."
71 (with-current-buffer (or buffer
(current-buffer))
72 (when buffer-display-time
(float-time buffer-display-time
))))
74 ;;; clean-buffer-list stuff
76 (defcustom clean-buffer-list-delay-general
3
77 "The number of days before any buffer becomes eligible for autokilling.
78 The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
79 Currently displayed and/or modified (unsaved) buffers, as well as buffers
80 matching `clean-buffer-list-kill-never-buffer-names' and
81 `clean-buffer-list-kill-never-regexps' are excluded."
85 (defcustom clean-buffer-list-delay-special
3600
86 "The number of seconds before some buffers become eligible for autokilling.
87 Buffers matched by `clean-buffer-list-kill-regexps' and
88 `clean-buffer-list-kill-buffer-names' are killed if they were last
89 displayed more than this many seconds ago."
93 (defcustom clean-buffer-list-kill-regexps nil
94 "List of regexps saying which buffers will be killed at midnight.
95 If buffer name matches a regexp in the list and the buffer was not displayed
96 in the last `clean-buffer-list-delay-special' seconds, it is killed by
97 `clean-buffer-list' when is it in `midnight-hook'.
98 If a member of the list is a cons, its `car' is the regexp and its `cdr' is
99 the number of seconds to use instead of `clean-buffer-list-delay-special'.
100 See also `clean-buffer-list-kill-buffer-names',
101 `clean-buffer-list-kill-never-regexps' and
102 `clean-buffer-list-kill-never-buffer-names'."
103 :type
'(repeat (regexp :tag
"Regexp matching Buffer Name"))
106 (defcustom clean-buffer-list-kill-buffer-names
107 '("*Help*" "*Apropos*" "*Man " "*Buffer List*" "*Compile-Log*" "*info*"
108 "*vc*" "*vc-diff*" "*diff*")
109 "List of strings saying which buffers will be killed at midnight.
110 Buffers with names in this list, which were not displayed in the last
111 `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list'
112 when is it in `midnight-hook'.
113 If a member of the list is a cons, its `car' is the name and its `cdr' is
114 the number of seconds to use instead of `clean-buffer-list-delay-special'.
115 See also `clean-buffer-list-kill-regexps',
116 `clean-buffer-list-kill-never-regexps' and
117 `clean-buffer-list-kill-never-buffer-names'."
118 :type
'(repeat (string :tag
"Buffer Name"))
121 (defcustom clean-buffer-list-kill-never-buffer-names
122 '("*scratch*" "*Messages*")
123 "List of buffer names which will never be killed by `clean-buffer-list'.
124 See also `clean-buffer-list-kill-never-regexps'.
125 Note that this does override `clean-buffer-list-kill-regexps' and
126 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
127 two lists will NOT be killed if it is also present in this list."
128 :type
'(repeat (string :tag
"Buffer Name"))
131 (defcustom clean-buffer-list-kill-never-regexps
'("^ \\*Minibuf-.*\\*$")
132 "List of regexp saying which buffers will never be killed at midnight.
133 See also `clean-buffer-list-kill-never-buffer-names'.
134 Killing is done by `clean-buffer-list'.
135 Note that this does override `clean-buffer-list-kill-regexps' and
136 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
137 two lists will NOT be killed if it also matches anything in this list."
138 :type
'(repeat (regexp :tag
"Regexp matching Buffer Name"))
141 (defun midnight-find (el ls test
&optional key
)
142 "A stopgap solution to the absence of `find' in ELisp."
144 (when (funcall test
(if key
(funcall key rr
) rr
) el
)
147 (defun clean-buffer-list-delay (name)
148 "Return the delay, in seconds, before killing a buffer named NAME.
149 Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps'
150 `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'.
151 Autokilling is done by `clean-buffer-list'."
152 (or (assoc-default name clean-buffer-list-kill-buffer-names
'string
=
153 clean-buffer-list-delay-special
)
154 (assoc-default name clean-buffer-list-kill-regexps
'string-match
155 clean-buffer-list-delay-special
)
156 (* clean-buffer-list-delay-general
24 60 60)))
159 (defun clean-buffer-list ()
160 "Kill old buffers that have not been displayed recently.
161 The relevant variables are `clean-buffer-list-delay-general',
162 `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names',
163 `clean-buffer-list-kill-never-buffer-names',
164 `clean-buffer-list-kill-regexps' and
165 `clean-buffer-list-kill-never-regexps'.
166 While processing buffers, this procedure displays messages containing
167 the current date/time, buffer name, how many seconds ago it was
168 displayed (can be nil if the buffer was never displayed) and its
169 lifetime, i.e., its \"age\" when it will be purged."
171 (let ((tm (float-time)) bts
(ts (format-time-string "%Y-%m-%d %T"))
173 (dolist (buf (buffer-list))
174 (when (buffer-live-p buf
)
175 (setq bts
(midnight-buffer-display-time buf
) bn
(buffer-name buf
)
176 delay
(if bts
(- tm bts
) 0) cbld
(clean-buffer-list-delay bn
))
177 (message "[%s] `%s' [%s %d]" ts bn
(if bts
(round delay
)) cbld
)
178 (unless (or (midnight-find bn clean-buffer-list-kill-never-regexps
180 (midnight-find bn clean-buffer-list-kill-never-buffer-names
182 (get-buffer-process buf
)
183 (and (buffer-file-name buf
) (buffer-modified-p buf
))
184 (get-buffer-window buf
'visible
) (< delay cbld
))
185 (message "[%s] killing `%s'" ts bn
)
186 (kill-buffer buf
))))))
190 (defvar midnight-period
(* 24 60 60)
191 "The number of seconds in a day--the delta for `midnight-timer'.")
193 (defcustom midnight-hook
'(clean-buffer-list)
194 "The hook run `midnight-delay' seconds after midnight every day.
195 The default value is `clean-buffer-list'."
199 (defun midnight-next ()
200 "Return the number of seconds till the next midnight."
201 (multiple-value-bind (sec min hrs
)
202 (values-list (decode-time))
203 (- (* 24 60 60) (* 60 60 hrs
) (* 60 min
) sec
)))
206 (defun midnight-delay-set (symb tm
)
207 "Modify `midnight-timer' according to `midnight-delay'.
208 Sets the first argument SYMB (which must be symbol `midnight-delay')
209 to its second argument TM."
210 (assert (eq symb
'midnight-delay
) t
211 "Invalid argument to `midnight-delay-set': `%s'")
213 (when (timerp midnight-timer
) (cancel-timer midnight-timer
))
215 (run-at-time (if (numberp tm
) (+ (midnight-next) tm
) tm
)
216 midnight-period
'run-hooks
'midnight-hook
)))
218 (defcustom midnight-delay
3600
219 "The number of seconds after the midnight when the `midnight-timer' is run.
220 You should set this variable before loading midnight.el, or
221 set it by calling `midnight-delay-set', or use `custom'.
222 If you wish, you can use a string instead, it will be passed as the
223 first argument to `run-at-time'."
225 :set
'midnight-delay-set
230 ;;; midnight.el ends here