1 ;; autorevert --- Revert buffers when file on disk change.
3 ;; Copyright (C) 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren <andersl@csd.uu.se>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 ;; Whenever a file that Emacs is editing has been changed by another
31 ;; program the user normally has to execute the command `revert-buffer'
32 ;; to load the new content of the file into Emacs.
34 ;; This package contains two minor modes: Global Auto-Revert Mode and
35 ;; Auto-Revert Mode. Both modes automatically revert buffers
36 ;; whenever the corresponding files have been changed on disk.
38 ;; Auto-Revert Mode can be activated for individual buffers.
39 ;; Global Auto-Revert Mode applies to all file buffers.
41 ;; Both modes operate by checking the time stamp of all files at
42 ;; intervals of `auto-revert-interval'. The default is every five
43 ;; seconds. The check is aborted whenever the user actually uses
44 ;; Emacs. You should never even notice that this package is active
45 ;; (except that your buffers will be reverted, of course).
49 ;; Go to the appropriate buffer and press:
50 ;; M-x auto-revert-mode RET
52 ;; To activate Global Auto-Revert Mode, press:
53 ;; M-x global-auto-revert-mode RET
55 ;; To activate Global Auto-Revert Mode every time Emacs is started
56 ;; customise the option `global-auto-revert-mode' or the following
57 ;; line could be added to your ~/.emacs:
58 ;; (global-auto-revert-mode 1)
60 ;; The function `turn-on-auto-revert-mode' could be added to any major
61 ;; mode hook to activate Auto-Revert Mode for all buffers in that
62 ;; mode. For example, the following line will activate Auto-Revert
63 ;; Mode in all C mode buffers:
65 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
72 (eval-when-compile (require 'cl))
77 ;; The two modes will be placed next to Auto Save Mode under the
78 ;; Files group under Emacs.
80 (defgroup auto-revert nil
81 "Revert individual buffers when files on disk change.
83 Auto-Revert Mode can be activated for individual buffer.
84 Global Auto-Revert Mode applies to all buffers."
90 (defvar auto-revert-mode nil
91 "*Non-nil when Auto-Revert Mode is active.
93 Never set this variable directly, use the command `auto-revert-mode'
96 (defcustom global-auto-revert-mode nil
97 "When on, buffers are automatically reverted when files on disk change.
99 Set this variable using \\[customize] only. Otherwise, use the
100 command `global-auto-revert-mode'."
102 :initialize 'custom-initialize-default
103 :set '(lambda (symbol value)
104 (global-auto-revert-mode (or value 0)))
106 :require 'autorevert)
108 (defcustom auto-revert-interval 5
109 "Time, in seconds, between Auto-Revert Mode file checks."
113 (defcustom auto-revert-stop-on-user-input t
114 "When non-nil Auto-Revert Mode stops checking files on user input."
118 (defcustom auto-revert-verbose t
119 "When nil, Auto-Revert Mode will not generate any messages.
121 Currently, messages are generated when the mode is activated or
122 deactivated, and whenever a file is reverted."
126 (defcustom auto-revert-mode-text " ARev"
127 "String to display in the mode line when Auto-Revert Mode is active.
129 \(When the string is not empty, make sure that it has a leading space.)"
130 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
134 (defcustom auto-revert-mode-hook nil
135 "Functions to run when Auto-Revert Mode is activated."
136 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
140 (defcustom global-auto-revert-mode-text ""
141 "String to display when Global Auto-Revert Mode is active.
143 The default is nothing since when this mode is active this text doesn't
144 vary over time, or between buffers. Hence mode line text
145 would only waste precious space."
149 (defcustom global-auto-revert-mode-hook nil
150 "Hook called when Global Auto-Revert Mode is activated."
154 (defcustom global-auto-revert-non-file-buffers nil
155 "*When nil only file buffers are reverted by Global Auto-Revert Mode.
157 When non-nil, both file buffers and buffers with a custom
158 `revert-buffer-function' are reverted by Global Auto-Revert Mode."
162 (defcustom global-auto-revert-non-file-buffers nil
163 "When nil only file buffers are reverted by Global Auto-Revert Mode.
165 When non-nil, both file buffers and buffers with a custom
166 `revert-buffer-function' are reverted by Global Auto-Revert Mode.
168 Use this option with care since it could lead to excessive reverts."
172 (defcustom global-auto-revert-ignore-modes '()
173 "List of major modes Global Auto-Revert Mode should not check."
175 :type '(repeat sexp))
177 (defcustom auto-revert-load-hook nil
178 "Functions to run when Auto-Revert Mode is first loaded."
183 (defvar global-auto-revert-ignore-buffer nil
184 "*When non-nil, Global Auto-Revert Mode will not revert this buffer.
186 This variable becomes buffer local when set in any fashion.")
187 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
190 ;; Internal variables:
192 (defvar auto-revert-buffer-list '()
193 "List of buffers in Auto-Revert Mode.
195 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
196 buffers to this list.
198 The timer function `auto-revert-buffers' is responsible for purging
199 the list of old buffers.")
201 (defvar auto-revert-timer nil
202 "Timer used by Auto-Revert Mode.")
204 (defvar auto-revert-remaining-buffers '()
205 "Buffers not checked when user input stopped execution.")
211 (defun auto-revert-mode (&optional arg)
212 "Toggle reverting buffer when file on disk changes.
214 With arg, turn Auto Revert mode on if and only if arg is positive.
215 This is a minor mode that affects only the current buffer.
216 Use `global-auto-revert-mode' to automatically revert all buffers."
218 (make-local-variable 'auto-revert-mode)
219 (setq auto-revert-mode
221 (not auto-revert-mode)
222 (> (prefix-numeric-value arg) 0)))
223 (if (and auto-revert-verbose
225 (message "Auto-Revert Mode is now %s."
226 (if auto-revert-mode "on" "off")))
228 (if (not (memq (current-buffer) auto-revert-buffer-list))
229 (push (current-buffer) auto-revert-buffer-list))
230 (setq auto-revert-buffer-list
231 (delq (current-buffer) auto-revert-buffer-list)))
232 (auto-revert-set-timer)
233 (when auto-revert-mode
234 (auto-revert-buffers)
235 (run-hooks 'auto-revert-mode-hook)))
239 (defun turn-on-auto-revert-mode ()
240 "Turn on Auto-Revert Mode.
242 This function is designed to be added to hooks, for example:
243 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
244 (auto-revert-mode 1))
248 (defun global-auto-revert-mode (&optional arg)
249 "Revert any buffer when file on disk change.
251 With arg, turn Auto Revert mode on globally if and only if arg is positive.
252 This is a minor mode that affects all buffers.
253 Use `auto-revert-mode' to revert a particular buffer."
255 (setq global-auto-revert-mode
257 (not global-auto-revert-mode)
258 (> (prefix-numeric-value arg) 0)))
259 (if (and auto-revert-verbose
261 (message "Global Auto-Revert Mode is now %s."
262 (if global-auto-revert-mode "on" "off")))
263 (auto-revert-set-timer)
264 (when global-auto-revert-mode
265 (auto-revert-buffers)
266 (run-hooks 'global-auto-revert-mode-hook)))
269 (defun auto-revert-set-timer ()
270 "Restart or cancel the timer."
271 (if (timerp auto-revert-timer)
272 (cancel-timer auto-revert-timer))
273 (if (or global-auto-revert-mode auto-revert-buffer-list)
274 (setq auto-revert-timer (run-with-timer auto-revert-interval
276 'auto-revert-buffers))
277 (setq auto-revert-timer nil)))
280 (defun auto-revert-buffers ()
281 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
283 Should `global-auto-revert-mode' be active all file buffers are checked.
285 Should `auto-revert-mode' be active in some buffers, those buffers
288 Non-file buffers that have a custom `revert-buffer-function' are
289 reverted either when Auto-Revert Mode is active in that buffer, or
290 when the variable `global-auto-revert-non-file-buffers' is non-nil
291 and Global Auto-Revert Mode is active.
293 This function stops whenever there is user input. The buffers not
294 checked are stored in the variable `auto-revert-remaining-buffers'.
296 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
297 are checked first the next time this function is called.
299 This function is also responsible for removing buffers no longer in
300 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
301 the timer when no buffers need to be checked."
302 (let ((bufs (if global-auto-revert-mode
304 auto-revert-buffer-list))
307 ;; Partition `bufs' into two halves depending on whether or not
308 ;; the buffers are in `auto-revert-remaining-buffers'. The two
309 ;; halves are then re-joined with the "remaining" buffers at the
311 (dolist (buf auto-revert-remaining-buffers)
313 (push buf remaining)))
315 (if (not (memq buf remaining))
317 (setq bufs (nreverse (nconc new remaining)))
319 (not (and auto-revert-stop-on-user-input
321 (let ((buf (car bufs)))
322 (if (buffer-name buf) ; Buffer still alive?
325 ;; Test if someone has turned off Auto-Revert Mode in a
326 ;; non-standard way, for example by changing major mode.
327 (if (and (not auto-revert-mode)
328 (memq buf auto-revert-buffer-list))
329 (setq auto-revert-buffer-list
330 (delq buf auto-revert-buffer-list)))
334 global-auto-revert-mode
335 (not global-auto-revert-ignore-buffer)
336 (not (memq major-mode
337 global-auto-revert-ignore-modes))))
338 (not (buffer-modified-p))
339 (if (buffer-file-name)
340 (and (file-readable-p (buffer-file-name))
341 (not (verify-visited-file-modtime buf)))
342 (and revert-buffer-function
343 (or (and global-auto-revert-mode
344 global-auto-revert-non-file-buffers)
346 (if auto-revert-verbose
347 (message "Reverting buffer `%s'." buf))
348 (revert-buffer t t)))
349 ;; Remove dead buffer from `auto-revert-buffer-list'.
350 (setq auto-revert-buffer-list
351 (delq buf auto-revert-buffer-list))))
352 (setq bufs (cdr bufs)))
353 (setq auto-revert-remaining-buffers bufs)
354 ;; Check if we should cancel the timer.
355 (when (and (not global-auto-revert-mode)
356 (null auto-revert-buffer-list))
357 (cancel-timer auto-revert-timer)
358 (setq auto-revert-timer nil))))
363 (unless (assq 'auto-revert-mode minor-mode-alist)
364 (push '(auto-revert-mode auto-revert-mode-text)
366 (unless (assq 'global-auto-revert-mode minor-mode-alist)
367 (push '(global-auto-revert-mode global-auto-revert-mode-text)
370 (provide 'autorevert)
372 (run-hooks 'auto-revert-load-hook)
374 ;; This makes it possible to set Global Auto-Revert Mode from
376 (if global-auto-revert-mode
377 (global-auto-revert-mode 1))
379 ;; autorevert.el ends here.