Mercurial > emacs
comparison lisp/midnight.el @ 22443:23e135b67081
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 12 Jun 1998 02:04:21 +0000 |
parents | |
children | 5f8133b3c592 |
comparison
equal
deleted
inserted
replaced
22442:e1d07ef2af10 | 22443:23e135b67081 |
---|---|
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 | |
45 ;;; time conversion | |
46 | |
47 (defun float-time (&optional tm) | |
48 "Convert `current-time' to a float number of seconds." | |
49 (multiple-value-bind (s0 s1 s2) (or tm (current-time)) | |
50 (+ (* (float (ash 1 16)) s0) (float s1) (* 0.0000001 s2)))) | |
51 | |
52 (defun time-float (num) | |
53 "Convert the float number of seconds since epoch to the list of 3 integers." | |
54 (let* ((div (ash 1 16)) (1st (floor num div))) | |
55 (list 1st (floor (- num (* (float div) 1st))) | |
56 (round (* 10000000 (mod num 1)))))) | |
57 | |
58 (defun buffer-display-time (&optional buf) | |
59 "Return the time-stamp of the given buffer, or current buffer, as float." | |
60 (save-excursion | |
61 (set-buffer (or buf (current-buffer))) | |
62 (when buffer-display-time (float-time buffer-display-time)))) | |
63 | |
64 ;;; clean-buffer-list stuff | |
65 | |
66 (defcustom clean-buffer-list-delay-general 3 | |
67 "*The number of days before any buffer becomes eligible for autokilling. | |
68 The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'. | |
69 Currently displayed and/or modified (unsaved) buffers, as well as buffers | |
70 matching `clean-buffer-list-kill-never-buffer-names' and | |
71 `clean-buffer-list-kill-never-regexps' are excluded." | |
72 :type 'integer | |
73 :group 'midnight | |
74 :version "20.3") | |
75 | |
76 (defcustom clean-buffer-list-delay-special 3600 | |
77 "*The number of seconds before some buffers become eligible for autokilling. | |
78 Buffers matched by `clean-buffer-list-kill-regexps' and | |
79 `clean-buffer-list-kill-buffer-names' are killed if they were last | |
80 displayed more than this many seconds ago." | |
81 :type 'integer | |
82 :group 'midnight | |
83 :version "20.3") | |
84 | |
85 (defcustom clean-buffer-list-kill-regexps nil | |
86 "*List of regexps saying which buffers will be killed at midnight. | |
87 If buffer name matches a regexp in the list and the buffer was not displayed | |
88 in the last `clean-buffer-list-delay-special' seconds, it is killed by | |
89 `clean-buffer-list' when is it in `midnight-hook'. | |
90 If a member of the list is a cons, it's `car' is the regexp and its `cdr' is | |
91 the number of seconds to use instead of `clean-buffer-list-delay-special'. | |
92 See also `clean-buffer-list-kill-buffer-names', | |
93 `clean-buffer-list-kill-never-regexps' and | |
94 `clean-buffer-list-kill-never-buffer-names'." | |
95 :type 'list | |
96 :group 'midnight | |
97 :version "20.3") | |
98 | |
99 (defcustom clean-buffer-list-kill-buffer-names | |
100 '("*Help*" "*Apropos*" "*Man " "*Buffer List*" "*Compile-Log*" "*info*") | |
101 "*List of strings saying which buffers will be killed at midnight. | |
102 Buffers with names in this list, which were not displayed in the last | |
103 `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list' | |
104 when is it in `midnight-hook'. | |
105 If a member of the list is a cons, it's `car' is the name and its `cdr' is | |
106 the number of seconds to use instead of `clean-buffer-list-delay-special'. | |
107 See also `clean-buffer-list-kill-regexps', | |
108 `clean-buffer-list-kill-never-regexps' and | |
109 `clean-buffer-list-kill-never-buffer-names'." | |
110 :type 'list | |
111 :group 'midnight | |
112 :version "20.3") | |
113 | |
114 (defcustom clean-buffer-list-kill-never-buffer-names | |
115 '("*scratch*" "*Messages*") | |
116 "*List of buffer names which will never be killed by `clean-buffer-list'. | |
117 See also `clean-buffer-list-kill-never-regexps'. | |
118 Note that this does override `clean-buffer-list-kill-regexps' and | |
119 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these | |
120 two lists will NOT be killed if it is also present in this list." | |
121 :type 'list | |
122 :group 'midnight | |
123 :version "20.3") | |
124 | |
125 (defcustom clean-buffer-list-kill-never-regexps '("^ \*Minibuf-.*\*$") | |
126 "*List of regexp saying which buffers will never be killed at midnight. | |
127 See also `clean-buffer-list-kill-never-buffer-names'. | |
128 Killing is done by `clean-buffer-list'. | |
129 Note that this does override `clean-buffer-list-kill-regexps' and | |
130 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these | |
131 two lists will NOT be killed if it also matches anything in this list." | |
132 :type 'list | |
133 :group 'midnight | |
134 :version "20.3") | |
135 | |
136 (defun midnight-find (el ls test &optional key) | |
137 "A stopgap solution to the absence of `find' in ELisp." | |
138 (if (fboundp 'find) | |
139 (find el ls :test test :key (or key 'eql)) | |
140 (loop for rr in ls when (funcall test el (if key (funcall key rr) rr)) | |
141 return rr))) | |
142 | |
143 (defun clean-buffer-list-delay (bn) | |
144 "Return the delay, in seconds, before this buffer name is auto-killed. | |
145 Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps' | |
146 `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'. | |
147 Autokilling is done by `clean-buffer-list'." | |
148 (flet ((ff (ls ts) | |
149 (let ((zz (midnight-find | |
150 bn ls ts (lambda (xx) (if (consp xx) (car xx) xx))))) | |
151 (cond ((consp zz) (cdr zz)) | |
152 ((null zz) nil) | |
153 (clean-buffer-list-delay-special))))) | |
154 (or (ff clean-buffer-list-kill-buffer-names 'string=) | |
155 (ff clean-buffer-list-kill-regexps 'string-match) | |
156 (* clean-buffer-list-delay-general 24 60 60)))) | |
157 | |
158 (defun clean-buffer-list () | |
159 "Kill old buffers. | |
160 The relevant vartiables are `clean-buffer-list-delay-general', | |
161 `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names', | |
162 `clean-buffer-list-kill-never-buffer-names', | |
163 `clean-buffer-list-kill-regexps' and `clean-buffer-list-kill-never-regexps'." | |
164 (interactive) | |
165 (let ((tm (float-time)) bts (ts (format-time-string "%Y-%m-%d %T")) bn) | |
166 (dolist (buf (buffer-list)) | |
167 (message "[%s] processing `%s'..." ts buf) | |
168 (setq bts (buffer-display-time buf) bn (buffer-name buf)) | |
169 (unless (or ;; (string-match clean-buffer-list-kill-never bn) | |
170 (midnight-find bn clean-buffer-list-kill-never-regexps | |
171 'string-match) | |
172 (midnight-find bn clean-buffer-list-kill-never-buffer-names | |
173 'string-equal) | |
174 (buffer-modified-p buf) (get-buffer-window buf 'visible) | |
175 (null bts) (< (- tm bts) (clean-buffer-list-delay bn))) | |
176 (message "[%s] killing `%s'" ts bn) | |
177 (kill-buffer buf))))) | |
178 | |
179 ;;; midnight hook | |
180 | |
181 (defvar midnight-period (* 24 60 60) | |
182 "The number of seconds in a day - the delta for `midnight-timer'.") | |
183 | |
184 (defcustom midnight-hook 'clean-buffer-list | |
185 "The hook run `midnight-delay' seconds after midnight every day. | |
186 The default value is `clean-buffer-list'." | |
187 :type 'hook | |
188 :group 'midnight | |
189 :version "20.3") | |
190 | |
191 (defun midnight-next () | |
192 "Return the number of seconds till the next midnight." | |
193 (multiple-value-bind (sec min hrs) (decode-time) | |
194 (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec))) | |
195 | |
196 (defvar midnight-timer nil | |
197 "Timer running the `midnight-hook' `midnight-delay' seconds after midnight. | |
198 Use `cancel-timer' to stop it and `midnight-delay-set' to change | |
199 the time when it is run.") | |
200 | |
201 ;;;###autoload | |
202 (defun midnight-delay-set (symb tm) | |
203 "Modify `midnight-timer' according to `midnight-delay'. | |
204 Sets the first argument (which must be symbol `midnight-delay') | |
205 to its second argument." | |
206 (unless (eq symb 'midnight-delay) | |
207 (error "Illegal argument to `midnight-delay-set': `%s'" symb)) | |
208 (set symb tm) | |
209 (when (timerp midnight-timer) (cancel-timer midnight-timer)) | |
210 (setq midnight-timer | |
211 (run-at-time (if (numberp tm) (+ (midnight-next) tm) tm) | |
212 midnight-period 'run-hooks 'midnight-hook))) | |
213 | |
214 (defcustom midnight-delay 3600 | |
215 "*The number of seconds after the midnight when the `midnight-timer' is run. | |
216 You should set this variable before loading midnight.el, or | |
217 set it by calling `midnight-delay-set', or use `custom'. | |
218 If you wish, you can use a string instead, it will be passed as the | |
219 first argument to `run-at-time'." | |
220 :type 'sexp | |
221 :set 'midnight-delay-set | |
222 :group 'midnight | |
223 :version "20.3") | |
224 | |
225 (provide 'midnight) | |
226 | |
227 ;;; midnight.el ends here |