Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; autorevert.el --- revert buffers when files on disk change |
4a35aff3 | 2 | |
38f442c6 | 3 | ;; Copyright (C) 1997, 1998, 1999, 2001, 2004 Free Software Foundation, Inc. |
4a35aff3 | 4 | |
a468671a | 5 | ;; Author: Anders Lindgren <andersl@andersl.com> |
f5f727f8 | 6 | ;; Keywords: convenience |
a468671a GM |
7 | ;; Created: 1997-06-01 |
8 | ;; Date: 1999-11-30 | |
4a35aff3 RS |
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 | ;; Introduction: | |
30 | ;; | |
31 | ;; Whenever a file that Emacs is editing has been changed by another | |
48764ae2 | 32 | ;; program the user normally has to execute the command `revert-buffer' |
4a35aff3 RS |
33 | ;; to load the new content of the file into Emacs. |
34 | ;; | |
35 | ;; This package contains two minor modes: Global Auto-Revert Mode and | |
48764ae2 | 36 | ;; Auto-Revert Mode. Both modes automatically revert buffers |
2d677766 LT |
37 | ;; whenever the corresponding files have been changed on disk and the |
38 | ;; buffer contains no unsaved changes. | |
4a35aff3 | 39 | ;; |
ae229809 LT |
40 | ;; Auto-Revert Mode can be activated for individual buffers. Global |
41 | ;; Auto-Revert Mode applies to all file buffers. (If the user option | |
42 | ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies | |
43 | ;; to some non-file buffers. This option is disabled by default.) | |
44 | ;; Since checking a remote file is too slow, these modes do not check | |
45 | ;; or revert remote files. | |
4a35aff3 | 46 | ;; |
48764ae2 DL |
47 | ;; Both modes operate by checking the time stamp of all files at |
48 | ;; intervals of `auto-revert-interval'. The default is every five | |
49 | ;; seconds. The check is aborted whenever the user actually uses | |
50 | ;; Emacs. You should never even notice that this package is active | |
51 | ;; (except that your buffers will be reverted, of course). | |
1f41bcba LT |
52 | ;; |
53 | ;; After reverting a file buffer, Auto Revert Mode normally puts point | |
54 | ;; at the same position that a regular manual revert would. However, | |
55 | ;; there is one exception to this rule. If point is at the end of the | |
56 | ;; buffer before reverting, it stays at the end. Similarly if point | |
57 | ;; is displayed at the end of a file buffer in any window, it will stay | |
58 | ;; at the end of the buffer in that window, even if the window is not | |
59 | ;; selected. This way, you can use Auto Revert Mode to `tail' a file. | |
60 | ;; Just put point at the end of the buffer and it will stay there. | |
61 | ;; These rules apply to file buffers. For non-file buffers, the | |
62 | ;; behavior may be mode dependent. | |
2d677766 LT |
63 | ;; |
64 | ;; While you can use Auto Revert Mode to tail a file, this package | |
65 | ;; contains a third minor mode, Auto Revert Tail Mode, which does so | |
66 | ;; more efficiently, as long as you are sure that the file will only | |
67 | ;; change by growing at the end. It only appends the new output, | |
68 | ;; instead of reverting the entire buffer. It does so even if the | |
69 | ;; buffer contains unsaved changes. (Because they will not be lost.) | |
4a35aff3 RS |
70 | |
71 | ;; Usage: | |
72 | ;; | |
dddc748b | 73 | ;; Go to the appropriate buffer and press either of: |
4a35aff3 | 74 | ;; M-x auto-revert-mode RET |
dddc748b | 75 | ;; M-x auto-revert-tail-mode RET |
4a35aff3 RS |
76 | ;; |
77 | ;; To activate Global Auto-Revert Mode, press: | |
78 | ;; M-x global-auto-revert-mode RET | |
79 | ;; | |
48764ae2 DL |
80 | ;; To activate Global Auto-Revert Mode every time Emacs is started |
81 | ;; customise the option `global-auto-revert-mode' or the following | |
82 | ;; line could be added to your ~/.emacs: | |
4a35aff3 RS |
83 | ;; (global-auto-revert-mode 1) |
84 | ;; | |
85 | ;; The function `turn-on-auto-revert-mode' could be added to any major | |
86 | ;; mode hook to activate Auto-Revert Mode for all buffers in that | |
87 | ;; mode. For example, the following line will activate Auto-Revert | |
88 | ;; Mode in all C mode buffers: | |
89 | ;; | |
90 | ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode) | |
91 | ||
92 | ;;; Code: | |
93 | ||
94 | ;; Dependencies: | |
95 | ||
96 | (require 'timer) | |
0f98bc23 | 97 | |
71c8db4c | 98 | (eval-when-compile (require 'cl)) |
4a35aff3 RS |
99 | |
100 | ||
101 | ;; Custom Group: | |
102 | ;; | |
103 | ;; The two modes will be placed next to Auto Save Mode under the | |
104 | ;; Files group under Emacs. | |
105 | ||
106 | (defgroup auto-revert nil | |
48764ae2 | 107 | "Revert individual buffers when files on disk change. |
4a35aff3 RS |
108 | |
109 | Auto-Revert Mode can be activated for individual buffer. | |
110 | Global Auto-Revert Mode applies to all buffers." | |
f5f727f8 DN |
111 | :group 'files |
112 | :group 'convenience) | |
4a35aff3 RS |
113 | |
114 | ||
115 | ;; Variables: | |
116 | ||
dddc748b DP |
117 | ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'. |
118 | ;;; What's this?: ;;;###autoload | |
4a35aff3 RS |
119 | (defvar auto-revert-mode nil |
120 | "*Non-nil when Auto-Revert Mode is active. | |
0e4f9468 SM |
121 | Never set this variable directly, use the command `auto-revert-mode' instead.") |
122 | (put 'auto-revert-mode 'permanent-local t) | |
4a35aff3 | 123 | |
dddc748b DP |
124 | (defvar auto-revert-tail-mode nil |
125 | "*Non-nil when Auto-Revert Tail Mode is active. | |
9e559f9b LT |
126 | Never set this variable directly, use the command |
127 | `auto-revert-tail-mode' instead.") | |
dddc748b DP |
128 | (put 'auto-revert-tail-mode 'permanent-local t) |
129 | ||
a2ac68f1 LT |
130 | (defvar auto-revert-timer nil |
131 | "Timer used by Auto-Revert Mode.") | |
132 | ||
4a35aff3 | 133 | (defcustom auto-revert-interval 5 |
ad660075 | 134 | "Time, in seconds, between Auto-Revert Mode file checks. |
a2ac68f1 LT |
135 | The value may be an integer or floating point number. |
136 | ||
137 | If a timer is already active, there are two ways to make sure | |
138 | that the new value will take effect immediately. You can set | |
139 | this variable through Custom or you can call the command | |
140 | `auto-revert-set-timer' after setting the variable. Otherwise, | |
141 | the new value will take effect the first time Auto Revert Mode | |
142 | calls `auto-revert-set-timer' for internal reasons or in your | |
143 | next editing session." | |
4a35aff3 | 144 | :group 'auto-revert |
a2ac68f1 LT |
145 | :type 'number |
146 | :set (lambda (variable value) | |
147 | (set-default variable value) | |
148 | (and (boundp 'auto-revert-timer) | |
149 | auto-revert-timer | |
150 | (auto-revert-set-timer)))) | |
4a35aff3 RS |
151 | |
152 | (defcustom auto-revert-stop-on-user-input t | |
153 | "When non-nil Auto-Revert Mode stops checking files on user input." | |
154 | :group 'auto-revert | |
155 | :type 'boolean) | |
156 | ||
157 | (defcustom auto-revert-verbose t | |
158 | "When nil, Auto-Revert Mode will not generate any messages. | |
0e5dcfd7 | 159 | When non-nil, a message is generated whenever a file is reverted." |
4a35aff3 RS |
160 | :group 'auto-revert |
161 | :type 'boolean) | |
162 | ||
163 | (defcustom auto-revert-mode-text " ARev" | |
164 | "String to display in the mode line when Auto-Revert Mode is active. | |
165 | ||
166 | \(When the string is not empty, make sure that it has a leading space.)" | |
167 | :tag "Auto Revert Mode Text" ; To separate it from `global-...' | |
168 | :group 'auto-revert | |
169 | :type 'string) | |
170 | ||
dddc748b DP |
171 | (defcustom auto-revert-tail-mode-text " Tail" |
172 | "String to display in the mode line when Auto-Revert Tail Mode is active. | |
173 | ||
174 | \(When the string is not empty, make sure that it has a leading space.)" | |
175 | :group 'auto-revert | |
0a306700 | 176 | :type 'string |
bf247b6e | 177 | :version "22.1") |
dddc748b | 178 | |
4a35aff3 RS |
179 | (defcustom auto-revert-mode-hook nil |
180 | "Functions to run when Auto-Revert Mode is activated." | |
181 | :tag "Auto Revert Mode Hook" ; To separate it from `global-...' | |
182 | :group 'auto-revert | |
183 | :type 'hook) | |
184 | ||
185 | (defcustom global-auto-revert-mode-text "" | |
186 | "String to display when Global Auto-Revert Mode is active. | |
187 | ||
188 | The default is nothing since when this mode is active this text doesn't | |
48764ae2 | 189 | vary over time, or between buffers. Hence mode line text |
4a35aff3 RS |
190 | would only waste precious space." |
191 | :group 'auto-revert | |
192 | :type 'string) | |
193 | ||
194 | (defcustom global-auto-revert-mode-hook nil | |
195 | "Hook called when Global Auto-Revert Mode is activated." | |
196 | :group 'auto-revert | |
197 | :type 'hook) | |
198 | ||
199 | (defcustom global-auto-revert-non-file-buffers nil | |
d9e4328d | 200 | "When nil, Global Auto-Revert mode operates only on file-visiting buffers. |
4a35aff3 RS |
201 | |
202 | When non-nil, both file buffers and buffers with a custom | |
0e5dcfd7 | 203 | `revert-buffer-function' and a `buffer-stale-function' are |
9ae0d84f LT |
204 | reverted by Global Auto-Revert mode. These include the Buffer |
205 | List buffer, and Dired buffers showing complete local | |
206 | directories. Dired buffers do not auto-revert as a result of | |
207 | changes in subdirectories or in the contents, size, modes, etc., | |
208 | of files. You may still sometimes want to revert them manually. | |
0e4f9468 | 209 | |
d9e4328d | 210 | Use this option with care since it could lead to excessive auto-reverts. |
9ae0d84f | 211 | For more information, see Info node `(emacs-xtra)Autorevert'." |
4a35aff3 | 212 | :group 'auto-revert |
843c51ae LT |
213 | :type 'boolean |
214 | :link '(info-link "(emacs-xtra)Autorevert")) | |
4a35aff3 | 215 | |
dddc748b | 216 | (defcustom global-auto-revert-ignore-modes () |
4a35aff3 RS |
217 | "List of major modes Global Auto-Revert Mode should not check." |
218 | :group 'auto-revert | |
219 | :type '(repeat sexp)) | |
220 | ||
221 | (defcustom auto-revert-load-hook nil | |
222 | "Functions to run when Auto-Revert Mode is first loaded." | |
223 | :tag "Load Hook" | |
224 | :group 'auto-revert | |
225 | :type 'hook) | |
226 | ||
71c8db4c LT |
227 | (defcustom auto-revert-check-vc-info nil |
228 | "If non-nil Auto Revert Mode reliably updates version control info. | |
229 | Auto Revert Mode updates version control info whenever the buffer | |
230 | needs reverting, regardless of the value of this variable. | |
231 | However, the version control state can change without changes to | |
232 | the work file. If the change is made from the current Emacs | |
233 | session, all info is updated. But if, for instance, a new | |
234 | version is checked in from outside the current Emacs session, the | |
235 | version control number in the mode line, as well as other version | |
236 | control related information, may not be properly updated. If you | |
237 | are worried about this, set this variable to a non-nil value. | |
238 | ||
239 | This currently works by automatically updating the version | |
240 | control info every `auto-revert-interval' seconds. Nevertheless, | |
241 | it should not cause excessive CPU usage on a reasonably fast | |
242 | machine, if it does not apply to too many version controlled | |
243 | buffers. CPU usage depends on the version control system" | |
244 | :group 'auto-revert | |
245 | :type 'boolean | |
bf247b6e | 246 | :version "22.1") |
71c8db4c | 247 | |
4a35aff3 | 248 | (defvar global-auto-revert-ignore-buffer nil |
f1e3ff80 | 249 | "*When non-nil, Global Auto-Revert Mode will not revert this buffer. |
4a35aff3 | 250 | |
48764ae2 | 251 | This variable becomes buffer local when set in any fashion.") |
4a35aff3 RS |
252 | (make-variable-buffer-local 'global-auto-revert-ignore-buffer) |
253 | ||
4a35aff3 RS |
254 | ;; Internal variables: |
255 | ||
dddc748b | 256 | (defvar auto-revert-buffer-list () |
4a35aff3 RS |
257 | "List of buffers in Auto-Revert Mode. |
258 | ||
259 | Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds | |
260 | buffers to this list. | |
261 | ||
262 | The timer function `auto-revert-buffers' is responsible for purging | |
263 | the list of old buffers.") | |
264 | ||
dddc748b | 265 | (defvar auto-revert-remaining-buffers () |
4a35aff3 RS |
266 | "Buffers not checked when user input stopped execution.") |
267 | ||
dddc748b DP |
268 | (defvar auto-revert-tail-pos 0 |
269 | "Position of last known end of file.") | |
270 | ||
271 | (add-hook 'find-file-hook | |
272 | (lambda () | |
273 | (set (make-local-variable 'auto-revert-tail-pos) | |
274 | (save-restriction (widen) (1- (point-max)))))) | |
4a35aff3 RS |
275 | |
276 | ;; Functions: | |
277 | ||
278 | ;;;###autoload | |
0e4f9468 | 279 | (define-minor-mode auto-revert-mode |
48764ae2 | 280 | "Toggle reverting buffer when file on disk changes. |
4a35aff3 | 281 | |
48764ae2 DL |
282 | With arg, turn Auto Revert mode on if and only if arg is positive. |
283 | This is a minor mode that affects only the current buffer. | |
dddc748b DP |
284 | Use `global-auto-revert-mode' to automatically revert all buffers. |
285 | Use `auto-revert-tail-mode' if you know that the file will only grow | |
286 | without being changed in the part that is already in the buffer." | |
0e4f9468 | 287 | nil auto-revert-mode-text nil |
4a35aff3 RS |
288 | (if auto-revert-mode |
289 | (if (not (memq (current-buffer) auto-revert-buffer-list)) | |
290 | (push (current-buffer) auto-revert-buffer-list)) | |
291 | (setq auto-revert-buffer-list | |
292 | (delq (current-buffer) auto-revert-buffer-list))) | |
293 | (auto-revert-set-timer) | |
294 | (when auto-revert-mode | |
dddc748b DP |
295 | (auto-revert-buffers) |
296 | (setq auto-revert-tail-mode nil))) | |
4a35aff3 RS |
297 | |
298 | ||
299 | ;;;###autoload | |
300 | (defun turn-on-auto-revert-mode () | |
301 | "Turn on Auto-Revert Mode. | |
302 | ||
303 | This function is designed to be added to hooks, for example: | |
304 | (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)" | |
305 | (auto-revert-mode 1)) | |
306 | ||
307 | ||
dddc748b DP |
308 | ;;;###autoload |
309 | (define-minor-mode auto-revert-tail-mode | |
310 | "Toggle reverting tail of buffer when file on disk grows. | |
311 | With arg, turn Tail mode on iff arg is positive. | |
312 | ||
313 | When Tail mode is enabled, the tail of the file is constantly | |
314 | followed, as with the shell command `tail -f'. This means that | |
315 | whenever the file grows on disk (presumably because some | |
316 | background process is appending to it from time to time), this is | |
317 | reflected in the current buffer. | |
318 | ||
319 | You can edit the buffer and turn this mode off and on again as | |
320 | you please. But make sure the background process has stopped | |
321 | writing before you save the file! | |
322 | ||
323 | Use `auto-revert-mode' for changes other than appends!" | |
324 | :group 'find-file :lighter auto-revert-tail-mode-text | |
325 | (when auto-revert-tail-mode | |
326 | (unless buffer-file-name | |
327 | (auto-revert-tail-mode 0) | |
328 | (error "This buffer is not visiting a file")) | |
329 | (if (and (buffer-modified-p) | |
330 | (not auto-revert-tail-pos) ; library was loaded only after finding file | |
331 | (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? "))) | |
332 | (auto-revert-tail-mode 0) | |
333 | ;; else we might reappend our own end when we save | |
334 | (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t) | |
335 | (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position | |
336 | (set (make-variable-buffer-local 'auto-revert-tail-pos) | |
337 | (save-restriction (widen) (1- (point-max))))) | |
338 | ;; let auto-revert-mode set up the mechanism for us if it isn't already | |
339 | (or auto-revert-mode | |
340 | (let ((auto-revert-tail-mode t)) | |
341 | (auto-revert-mode 1))) | |
342 | (setq auto-revert-mode nil)))) | |
343 | ||
344 | ||
345 | ;;;###autoload | |
346 | (defun turn-on-auto-revert-tail-mode () | |
347 | "Turn on Auto-Revert Tail Mode. | |
348 | ||
349 | This function is designed to be added to hooks, for example: | |
350 | (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)" | |
351 | (auto-revert-tail-mode 1)) | |
352 | ||
353 | ||
4a35aff3 | 354 | ;;;###autoload |
0e4f9468 | 355 | (define-minor-mode global-auto-revert-mode |
0e5dcfd7 | 356 | "Revert any buffer when file on disk changes. |
4a35aff3 | 357 | |
48764ae2 DL |
358 | With arg, turn Auto Revert mode on globally if and only if arg is positive. |
359 | This is a minor mode that affects all buffers. | |
4a35aff3 | 360 | Use `auto-revert-mode' to revert a particular buffer." |
0e4f9468 | 361 | :global t :group 'auto-revert :lighter global-auto-revert-mode-text |
4a35aff3 RS |
362 | (auto-revert-set-timer) |
363 | (when global-auto-revert-mode | |
0e4f9468 | 364 | (auto-revert-buffers))) |
4a35aff3 RS |
365 | |
366 | ||
367 | (defun auto-revert-set-timer () | |
0e5dcfd7 | 368 | "Restart or cancel the timer used by Auto-Revert Mode. |
d97c8375 | 369 | If such a timer is active, cancel it. Start a new timer if |
0e5dcfd7 LT |
370 | Global Auto-Revert Mode is active or if Auto-Revert Mode is active |
371 | in some buffer. Restarting the timer ensures that Auto-Revert Mode | |
372 | will use an up-to-date value of `auto-revert-interval'" | |
a2ac68f1 | 373 | (interactive) |
4a35aff3 RS |
374 | (if (timerp auto-revert-timer) |
375 | (cancel-timer auto-revert-timer)) | |
0e4f9468 SM |
376 | (setq auto-revert-timer |
377 | (if (or global-auto-revert-mode auto-revert-buffer-list) | |
378 | (run-with-timer auto-revert-interval | |
379 | auto-revert-interval | |
dddc748b | 380 | 'auto-revert-buffers)))) |
4a35aff3 | 381 | |
ed35db71 EZ |
382 | (defun auto-revert-active-p () |
383 | "Check if auto-revert is active (in current buffer or globally)." | |
384 | (or auto-revert-mode | |
dddc748b | 385 | auto-revert-tail-mode |
ed35db71 EZ |
386 | (and |
387 | global-auto-revert-mode | |
388 | (not global-auto-revert-ignore-buffer) | |
389 | (not (memq major-mode | |
390 | global-auto-revert-ignore-modes))))) | |
391 | ||
ed35db71 | 392 | (defun auto-revert-handler () |
0e5dcfd7 LT |
393 | "Revert current buffer, if appropriate. |
394 | This is an internal function used by Auto-Revert Mode." | |
dddc748b DP |
395 | (when (or auto-revert-tail-mode (not (buffer-modified-p))) |
396 | (let* ((buffer (current-buffer)) | |
397 | (revert | |
398 | (or (and buffer-file-name | |
399 | (not (file-remote-p buffer-file-name)) | |
400 | (file-readable-p buffer-file-name) | |
401 | (not (verify-visited-file-modtime buffer))) | |
2d677766 | 402 | (and (or auto-revert-mode |
dddc748b DP |
403 | global-auto-revert-non-file-buffers) |
404 | revert-buffer-function | |
405 | (boundp 'buffer-stale-function) | |
406 | (functionp buffer-stale-function) | |
407 | (funcall buffer-stale-function t)))) | |
408 | eob eoblist) | |
d4411cef | 409 | (when revert |
71c8db4c LT |
410 | (when (and auto-revert-verbose |
411 | (not (eq revert 'fast))) | |
633e0363 | 412 | (message "Reverting buffer `%s'." (buffer-name))) |
1f41bcba LT |
413 | ;; If point (or a window point) is at the end of the buffer, |
414 | ;; we want to keep it at the end after reverting. This allows | |
415 | ;; to tail a file. | |
416 | (when buffer-file-name | |
417 | (setq eob (eobp)) | |
418 | (walk-windows | |
419 | #'(lambda (window) | |
420 | (and (eq (window-buffer window) buffer) | |
421 | (= (window-point window) (point-max)) | |
422 | (push window eoblist))) | |
423 | 'no-mini t)) | |
dddc748b DP |
424 | (if auto-revert-tail-mode |
425 | (auto-revert-tail-handler) | |
7ebc19f9 RS |
426 | ;; Bind buffer-read-only in case user has done C-x C-q, |
427 | ;; so as not to forget that. This gives undesirable results | |
428 | ;; when the file's mode changes, but that is less common. | |
90e118ab LT |
429 | (let ((buffer-read-only buffer-read-only)) |
430 | (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes))) | |
1f41bcba LT |
431 | (when buffer-file-name |
432 | (when eob (goto-char (point-max))) | |
433 | (dolist (window eoblist) | |
434 | (set-window-point window (point-max))))) | |
71c8db4c LT |
435 | ;; `preserve-modes' avoids changing the (minor) modes. But we |
436 | ;; do want to reset the mode for VC, so we do it manually. | |
437 | (when (or revert auto-revert-check-vc-info) | |
438 | (vc-find-file-hook))))) | |
ed35db71 | 439 | |
dddc748b DP |
440 | (defun auto-revert-tail-handler () |
441 | (let ((size (nth 7 (file-attributes buffer-file-name))) | |
442 | (modified (buffer-modified-p)) | |
443 | buffer-read-only ; ignore | |
444 | (file buffer-file-name) | |
445 | buffer-file-name) ; ignore that file has changed | |
446 | (when (> size auto-revert-tail-pos) | |
d918508e | 447 | (undo-boundary) |
dddc748b DP |
448 | (save-restriction |
449 | (widen) | |
450 | (save-excursion | |
451 | (goto-char (point-max)) | |
452 | (insert-file-contents file nil auto-revert-tail-pos size))) | |
d918508e | 453 | (undo-boundary) |
dddc748b DP |
454 | (setq auto-revert-tail-pos size) |
455 | (set-buffer-modified-p modified))) | |
456 | (set-visited-file-modtime)) | |
457 | ||
4a35aff3 RS |
458 | (defun auto-revert-buffers () |
459 | "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode. | |
460 | ||
461 | Should `global-auto-revert-mode' be active all file buffers are checked. | |
462 | ||
463 | Should `auto-revert-mode' be active in some buffers, those buffers | |
464 | are checked. | |
465 | ||
0e5dcfd7 LT |
466 | Non-file buffers that have a custom `revert-buffer-function' and |
467 | a `buffer-stale-function' are reverted either when Auto-Revert | |
468 | Mode is active in that buffer, or when the variable | |
469 | `global-auto-revert-non-file-buffers' is non-nil and Global | |
470 | Auto-Revert Mode is active. | |
4a35aff3 | 471 | |
48764ae2 | 472 | This function stops whenever there is user input. The buffers not |
4a35aff3 RS |
473 | checked are stored in the variable `auto-revert-remaining-buffers'. |
474 | ||
475 | To avoid starvation, the buffers in `auto-revert-remaining-buffers' | |
476 | are checked first the next time this function is called. | |
477 | ||
48764ae2 | 478 | This function is also responsible for removing buffers no longer in |
4a35aff3 RS |
479 | Auto-Revert mode from `auto-revert-buffer-list', and for canceling |
480 | the timer when no buffers need to be checked." | |
481 | (let ((bufs (if global-auto-revert-mode | |
482 | (buffer-list) | |
483 | auto-revert-buffer-list)) | |
dddc748b DP |
484 | (remaining ()) |
485 | (new ())) | |
4a35aff3 RS |
486 | ;; Partition `bufs' into two halves depending on whether or not |
487 | ;; the buffers are in `auto-revert-remaining-buffers'. The two | |
488 | ;; halves are then re-joined with the "remaining" buffers at the | |
489 | ;; head of the list. | |
490 | (dolist (buf auto-revert-remaining-buffers) | |
491 | (if (memq buf bufs) | |
492 | (push buf remaining))) | |
493 | (dolist (buf bufs) | |
494 | (if (not (memq buf remaining)) | |
495 | (push buf new))) | |
496 | (setq bufs (nreverse (nconc new remaining))) | |
497 | (while (and bufs | |
498 | (not (and auto-revert-stop-on-user-input | |
499 | (input-pending-p)))) | |
500 | (let ((buf (car bufs))) | |
501 | (if (buffer-name buf) ; Buffer still alive? | |
0e4f9468 | 502 | (with-current-buffer buf |
4a35aff3 RS |
503 | ;; Test if someone has turned off Auto-Revert Mode in a |
504 | ;; non-standard way, for example by changing major mode. | |
505 | (if (and (not auto-revert-mode) | |
dddc748b | 506 | (not auto-revert-tail-mode) |
4a35aff3 RS |
507 | (memq buf auto-revert-buffer-list)) |
508 | (setq auto-revert-buffer-list | |
509 | (delq buf auto-revert-buffer-list))) | |
c86afc19 | 510 | (when (auto-revert-active-p) (auto-revert-handler))) |
4a35aff3 RS |
511 | ;; Remove dead buffer from `auto-revert-buffer-list'. |
512 | (setq auto-revert-buffer-list | |
513 | (delq buf auto-revert-buffer-list)))) | |
514 | (setq bufs (cdr bufs))) | |
515 | (setq auto-revert-remaining-buffers bufs) | |
516 | ;; Check if we should cancel the timer. | |
517 | (when (and (not global-auto-revert-mode) | |
518 | (null auto-revert-buffer-list)) | |
519 | (cancel-timer auto-revert-timer) | |
520 | (setq auto-revert-timer nil)))) | |
521 | ||
522 | ||
523 | ;; The end: | |
4a35aff3 RS |
524 | (provide 'autorevert) |
525 | ||
526 | (run-hooks 'auto-revert-load-hook) | |
527 | ||
ab5796a9 | 528 | ;;; arch-tag: f6bcb07b-4841-477e-9e44-b18678e58876 |
e8af40ee | 529 | ;;; autorevert.el ends here |