]> code.delx.au - gnu-emacs/blob - lisp/autorevert.el
Various doc fixes, mainly grammar.
[gnu-emacs] / lisp / autorevert.el
1 ;; autorevert --- Revert buffers when file on disk change.
2
3 ;; Copyright (C) 1997, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Anders Lindgren <andersl@csd.uu.se>
6 ;; Created: 1 Jun 1997
7 ;; Date: 3 Jul 1997
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Introduction:
29 ;;
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.
33 ;;
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.
37 ;;
38 ;; Auto-Revert Mode can be activated for individual buffers.
39 ;; Global Auto-Revert Mode applies to all file buffers.
40 ;;
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).
46
47 ;; Usage:
48 ;;
49 ;; Go to the appropriate buffer and press:
50 ;; M-x auto-revert-mode RET
51 ;;
52 ;; To activate Global Auto-Revert Mode, press:
53 ;; M-x global-auto-revert-mode RET
54 ;;
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)
59 ;;
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:
64 ;;
65 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
66
67 ;;; Code:
68
69 ;; Dependencies:
70
71 (require 'timer)
72 (eval-when-compile (require 'cl))
73
74
75 ;; Custom Group:
76 ;;
77 ;; The two modes will be placed next to Auto Save Mode under the
78 ;; Files group under Emacs.
79
80 (defgroup auto-revert nil
81 "Revert individual buffers when files on disk change.
82
83 Auto-Revert Mode can be activated for individual buffer.
84 Global Auto-Revert Mode applies to all buffers."
85 :group 'files)
86
87
88 ;; Variables:
89
90 (defvar auto-revert-mode nil
91 "*Non-nil when Auto-Revert Mode is active.
92
93 Never set this variable directly, use the command `auto-revert-mode'
94 instead.")
95
96 (defcustom global-auto-revert-mode nil
97 "When on, buffers are automatically reverted when files on disk change.
98
99 Set this variable using \\[customize] only. Otherwise, use the
100 command `global-auto-revert-mode'."
101 :group 'auto-revert
102 :initialize 'custom-initialize-default
103 :set '(lambda (symbol value)
104 (global-auto-revert-mode (or value 0)))
105 :type 'boolean
106 :require 'autorevert)
107
108 (defcustom auto-revert-interval 5
109 "Time, in seconds, between Auto-Revert Mode file checks."
110 :group 'auto-revert
111 :type 'integer)
112
113 (defcustom auto-revert-stop-on-user-input t
114 "When non-nil Auto-Revert Mode stops checking files on user input."
115 :group 'auto-revert
116 :type 'boolean)
117
118 (defcustom auto-revert-verbose t
119 "When nil, Auto-Revert Mode will not generate any messages.
120
121 Currently, messages are generated when the mode is activated or
122 deactivated, and whenever a file is reverted."
123 :group 'auto-revert
124 :type 'boolean)
125
126 (defcustom auto-revert-mode-text " ARev"
127 "String to display in the mode line when Auto-Revert Mode is active.
128
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-...'
131 :group 'auto-revert
132 :type 'string)
133
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-...'
137 :group 'auto-revert
138 :type 'hook)
139
140 (defcustom global-auto-revert-mode-text ""
141 "String to display when Global Auto-Revert Mode is active.
142
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."
146 :group 'auto-revert
147 :type 'string)
148
149 (defcustom global-auto-revert-mode-hook nil
150 "Hook called when Global Auto-Revert Mode is activated."
151 :group 'auto-revert
152 :type 'hook)
153
154 (defcustom global-auto-revert-non-file-buffers nil
155 "*When nil only file buffers are reverted by Global Auto-Revert Mode.
156
157 When non-nil, both file buffers and buffers with a custom
158 `revert-buffer-function' are reverted by Global Auto-Revert Mode."
159 :group 'auto-revert
160 :type 'boolean)
161
162 (defcustom global-auto-revert-non-file-buffers nil
163 "When nil only file buffers are reverted by Global Auto-Revert Mode.
164
165 When non-nil, both file buffers and buffers with a custom
166 `revert-buffer-function' are reverted by Global Auto-Revert Mode.
167
168 Use this option with care since it could lead to excessive reverts."
169 :group 'auto-revert
170 :type 'boolean)
171
172 (defcustom global-auto-revert-ignore-modes '()
173 "List of major modes Global Auto-Revert Mode should not check."
174 :group 'auto-revert
175 :type '(repeat sexp))
176
177 (defcustom auto-revert-load-hook nil
178 "Functions to run when Auto-Revert Mode is first loaded."
179 :tag "Load Hook"
180 :group 'auto-revert
181 :type 'hook)
182
183 (defvar global-auto-revert-ignore-buffer nil
184 "*When non-nil, Global Auto-Revert Mode will not revert this buffer.
185
186 This variable becomes buffer local when set in any fashion.")
187 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
188
189
190 ;; Internal variables:
191
192 (defvar auto-revert-buffer-list '()
193 "List of buffers in Auto-Revert Mode.
194
195 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
196 buffers to this list.
197
198 The timer function `auto-revert-buffers' is responsible for purging
199 the list of old buffers.")
200
201 (defvar auto-revert-timer nil
202 "Timer used by Auto-Revert Mode.")
203
204 (defvar auto-revert-remaining-buffers '()
205 "Buffers not checked when user input stopped execution.")
206
207
208 ;; Functions:
209
210 ;;;###autoload
211 (defun auto-revert-mode (&optional arg)
212 "Toggle reverting buffer when file on disk changes.
213
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."
217 (interactive "P")
218 (make-local-variable 'auto-revert-mode)
219 (setq auto-revert-mode
220 (if (null arg)
221 (not auto-revert-mode)
222 (> (prefix-numeric-value arg) 0)))
223 (if (and auto-revert-verbose
224 (interactive-p))
225 (message "Auto-Revert Mode is now %s."
226 (if auto-revert-mode "on" "off")))
227 (if auto-revert-mode
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)))
236
237
238 ;;;###autoload
239 (defun turn-on-auto-revert-mode ()
240 "Turn on Auto-Revert Mode.
241
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))
245
246
247 ;;;###autoload
248 (defun global-auto-revert-mode (&optional arg)
249 "Revert any buffer when file on disk change.
250
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."
254 (interactive "P")
255 (setq global-auto-revert-mode
256 (if (null arg)
257 (not global-auto-revert-mode)
258 (> (prefix-numeric-value arg) 0)))
259 (if (and auto-revert-verbose
260 (interactive-p))
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)))
267
268
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
275 auto-revert-interval
276 'auto-revert-buffers))
277 (setq auto-revert-timer nil)))
278
279
280 (defun auto-revert-buffers ()
281 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
282
283 Should `global-auto-revert-mode' be active all file buffers are checked.
284
285 Should `auto-revert-mode' be active in some buffers, those buffers
286 are checked.
287
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.
292
293 This function stops whenever there is user input. The buffers not
294 checked are stored in the variable `auto-revert-remaining-buffers'.
295
296 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
297 are checked first the next time this function is called.
298
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
303 (buffer-list)
304 auto-revert-buffer-list))
305 (remaining '())
306 (new '()))
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
310 ;; head of the list.
311 (dolist (buf auto-revert-remaining-buffers)
312 (if (memq buf bufs)
313 (push buf remaining)))
314 (dolist (buf bufs)
315 (if (not (memq buf remaining))
316 (push buf new)))
317 (setq bufs (nreverse (nconc new remaining)))
318 (while (and bufs
319 (not (and auto-revert-stop-on-user-input
320 (input-pending-p))))
321 (let ((buf (car bufs)))
322 (if (buffer-name buf) ; Buffer still alive?
323 (save-excursion
324 (set-buffer buf)
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)))
331 (when (and
332 (or auto-revert-mode
333 (and
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)
345 auto-revert-mode))))
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))))
359
360
361 ;; The end:
362
363 (unless (assq 'auto-revert-mode minor-mode-alist)
364 (push '(auto-revert-mode auto-revert-mode-text)
365 minor-mode-alist))
366 (unless (assq 'global-auto-revert-mode minor-mode-alist)
367 (push '(global-auto-revert-mode global-auto-revert-mode-text)
368 minor-mode-alist))
369
370 (provide 'autorevert)
371
372 (run-hooks 'auto-revert-load-hook)
373
374 ;; This makes it possible to set Global Auto-Revert Mode from
375 ;; Customize.
376 (if global-auto-revert-mode
377 (global-auto-revert-mode 1))
378
379 ;; autorevert.el ends here.