Commit | Line | Data |
---|---|---|
ac06bd0f RS |
1 | ;;; midnight.el --- run something every midnight, e.g., kill old buffers. |
2 | ||
3 | ;;; Copyright (C) 1998 Free Software Foundation, Inc. | |
4 | ||
5 | ;;; Author: Sam Steingold <sds@usa.net> | |
6 | ;;; Maintainer: Sam Steingold <sds@usa.net> | |
7 | ;;; Created: 1998-05-18 | |
8 | ;;; Keywords: utilities | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; To use the file, put (require 'midnight) into your .emacs. Then, at | |
30 | ;; midnight, Emacs will run the normal hook `midnight-hook'. You can | |
31 | ;; put whatever you like there, say, `calendar'; by default there is | |
32 | ;; only one function there - `clean-buffer-list'. It will kill the | |
33 | ;; buffers matching `clean-buffer-list-kill-buffer-names' and | |
34 | ;; `clean-buffer-list-kill-regexps' and the buffers which where last | |
35 | ;; displayed more than `clean-buffer-list-delay-general' days ago, | |
36 | ;; keeping `clean-buffer-list-kill-never-buffer-names' and | |
37 | ;; `clean-buffer-list-kill-never-regexps'. | |
38 | ||
39 | (eval-when-compile (require 'cl)) | |
40 | ||
41 | (defgroup midnight nil | |
42 | "Run something every day at midnight." | |
43 | :group 'calendar) | |
44 | ||
e9d7cff0 RS |
45 | (defcustom midnight-mode t |
46 | "*Non-nil means run `midnight-hook' at midnight. | |
47 | Setting this variable outside customize has no effect; | |
48 | call `cancel-timer' or `timer-activate' on `midnight-timer' instead." | |
49 | :type 'boolean | |
50 | :group 'midnight | |
51 | :require 'midnight | |
52 | :version "20.3" | |
53 | :set (lambda (symb val) | |
54 | (set symb val) (require 'midnight) | |
55 | (if val (timer-activate midnight-timer) | |
56 | (cancel-timer midnight-timer)))) | |
57 | ||
ac06bd0f RS |
58 | ;;; time conversion |
59 | ||
60 | (defun float-time (&optional tm) | |
61 | "Convert `current-time' to a float number of seconds." | |
62 | (multiple-value-bind (s0 s1 s2) (or tm (current-time)) | |
63 | (+ (* (float (ash 1 16)) s0) (float s1) (* 0.0000001 s2)))) | |
64 | ||
65 | (defun time-float (num) | |
66 | "Convert the float number of seconds since epoch to the list of 3 integers." | |
67 | (let* ((div (ash 1 16)) (1st (floor num div))) | |
68 | (list 1st (floor (- num (* (float div) 1st))) | |
69 | (round (* 10000000 (mod num 1)))))) | |
70 | ||
71 | (defun buffer-display-time (&optional buf) | |
72 | "Return the time-stamp of the given buffer, or current buffer, as float." | |
73 | (save-excursion | |
74 | (set-buffer (or buf (current-buffer))) | |
75 | (when buffer-display-time (float-time buffer-display-time)))) | |
76 | ||
77 | ;;; clean-buffer-list stuff | |
78 | ||
79 | (defcustom clean-buffer-list-delay-general 3 | |
80 | "*The number of days before any buffer becomes eligible for autokilling. | |
81 | The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'. | |
82 | Currently displayed and/or modified (unsaved) buffers, as well as buffers | |
83 | matching `clean-buffer-list-kill-never-buffer-names' and | |
84 | `clean-buffer-list-kill-never-regexps' are excluded." | |
85 | :type 'integer | |
86 | :group 'midnight | |
87 | :version "20.3") | |
88 | ||
89 | (defcustom clean-buffer-list-delay-special 3600 | |
90 | "*The number of seconds before some buffers become eligible for autokilling. | |
91 | Buffers matched by `clean-buffer-list-kill-regexps' and | |
92 | `clean-buffer-list-kill-buffer-names' are killed if they were last | |
93 | displayed more than this many seconds ago." | |
94 | :type 'integer | |
95 | :group 'midnight | |
96 | :version "20.3") | |
97 | ||
98 | (defcustom clean-buffer-list-kill-regexps nil | |
99 | "*List of regexps saying which buffers will be killed at midnight. | |
100 | If buffer name matches a regexp in the list and the buffer was not displayed | |
101 | in the last `clean-buffer-list-delay-special' seconds, it is killed by | |
102 | `clean-buffer-list' when is it in `midnight-hook'. | |
103 | If a member of the list is a cons, it's `car' is the regexp and its `cdr' is | |
104 | the number of seconds to use instead of `clean-buffer-list-delay-special'. | |
105 | See also `clean-buffer-list-kill-buffer-names', | |
106 | `clean-buffer-list-kill-never-regexps' and | |
107 | `clean-buffer-list-kill-never-buffer-names'." | |
108 | :type 'list | |
109 | :group 'midnight | |
110 | :version "20.3") | |
111 | ||
112 | (defcustom clean-buffer-list-kill-buffer-names | |
113 | '("*Help*" "*Apropos*" "*Man " "*Buffer List*" "*Compile-Log*" "*info*") | |
114 | "*List of strings saying which buffers will be killed at midnight. | |
115 | Buffers with names in this list, which were not displayed in the last | |
116 | `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list' | |
117 | when is it in `midnight-hook'. | |
118 | If a member of the list is a cons, it's `car' is the name and its `cdr' is | |
119 | the number of seconds to use instead of `clean-buffer-list-delay-special'. | |
120 | See also `clean-buffer-list-kill-regexps', | |
121 | `clean-buffer-list-kill-never-regexps' and | |
122 | `clean-buffer-list-kill-never-buffer-names'." | |
123 | :type 'list | |
124 | :group 'midnight | |
125 | :version "20.3") | |
126 | ||
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 'list | |
135 | :group 'midnight | |
136 | :version "20.3") | |
137 | ||
138 | (defcustom clean-buffer-list-kill-never-regexps '("^ \*Minibuf-.*\*$") | |
139 | "*List of regexp saying which buffers will never be killed at midnight. | |
140 | See also `clean-buffer-list-kill-never-buffer-names'. | |
141 | Killing is done by `clean-buffer-list'. | |
142 | Note that this does override `clean-buffer-list-kill-regexps' and | |
143 | `clean-buffer-list-kill-buffer-names' so a buffer matching any of these | |
144 | two lists will NOT be killed if it also matches anything in this list." | |
145 | :type 'list | |
146 | :group 'midnight | |
147 | :version "20.3") | |
148 | ||
149 | (defun midnight-find (el ls test &optional key) | |
150 | "A stopgap solution to the absence of `find' in ELisp." | |
151 | (if (fboundp 'find) | |
152 | (find el ls :test test :key (or key 'eql)) | |
153 | (loop for rr in ls when (funcall test el (if key (funcall key rr) rr)) | |
154 | return rr))) | |
155 | ||
156 | (defun clean-buffer-list-delay (bn) | |
157 | "Return the delay, in seconds, before this buffer name is auto-killed. | |
158 | Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps' | |
159 | `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'. | |
160 | Autokilling is done by `clean-buffer-list'." | |
161 | (flet ((ff (ls ts) | |
162 | (let ((zz (midnight-find | |
163 | bn ls ts (lambda (xx) (if (consp xx) (car xx) xx))))) | |
164 | (cond ((consp zz) (cdr zz)) | |
165 | ((null zz) nil) | |
166 | (clean-buffer-list-delay-special))))) | |
167 | (or (ff clean-buffer-list-kill-buffer-names 'string=) | |
168 | (ff clean-buffer-list-kill-regexps 'string-match) | |
169 | (* clean-buffer-list-delay-general 24 60 60)))) | |
170 | ||
171 | (defun clean-buffer-list () | |
172 | "Kill old buffers. | |
173 | The relevant vartiables are `clean-buffer-list-delay-general', | |
174 | `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names', | |
175 | `clean-buffer-list-kill-never-buffer-names', | |
176 | `clean-buffer-list-kill-regexps' and `clean-buffer-list-kill-never-regexps'." | |
177 | (interactive) | |
178 | (let ((tm (float-time)) bts (ts (format-time-string "%Y-%m-%d %T")) bn) | |
179 | (dolist (buf (buffer-list)) | |
180 | (message "[%s] processing `%s'..." ts buf) | |
181 | (setq bts (buffer-display-time buf) bn (buffer-name buf)) | |
182 | (unless (or ;; (string-match clean-buffer-list-kill-never bn) | |
183 | (midnight-find bn clean-buffer-list-kill-never-regexps | |
184 | 'string-match) | |
185 | (midnight-find bn clean-buffer-list-kill-never-buffer-names | |
186 | 'string-equal) | |
187 | (buffer-modified-p buf) (get-buffer-window buf 'visible) | |
188 | (null bts) (< (- tm bts) (clean-buffer-list-delay bn))) | |
189 | (message "[%s] killing `%s'" ts bn) | |
190 | (kill-buffer buf))))) | |
191 | ||
192 | ;;; midnight hook | |
193 | ||
194 | (defvar midnight-period (* 24 60 60) | |
195 | "The number of seconds in a day - the delta for `midnight-timer'.") | |
196 | ||
197 | (defcustom midnight-hook 'clean-buffer-list | |
198 | "The hook run `midnight-delay' seconds after midnight every day. | |
199 | The default value is `clean-buffer-list'." | |
200 | :type 'hook | |
201 | :group 'midnight | |
202 | :version "20.3") | |
203 | ||
204 | (defun midnight-next () | |
205 | "Return the number of seconds till the next midnight." | |
206 | (multiple-value-bind (sec min hrs) (decode-time) | |
207 | (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec))) | |
208 | ||
209 | (defvar midnight-timer nil | |
210 | "Timer running the `midnight-hook' `midnight-delay' seconds after midnight. | |
211 | Use `cancel-timer' to stop it and `midnight-delay-set' to change | |
212 | the time when it is run.") | |
213 | ||
214 | ;;;###autoload | |
215 | (defun midnight-delay-set (symb tm) | |
216 | "Modify `midnight-timer' according to `midnight-delay'. | |
217 | Sets the first argument (which must be symbol `midnight-delay') | |
218 | to its second argument." | |
219 | (unless (eq symb 'midnight-delay) | |
220 | (error "Illegal argument to `midnight-delay-set': `%s'" symb)) | |
221 | (set symb tm) | |
222 | (when (timerp midnight-timer) (cancel-timer midnight-timer)) | |
223 | (setq midnight-timer | |
224 | (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm) | |
e9d7cff0 RS |
225 | midnight-period 'midnight-timer-function))) |
226 | ||
227 | (defun midnight-timer-function () | |
228 | "This is the function run by the `midnight-mode' timer once each day." | |
229 | (when midnight-mode | |
230 | (run-hooks 'midnight-hook))) | |
ac06bd0f RS |
231 | |
232 | (defcustom midnight-delay 3600 | |
233 | "*The number of seconds after the midnight when the `midnight-timer' is run. | |
234 | You should set this variable before loading midnight.el, or | |
235 | set it by calling `midnight-delay-set', or use `custom'. | |
236 | If you wish, you can use a string instead, it will be passed as the | |
237 | first argument to `run-at-time'." | |
238 | :type 'sexp | |
239 | :set 'midnight-delay-set | |
240 | :group 'midnight | |
241 | :version "20.3") | |
242 | ||
243 | (provide 'midnight) | |
244 | ||
245 | ;;; midnight.el ends here |