Add new function dom-remove-node
[emacs.git] / lisp / midnight.el
blob878c5a7f71fcd69eb94604ac589b9f6318794389
1 ;;; midnight.el --- run something every midnight, e.g., kill old buffers -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2001-2016 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 (require 'cl-lib)
41 (defgroup midnight nil
42 "Run something every day at midnight."
43 :group 'calendar
44 :version "20.3")
46 (defvar midnight-timer nil
47 "Timer running the `midnight-hook' `midnight-delay' seconds after midnight.
48 Use `cancel-timer' to stop it and `midnight-delay-set' to change
49 the time when it is run.")
51 ;;;###autoload
52 (define-minor-mode midnight-mode
53 "Non-nil means run `midnight-hook' at midnight."
54 :global t
55 :initialize #'custom-initialize-default
56 ;; Disable first, since the ':initialize' function above already
57 ;; starts the timer when the mode is turned on for the first time,
58 ;; via setting 'midnight-delay', which calls 'midnight-delay-set',
59 ;; which starts the timer.
60 (when (timerp midnight-timer) (cancel-timer midnight-timer))
61 (if midnight-mode (timer-activate midnight-timer)))
63 ;;; time conversion
65 (defun midnight-buffer-display-time (buffer)
66 "Return the time-stamp of BUFFER, or current buffer, as float."
67 (with-current-buffer buffer
68 (when buffer-display-time (float-time buffer-display-time))))
70 ;;; clean-buffer-list stuff
72 (defcustom clean-buffer-list-delay-general 3
73 "The number of days before any buffer becomes eligible for autokilling.
74 The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
75 Currently displayed and/or modified (unsaved) buffers, as well as buffers
76 matching `clean-buffer-list-kill-never-buffer-names' and
77 `clean-buffer-list-kill-never-regexps' are excluded."
78 :type 'integer)
80 (defcustom clean-buffer-list-delay-special 3600
81 "The number of seconds before some buffers become eligible for autokilling.
82 Buffers matched by `clean-buffer-list-kill-regexps' and
83 `clean-buffer-list-kill-buffer-names' are killed if they were last
84 displayed more than this many seconds ago."
85 :type 'integer)
87 (defcustom clean-buffer-list-kill-regexps '("\\`\\*Man ")
88 "List of regexps saying which buffers will be killed at midnight.
89 If buffer name matches a regexp in the list and the buffer was not displayed
90 in the last `clean-buffer-list-delay-special' seconds, it is killed by
91 `clean-buffer-list' when is it in `midnight-hook'.
92 If a member of the list is a cons, its `car' is the regexp and its `cdr' is
93 the number of seconds to use instead of `clean-buffer-list-delay-special'.
94 See also `clean-buffer-list-kill-buffer-names',
95 `clean-buffer-list-kill-never-regexps' and
96 `clean-buffer-list-kill-never-buffer-names'.
98 Each element can also be a function instead of a regexp, in which case
99 it takes a single argument (a buffer name) and should return non-nil
100 if the buffer should be killed by `clean-buffer-list'."
101 :type '(repeat
102 (choice (regexp :tag "Regexp matching Buffer Name")
103 (function :tag "Predicate function"))))
105 (defcustom clean-buffer-list-kill-buffer-names
106 '("*Help*" "*Apropos*" "*Buffer List*" "*Compile-Log*" "*info*"
107 "*vc*" "*vc-diff*" "*diff*")
108 "List of strings saying which buffers will be killed at midnight.
109 Buffers with names in this list, which were not displayed in the last
110 `clean-buffer-list-delay-special' seconds, are killed by `clean-buffer-list'
111 when is it in `midnight-hook'.
112 If a member of the list is a cons, its `car' is the name and its `cdr' is
113 the number of seconds to use instead of `clean-buffer-list-delay-special'.
114 See also `clean-buffer-list-kill-regexps',
115 `clean-buffer-list-kill-never-regexps' and
116 `clean-buffer-list-kill-never-buffer-names'."
117 :type '(repeat (string :tag "Buffer Name")))
119 (defcustom clean-buffer-list-kill-never-buffer-names
120 '("*scratch*" "*Messages*")
121 "List of buffer names which will never be killed by `clean-buffer-list'.
122 See also `clean-buffer-list-kill-never-regexps'.
123 Note that this does override `clean-buffer-list-kill-regexps' and
124 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
125 two lists will NOT be killed if it is also present in this list."
126 :type '(repeat (string :tag "Buffer Name")))
128 (defcustom clean-buffer-list-kill-never-regexps '("\\` \\*Minibuf-.*\\*\\'")
129 "List of regexp saying which buffers will never be killed at midnight.
130 See also `clean-buffer-list-kill-never-buffer-names'.
131 Killing is done by `clean-buffer-list'.
132 Note that this does override `clean-buffer-list-kill-regexps' and
133 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
134 two lists will NOT be killed if it also matches anything in this list.
136 Each element can also be a function instead of a regexp, in which case
137 it takes a single argument (a buffer name) and should return non-nil
138 if the buffer should never be killed by `clean-buffer-list'."
139 :type '(repeat
140 (choice (regexp :tag "Regexp matching Buffer Name")
141 (function :tag "Predicate function"))))
143 (defun clean-buffer-list-delay (name)
144 "Return the delay, in seconds, before killing a buffer named NAME.
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 (or (assoc-default name clean-buffer-list-kill-buffer-names #'string=
149 clean-buffer-list-delay-special)
150 (assoc-default name clean-buffer-list-kill-regexps
151 (lambda (re str)
152 (if (functionp re)
153 (funcall re str) (string-match re str)))
154 clean-buffer-list-delay-special)
155 (* clean-buffer-list-delay-general 24 60 60)))
157 ;;;###autoload
158 (defun clean-buffer-list ()
159 "Kill old buffers that have not been displayed recently.
160 The relevant variables 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
164 `clean-buffer-list-kill-never-regexps'.
165 While processing buffers, this procedure displays messages containing
166 the current date/time, buffer name, how many seconds ago it was
167 displayed (can be nil if the buffer was never displayed) and its
168 lifetime, i.e., its \"age\" when it will be purged."
169 (interactive)
170 (let ((tm (float-time)) bts (ts (format-time-string "%Y-%m-%d %T"))
171 delay cbld bn)
172 (dolist (buf (buffer-list))
173 (when (buffer-live-p buf)
174 (setq bts (midnight-buffer-display-time buf) bn (buffer-name buf)
175 delay (if bts (- tm bts) 0) cbld (clean-buffer-list-delay bn))
176 (message "[%s] `%s' [%s %d]" ts bn (if bts (round delay)) cbld)
177 (unless (or (cl-find bn clean-buffer-list-kill-never-regexps
178 :test (lambda (bn re)
179 (if (functionp re)
180 (funcall re bn)
181 (string-match re bn))))
182 (cl-find bn clean-buffer-list-kill-never-buffer-names
183 :test #'string-equal)
184 (get-buffer-process buf)
185 (and (buffer-file-name buf) (buffer-modified-p buf))
186 (get-buffer-window buf 'visible) (< delay cbld))
187 (message "[%s] killing `%s'" ts bn)
188 (kill-buffer buf))))))
190 ;;; midnight hook
192 (defvar midnight-period (* 24 60 60)
193 "The number of seconds in a day--the delta for `midnight-timer'.")
195 (defcustom midnight-hook '(clean-buffer-list)
196 "The hook run `midnight-delay' seconds after midnight every day.
197 The default value is `clean-buffer-list'."
198 :type 'hook)
200 (defun midnight-next ()
201 "Return the number of seconds till the next midnight."
202 (pcase-let ((`(,sec ,min ,hrs) (decode-time)))
203 (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec)))
205 ;;;###autoload
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 (cl-assert (eq symb 'midnight-delay) t
211 "Invalid argument to `midnight-delay-set': `%s'")
212 (set symb tm)
213 (when (timerp midnight-timer) (cancel-timer midnight-timer))
214 (setq 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'."
224 :type '(choice integer string)
225 :set #'midnight-delay-set)
227 (provide 'midnight)
229 ;;; midnight.el ends here