Merge branch 'master' into comment-cache
[emacs.git] / lisp / midnight.el
blobb9893fbfcedda51daef3f8d775721c9d58443128
1 ;;; midnight.el --- run something every midnight, e.g., kill old buffers -*- lexical-binding:t -*-
3 ;; Copyright (C) 1998, 2001-2017 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 ;;; clean-buffer-list stuff
65 (defcustom clean-buffer-list-delay-general 3
66 "The number of days before any buffer becomes eligible for autokilling.
67 The autokilling is done by `clean-buffer-list' when is it in `midnight-hook'.
68 Currently displayed and/or modified (unsaved) buffers, as well as buffers
69 matching `clean-buffer-list-kill-never-buffer-names' and
70 `clean-buffer-list-kill-never-regexps' are excluded."
71 :type 'integer)
73 (defcustom clean-buffer-list-delay-special 3600
74 "The number of seconds before some buffers become eligible for autokilling.
75 Buffers matched by `clean-buffer-list-kill-regexps' and
76 `clean-buffer-list-kill-buffer-names' are killed if they were last
77 displayed more than this many seconds ago."
78 :type 'integer)
80 (defcustom clean-buffer-list-kill-regexps '("\\`\\*Man ")
81 "List of regexps saying which buffers will be killed at midnight.
82 If buffer name matches a regexp in the list and the buffer was not displayed
83 in the last `clean-buffer-list-delay-special' seconds, it is killed by
84 `clean-buffer-list' when is it in `midnight-hook'.
85 If a member of the list is a cons, its `car' is the regexp and its `cdr' is
86 the number of seconds to use instead of `clean-buffer-list-delay-special'.
87 See also `clean-buffer-list-kill-buffer-names',
88 `clean-buffer-list-kill-never-regexps' and
89 `clean-buffer-list-kill-never-buffer-names'.
91 Each element can also be a function instead of a regexp, in which case
92 it takes a single argument (a buffer name) and should return non-nil
93 if the buffer should be killed by `clean-buffer-list'."
94 :type '(repeat
95 (choice (regexp :tag "Regexp matching Buffer Name")
96 (function :tag "Predicate function"))))
98 (defcustom clean-buffer-list-kill-buffer-names
99 '("*Help*" "*Apropos*" "*Buffer List*" "*Compile-Log*" "*info*"
100 "*vc*" "*vc-diff*" "*diff*")
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, its `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 '(repeat (string :tag "Buffer Name")))
112 (defcustom clean-buffer-list-kill-never-buffer-names
113 '("*scratch*" "*Messages*")
114 "List of buffer names which will never be killed by `clean-buffer-list'.
115 See also `clean-buffer-list-kill-never-regexps'.
116 Note that this does override `clean-buffer-list-kill-regexps' and
117 `clean-buffer-list-kill-buffer-names' so a buffer matching any of these
118 two lists will NOT be killed if it is also present in this list."
119 :type '(repeat (string :tag "Buffer Name")))
121 (defcustom clean-buffer-list-kill-never-regexps '("\\` \\*Minibuf-.*\\*\\'")
122 "List of regexp saying which buffers will never be killed at midnight.
123 See also `clean-buffer-list-kill-never-buffer-names'.
124 Killing is done by `clean-buffer-list'.
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 also matches anything in this list.
129 Each element can also be a function instead of a regexp, in which case
130 it takes a single argument (a buffer name) and should return non-nil
131 if the buffer should never be killed by `clean-buffer-list'."
132 :type '(repeat
133 (choice (regexp :tag "Regexp matching Buffer Name")
134 (function :tag "Predicate function"))))
136 (defun clean-buffer-list-delay (name)
137 "Return the delay, in seconds, before killing a buffer named NAME.
138 Uses `clean-buffer-list-kill-buffer-names', `clean-buffer-list-kill-regexps'
139 `clean-buffer-list-delay-general' and `clean-buffer-list-delay-special'.
140 Autokilling is done by `clean-buffer-list'."
141 (or (assoc-default name clean-buffer-list-kill-buffer-names #'string=
142 clean-buffer-list-delay-special)
143 (assoc-default name clean-buffer-list-kill-regexps
144 (lambda (re str)
145 (if (functionp re)
146 (funcall re str) (string-match re str)))
147 clean-buffer-list-delay-special)
148 (* clean-buffer-list-delay-general 24 60 60)))
150 ;;;###autoload
151 (defun clean-buffer-list ()
152 "Kill old buffers that have not been displayed recently.
153 The relevant variables are `clean-buffer-list-delay-general',
154 `clean-buffer-list-delay-special', `clean-buffer-list-kill-buffer-names',
155 `clean-buffer-list-kill-never-buffer-names',
156 `clean-buffer-list-kill-regexps' and
157 `clean-buffer-list-kill-never-regexps'.
158 While processing buffers, this procedure displays messages containing
159 the current date/time, buffer name, how many seconds ago it was
160 displayed (can be nil if the buffer was never displayed) and its
161 lifetime, i.e., its \"age\" when it will be purged."
162 (interactive)
163 (let ((tm (current-time)) bts (ts (format-time-string "%Y-%m-%d %T"))
164 delay cbld bn)
165 (dolist (buf (buffer-list))
166 (when (buffer-live-p buf)
167 (setq bts (with-current-buffer buf buffer-display-time)
168 bn (buffer-name buf)
169 delay (if bts (round (float-time (time-subtract tm bts))) 0)
170 cbld (clean-buffer-list-delay bn))
171 (message "[%s] `%s' [%s %d]" ts bn delay cbld)
172 (unless (or (cl-find bn clean-buffer-list-kill-never-regexps
173 :test (lambda (bn re)
174 (if (functionp re)
175 (funcall re bn)
176 (string-match re bn))))
177 (cl-find bn clean-buffer-list-kill-never-buffer-names
178 :test #'string-equal)
179 (get-buffer-process buf)
180 (and (buffer-file-name buf) (buffer-modified-p buf))
181 (get-buffer-window buf 'visible)
182 (< delay cbld))
183 (message "[%s] killing `%s'" ts bn)
184 (kill-buffer buf))))))
186 ;;; midnight hook
188 (defvar midnight-period (* 24 60 60)
189 "The number of seconds in a day--the delta for `midnight-timer'.")
191 (defcustom midnight-hook '(clean-buffer-list)
192 "The hook run `midnight-delay' seconds after midnight every day.
193 The default value is `clean-buffer-list'."
194 :type 'hook)
196 (defun midnight-next ()
197 "Return the number of seconds till the next midnight."
198 (pcase-let ((`(,sec ,min ,hrs) (decode-time)))
199 (- (* 24 60 60) (* 60 60 hrs) (* 60 min) sec)))
201 ;;;###autoload
202 (defun midnight-delay-set (symb tm)
203 "Modify `midnight-timer' according to `midnight-delay'.
204 Sets the first argument SYMB (which must be symbol `midnight-delay')
205 to its second argument TM."
206 (cl-assert (eq symb 'midnight-delay) t
207 "Invalid argument to `midnight-delay-set': `%s'")
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)))
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 '(choice integer string)
221 :set #'midnight-delay-set)
223 (provide 'midnight)
225 ;;; midnight.el ends here