* lisp/progmodes/gdb-mi.el (gdb): Fix typo in previous change.
[emacs.git] / lisp / midnight.el
blob9a6b162e986776b6db526bec8924dd6c4d4c0c8a
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>
7 ;; Created: 1998-05-18
8 ;; Keywords: utilities
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/>.
25 ;;; Commentary:
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'.
37 ;;; Code:
39 (eval-when-compile
40 (require 'cl))
42 (require 'timer)
44 (defgroup midnight nil
45 "Run something every day at midnight."
46 :group 'calendar
47 :version "20.3")
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."
58 :type 'boolean
59 :group 'midnight
60 :require 'midnight
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))))
67 ;;; time conversion
69 (defun midnight-time-float (num)
70 "Convert the float number of seconds since epoch to the list of 3 integers."
71 (let* ((div (ash 1 16)) (1st (floor num div)))
72 (list 1st (floor (- num (* (float div) 1st)))
73 (round (* 10000000 (mod num 1))))))
75 (defun midnight-buffer-display-time (&optional buffer)
76 "Return the time-stamp of BUFFER, or current buffer, as float."
77 (with-current-buffer (or buffer (current-buffer))
78 (when buffer-display-time (float-time buffer-display-time))))
80 ;;; clean-buffer-list stuff
82 (defcustom clean-buffer-list-delay-general 3
83 "The number of days before any buffer becomes eligible for autokilling.
84 The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
85 Currently displayed and/or modified (unsaved) buffers, as well as buffers
86 matching `clean-buffer-list-kill-never-buffer-names' and
87 `clean-buffer-list-kill-never-regexps' are excluded."
88 :type 'integer
89 :group 'midnight)
91 (defcustom clean-buffer-list-delay-special 3600
92 "The number of seconds before some buffers become eligible for autokilling.
93 Buffers matched by `clean-buffer-list-kill-regexps' and
94 `clean-buffer-list-kill-buffer-names' are killed if they were last
95 displayed more than this many seconds ago."
96 :type 'integer
97 :group 'midnight)
99 (defcustom clean-buffer-list-kill-regexps nil
100 "List of regexps saying which buffers will be killed at midnight.
101 If buffer name matches a regexp in the list and the buffer was not displayed
102 in the last `clean-buffer-list-delay-special' seconds, it is killed by
103 `clean-buffer-list' when is it in `midnight-hook'.
104 If a member of the list is a cons, its `car' is the regexp and its `cdr' is
105 the number of seconds to use instead of `clean-buffer-list-delay-special'.
106 See also `clean-buffer-list-kill-buffer-names',
107 `clean-buffer-list-kill-never-regexps' and
108 `clean-buffer-list-kill-never-buffer-names'."
109 :type '(repeat (regexp :tag "Regexp matching Buffer Name"))
110 :group 'midnight)
112 (defcustom clean-buffer-list-kill-buffer-names
113 '("*Help*" "*Apropos*" "*Man " "*Buffer List*" "*Compile-Log*" "*info*"
114 "*vc*" "*vc-diff*" "*diff*")
115 "List of strings saying which buffers will be killed at midnight.
116 Buffers with names in this list, which were not displayed in the last
117 `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list'
118 when is it in `midnight-hook'.
119 If a member of the list is a cons, its `car' is the name and its `cdr' is
120 the number of seconds to use instead of `clean-buffer-list-delay-special'.
121 See also `clean-buffer-list-kill-regexps',
122 `clean-buffer-list-kill-never-regexps' and
123 `clean-buffer-list-kill-never-buffer-names'."
124 :type '(repeat (string :tag "Buffer Name"))
125 :group 'midnight)
127 (defcustom clean-buffer-list-kill-never-buffer-names
128 '("*scratch*" "*Messages*")
129 "List of buffer names which will never be killed by `clean-buffer-list'.
130 See also `clean-buffer-list-kill-never-regexps'.
131 Note that this does override `clean-buffer-list-kill-regexps' and
132 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
133 two lists will NOT be killed if it is also present in this list."
134 :type '(repeat (string :tag "Buffer Name"))
135 :group 'midnight)
137 (defcustom clean-buffer-list-kill-never-regexps '("^ \\*Minibuf-.*\\*$")
138 "List of regexp saying which buffers will never be killed at midnight.
139 See also `clean-buffer-list-kill-never-buffer-names'.
140 Killing is done by `clean-buffer-list'.
141 Note that this does override `clean-buffer-list-kill-regexps' and
142 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
143 two lists will NOT be killed if it also matches anything in this list."
144 :type '(repeat (regexp :tag "Regexp matching Buffer Name"))
145 :group 'midnight)
147 (defun midnight-find (el ls test &optional key)
148 "A stopgap solution to the absence of `find' in ELisp."
149 (dolist (rr ls)
150 (when (funcall test (if key (funcall key rr) rr) el)
151 (return rr))))
153 (defun clean-buffer-list-delay (name)
154 "Return the delay, in seconds, before killing a buffer named NAME.
155 Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps'
156 `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'.
157 Autokilling is done by `clean-buffer-list'."
158 (or (assoc-default name clean-buffer-list-kill-buffer-names 'string=
159 clean-buffer-list-delay-special)
160 (assoc-default name clean-buffer-list-kill-regexps 'string-match
161 clean-buffer-list-delay-special)
162 (* clean-buffer-list-delay-general 24 60 60)))
164 ;;;###autoload
165 (defun clean-buffer-list ()
166 "Kill old buffers that have not been displayed recently.
167 The relevant variables are `clean-buffer-list-delay-general',
168 `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names',
169 `clean-buffer-list-kill-never-buffer-names',
170 `clean-buffer-list-kill-regexps' and
171 `clean-buffer-list-kill-never-regexps'.
172 While processing buffers, this procedure displays messages containing
173 the current date/time, buffer name, how many seconds ago it was
174 displayed (can be nil if the buffer was never displayed) and its
175 lifetime, i.e., its \"age\" when it will be purged."
176 (interactive)
177 (let ((tm (float-time)) bts (ts (format-time-string "%Y-%m-%d %T"))
178 delay cbld bn)
179 (dolist (buf (buffer-list))
180 (when (buffer-live-p buf)
181 (setq bts (midnight-buffer-display-time buf) bn (buffer-name buf)
182 delay (if bts (- tm bts) 0) cbld (clean-buffer-list-delay bn))
183 (message "[%s] `%s' [%s %d]" ts bn (if bts (round delay)) cbld)
184 (unless (or (midnight-find bn clean-buffer-list-kill-never-regexps
185 'string-match)
186 (midnight-find bn clean-buffer-list-kill-never-buffer-names
187 'string-equal)
188 (get-buffer-process buf)
189 (and (buffer-file-name buf) (buffer-modified-p buf))
190 (get-buffer-window buf 'visible) (< delay cbld))
191 (message "[%s] killing `%s'" ts bn)
192 (kill-buffer buf))))))
194 ;;; midnight hook
196 (defvar midnight-period (* 24 60 60)
197 "The number of seconds in a day--the delta for `midnight-timer'.")
199 (defcustom midnight-hook '(clean-buffer-list)
200 "The hook run `midnight-delay' seconds after midnight every day.
201 The default value is `clean-buffer-list'."
202 :type 'hook
203 :group 'midnight)
205 (defun midnight-next ()
206 "Return the number of seconds till the next midnight."
207 (multiple-value-bind (sec min hrs)
208 (values-list (decode-time))
209 (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec)))
211 ;;;###autoload
212 (defun midnight-delay-set (symb tm)
213 "Modify `midnight-timer' according to `midnight-delay'.
214 Sets the first argument SYMB (which must be symbol `midnight-delay')
215 to its second argument TM."
216 (assert (eq symb 'midnight-delay) t
217 "Invalid argument to `midnight-delay-set': `%s'")
218 (set symb tm)
219 (when (timerp midnight-timer) (cancel-timer midnight-timer))
220 (setq midnight-timer
221 (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm)
222 midnight-period 'run-hooks 'midnight-hook)))
224 (defcustom midnight-delay 3600
225 "The number of seconds after the midnight when the `midnight-timer' is run.
226 You should set this variable before loading midnight.el, or
227 set it by calling `midnight-delay-set', or use `custom'.
228 If you wish, you can use a string instead, it will be passed as the
229 first argument to `run-at-time'."
230 :type 'sexp
231 :set 'midnight-delay-set
232 :group 'midnight)
234 (provide 'midnight)
236 ;;; midnight.el ends here