Fix last commit in autorevert.el.
[bpt/emacs.git] / lisp / autorevert.el
1 ;;; autorevert.el --- revert buffers when files on disk change
2
3 ;; Copyright (C) 1997-1999, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Keywords: convenience
7 ;; Created: 1997-06-01
8 ;; Date: 1999-11-30
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Introduction:
28 ;;
29 ;; Whenever a file that Emacs is editing has been changed by another
30 ;; program the user normally has to execute the command `revert-buffer'
31 ;; to load the new content of the file into Emacs.
32 ;;
33 ;; This package contains two minor modes: Global Auto-Revert Mode and
34 ;; Auto-Revert Mode. Both modes automatically revert buffers
35 ;; whenever the corresponding files have been changed on disk and the
36 ;; buffer contains no unsaved changes.
37 ;;
38 ;; Auto-Revert Mode can be activated for individual buffers. Global
39 ;; Auto-Revert Mode applies to all file buffers. (If the user option
40 ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies
41 ;; to some non-file buffers. This option is disabled by default.)
42 ;; Since checking a remote file is too slow, these modes do not check
43 ;; or revert remote files.
44 ;;
45 ;; Both modes operate by checking the time stamp of all files at
46 ;; intervals of `auto-revert-interval'. The default is every five
47 ;; seconds. The check is aborted whenever the user actually uses
48 ;; Emacs. You should never even notice that this package is active
49 ;; (except that your buffers will be reverted, of course).
50 ;;
51 ;; If Emacs is compiled with file watch support, notifications are
52 ;; used instead of checking the time stamp of the files. You can
53 ;; disable this by setting the user option `auto-revert-use-notify' to
54 ;; nil.
55 ;;
56 ;; After reverting a file buffer, Auto Revert Mode normally puts point
57 ;; at the same position that a regular manual revert would. However,
58 ;; there is one exception to this rule. If point is at the end of the
59 ;; buffer before reverting, it stays at the end. Similarly if point
60 ;; is displayed at the end of a file buffer in any window, it will stay
61 ;; at the end of the buffer in that window, even if the window is not
62 ;; selected. This way, you can use Auto Revert Mode to `tail' a file.
63 ;; Just put point at the end of the buffer and it will stay there.
64 ;; These rules apply to file buffers. For non-file buffers, the
65 ;; behavior may be mode dependent.
66 ;;
67 ;; While you can use Auto Revert Mode to tail a file, this package
68 ;; contains a third minor mode, Auto Revert Tail Mode, which does so
69 ;; more efficiently, as long as you are sure that the file will only
70 ;; change by growing at the end. It only appends the new output,
71 ;; instead of reverting the entire buffer. It does so even if the
72 ;; buffer contains unsaved changes. (Because they will not be lost.)
73 ;; Auto Revert Tail Mode works also for remote files.
74
75 ;; Usage:
76 ;;
77 ;; Go to the appropriate buffer and press either of:
78 ;; M-x auto-revert-mode RET
79 ;; M-x auto-revert-tail-mode RET
80 ;;
81 ;; To activate Global Auto-Revert Mode, press:
82 ;; M-x global-auto-revert-mode RET
83 ;;
84 ;; To activate Global Auto-Revert Mode every time Emacs is started
85 ;; customize the option `global-auto-revert-mode' or the following
86 ;; line could be added to your ~/.emacs:
87 ;; (global-auto-revert-mode 1)
88 ;;
89 ;; The function `turn-on-auto-revert-mode' could be added to any major
90 ;; mode hook to activate Auto-Revert Mode for all buffers in that
91 ;; mode. For example, the following line will activate Auto-Revert
92 ;; Mode in all C mode buffers:
93 ;;
94 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
95
96 ;;; Code:
97
98 ;; Dependencies:
99
100 (require 'timer)
101
102 ;; Custom Group:
103 ;;
104 ;; The two modes will be placed next to Auto Save Mode under the
105 ;; Files group under Emacs.
106
107 (defgroup auto-revert nil
108 "Revert individual buffers when files on disk change.
109 Auto-Revert mode enables auto-revert in individual buffers.
110 Global Auto-Revert mode does so in all buffers."
111 :group 'files
112 :group 'convenience)
113
114
115 ;; Variables:
116
117 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
118 ;;; What's this?: ;;;###autoload
119 (defvar auto-revert-mode nil
120 "Non-nil when Auto-Revert Mode is active.
121 Never set this variable directly, use the command `auto-revert-mode' instead.")
122 (put 'auto-revert-mode 'permanent-local t)
123
124 (defvar auto-revert-tail-mode nil
125 "Non-nil when Auto-Revert Tail Mode is active.
126 Never set this variable directly, use the command
127 `auto-revert-tail-mode' instead.")
128 (put 'auto-revert-tail-mode 'permanent-local t)
129
130 (defvar auto-revert-timer nil
131 "Timer used by Auto-Revert Mode.")
132
133 (defcustom auto-revert-interval 5
134 "Time, in seconds, between Auto-Revert Mode file checks.
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."
144 :group 'auto-revert
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))))
151
152 (defcustom auto-revert-stop-on-user-input t
153 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
154 With this setting, Auto-Revert Mode checks for user input after
155 handling each buffer and does not process any further buffers
156 \(until the next run of the timer) if user input is available.
157 When nil, Auto-Revert Mode checks files and reverts buffers,
158 with quitting disabled, without paying attention to user input.
159 Thus, with this setting, Emacs might be non-responsive at times."
160 :group 'auto-revert
161 :type 'boolean)
162
163 (defcustom auto-revert-verbose t
164 "When nil, Auto-Revert Mode does not generate any messages.
165 When non-nil, a message is generated whenever a file is reverted."
166 :group 'auto-revert
167 :type 'boolean)
168
169 (defcustom auto-revert-mode-text " ARev"
170 "String to display in the mode line when Auto-Revert Mode is active.
171
172 \(When the string is not empty, make sure that it has a leading space.)"
173 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
174 :group 'auto-revert
175 :type 'string)
176
177 (defcustom auto-revert-tail-mode-text " Tail"
178 "String to display in the mode line when Auto-Revert Tail Mode is active.
179
180 \(When the string is not empty, make sure that it has a leading space.)"
181 :group 'auto-revert
182 :type 'string
183 :version "22.1")
184
185 (defcustom auto-revert-mode-hook nil
186 "Functions to run when Auto-Revert Mode is activated."
187 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
188 :group 'auto-revert
189 :type 'hook)
190
191 (defcustom global-auto-revert-mode-text ""
192 "String to display when Global Auto-Revert Mode is active.
193
194 The default is nothing since when this mode is active this text doesn't
195 vary over time, or between buffers. Hence mode line text
196 would only waste precious space."
197 :group 'auto-revert
198 :type 'string)
199
200 (defcustom global-auto-revert-mode-hook nil
201 "Hook called when Global Auto-Revert Mode is activated."
202 :group 'auto-revert
203 :type 'hook)
204
205 (defcustom global-auto-revert-non-file-buffers nil
206 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
207
208 When non-nil, both file buffers and buffers with a custom
209 `revert-buffer-function' and a `buffer-stale-function' are
210 reverted by Global Auto-Revert mode. These include the Buffer
211 List buffer displayed by `buffer-menu', and Dired buffers showing
212 complete local directories. The Buffer List buffer reverts every
213 `auto-revert-interval' seconds; Dired buffers when the file list of
214 the main directory changes. Dired buffers do not auto-revert as
215 a result of changes in subdirectories, or in the contents, size,
216 modes, etc., of files. You may still sometimes want to revert
217 them manually.
218
219 Use this option with care since it could lead to excessive auto-reverts.
220 For more information, see Info node `(emacs)Autorevert'."
221 :group 'auto-revert
222 :type 'boolean
223 :link '(info-link "(emacs)Autorevert"))
224
225 (defcustom global-auto-revert-ignore-modes ()
226 "List of major modes Global Auto-Revert Mode should not check."
227 :group 'auto-revert
228 :type '(repeat sexp))
229
230 (defcustom auto-revert-load-hook nil
231 "Functions to run when Auto-Revert Mode is first loaded."
232 :tag "Load Hook"
233 :group 'auto-revert
234 :type 'hook)
235
236 (defcustom auto-revert-check-vc-info nil
237 "If non-nil Auto Revert Mode reliably updates version control info.
238 Auto Revert Mode updates version control info whenever the buffer
239 needs reverting, regardless of the value of this variable.
240 However, the version control state can change without changes to
241 the work file. If the change is made from the current Emacs
242 session, all info is updated. But if, for instance, a new
243 version is checked in from outside the current Emacs session, the
244 version control number in the mode line, as well as other version
245 control related information, may not be properly updated. If you
246 are worried about this, set this variable to a non-nil value.
247
248 This currently works by automatically updating the version
249 control info every `auto-revert-interval' seconds. Nevertheless,
250 it should not cause excessive CPU usage on a reasonably fast
251 machine, if it does not apply to too many version controlled
252 buffers. CPU usage depends on the version control system."
253 :group 'auto-revert
254 :type 'boolean
255 :version "22.1")
256
257 (defvar global-auto-revert-ignore-buffer nil
258 "When non-nil, Global Auto-Revert Mode will not revert this buffer.
259 This variable becomes buffer local when set in any fashion.")
260 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
261
262 (defconst auto-revert-notify-enabled
263 (or (featurep 'inotify) (featurep 'w32notify))
264 "Non-nil when Emacs has been compiled with file watch support.")
265
266 (defcustom auto-revert-use-notify auto-revert-notify-enabled
267 "If non-nil Auto Revert Mode uses file watch functions.
268 This requires Emacs being compiled with file watch support (see
269 `auto-revert-notify-enabled'). You should set this variable
270 through Custom only."
271 :group 'auto-revert
272 :type 'boolean
273 :set (lambda (variable value)
274 (set-default variable (and auto-revert-notify-enabled value))
275 (if (symbol-value variable)
276 (add-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch)
277 (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch)
278 (when auto-revert-notify-enabled
279 (dolist (buf (buffer-list))
280 (with-current-buffer buf
281 (auto-revert-notify-rm-watch))))))
282 :version "24.4")
283
284 ;; Internal variables:
285
286 (defvar auto-revert-buffer-list ()
287 "List of buffers in Auto-Revert Mode.
288
289 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
290 buffers to this list.
291
292 The timer function `auto-revert-buffers' is responsible for purging
293 the list of old buffers.")
294
295 (defvar auto-revert-remaining-buffers ()
296 "Buffers not checked when user input stopped execution.")
297
298 (defvar auto-revert-tail-pos 0
299 "Position of last known end of file.")
300
301 (add-hook 'find-file-hook
302 (lambda ()
303 (set (make-local-variable 'auto-revert-tail-pos)
304 (nth 7 (file-attributes buffer-file-name)))))
305
306 (defvar auto-revert-notify-watch-descriptor-hash-list
307 (make-hash-table :test 'equal)
308 "A hash table collecting all file watch descriptors.
309 Hash key is a watch descriptor, hash value is the corresponding buffer.")
310
311 (defvar auto-revert-notify-watch-descriptor nil
312 "The file watch descriptor active for the current buffer.")
313 (make-variable-buffer-local 'auto-revert-notify-watch-descriptor)
314
315 (defvar auto-revert-notify-modified-p nil
316 "Non-nil when file has been modified on the file system.
317 This has been reported by a file watch event.")
318 (make-variable-buffer-local 'auto-revert-notify-modified-p)
319
320 ;; Functions:
321
322 ;;;###autoload
323 (define-minor-mode auto-revert-mode
324 "Toggle reverting buffer when the file changes (Auto Revert mode).
325 With a prefix argument ARG, enable Auto Revert mode if ARG is
326 positive, and disable it otherwise. If called from Lisp, enable
327 the mode if ARG is omitted or nil.
328
329 Auto Revert mode is a minor mode that affects only the current
330 buffer. When enabled, it reverts the buffer when the file on
331 disk changes.
332
333 Use `global-auto-revert-mode' to automatically revert all buffers.
334 Use `auto-revert-tail-mode' if you know that the file will only grow
335 without being changed in the part that is already in the buffer."
336 :group 'auto-revert :lighter auto-revert-mode-text
337 (if auto-revert-mode
338 (if (not (memq (current-buffer) auto-revert-buffer-list))
339 (push (current-buffer) auto-revert-buffer-list))
340 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
341 (setq auto-revert-buffer-list
342 (delq (current-buffer) auto-revert-buffer-list)))
343 (auto-revert-set-timer)
344 (when auto-revert-mode
345 (auto-revert-buffers)
346 (setq auto-revert-tail-mode nil)))
347
348
349 ;;;###autoload
350 (defun turn-on-auto-revert-mode ()
351 "Turn on Auto-Revert Mode.
352
353 This function is designed to be added to hooks, for example:
354 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
355 (auto-revert-mode 1))
356
357
358 ;;;###autoload
359 (define-minor-mode auto-revert-tail-mode
360 "Toggle reverting tail of buffer when the file grows.
361 With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
362 is positive, and disable it otherwise. If called from Lisp,
363 enable the mode if ARG is omitted or nil.
364
365 When Auto Revert Tail mode is enabled, the tail of the file is
366 constantly followed, as with the shell command `tail -f'. This
367 means that whenever the file grows on disk (presumably because
368 some background process is appending to it from time to time),
369 this is reflected in the current buffer.
370
371 You can edit the buffer and turn this mode off and on again as
372 you please. But make sure the background process has stopped
373 writing before you save the file!
374
375 Use `auto-revert-mode' for changes other than appends!"
376 :group 'find-file :lighter auto-revert-tail-mode-text
377 (when auto-revert-tail-mode
378 (unless buffer-file-name
379 (auto-revert-tail-mode 0)
380 (error "This buffer is not visiting a file"))
381 (if (and (buffer-modified-p)
382 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
383 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
384 (auto-revert-tail-mode 0)
385 ;; a-r-tail-pos stores the size of the file at the time of the
386 ;; last revert. After this package loads, it adds a
387 ;; find-file-hook to set this variable every time a file is
388 ;; loaded. If the package is loaded only _after_ visiting the
389 ;; file to be reverted, then we have no idea what the value of
390 ;; a-r-tail-pos should have been when the file was visited. If
391 ;; the file has changed on disk in the meantime, all we can do
392 ;; is offer to revert the whole thing. If you choose not to
393 ;; revert, then you might miss some output then happened
394 ;; between visiting the file and activating a-r-t-mode.
395 (and (zerop auto-revert-tail-pos)
396 (not (verify-visited-file-modtime (current-buffer)))
397 (y-or-n-p "File changed on disk, content may be missing. \
398 Perform a full revert? ")
399 ;; Use this (not just revert-buffer) for point-preservation.
400 (auto-revert-handler))
401 ;; else we might reappend our own end when we save
402 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
403 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
404 (set (make-local-variable 'auto-revert-tail-pos)
405 (nth 7 (file-attributes buffer-file-name))))
406 ;; let auto-revert-mode set up the mechanism for us if it isn't already
407 (or auto-revert-mode
408 (let ((auto-revert-tail-mode t))
409 (auto-revert-mode 1)))
410 (setq auto-revert-mode nil))))
411
412
413 ;;;###autoload
414 (defun turn-on-auto-revert-tail-mode ()
415 "Turn on Auto-Revert Tail mode.
416
417 This function is designed to be added to hooks, for example:
418 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
419 (auto-revert-tail-mode 1))
420
421
422 ;;;###autoload
423 (define-minor-mode global-auto-revert-mode
424 "Toggle Global Auto Revert mode.
425 With a prefix argument ARG, enable Global Auto Revert mode if ARG
426 is positive, and disable it otherwise. If called from Lisp,
427 enable the mode if ARG is omitted or nil.
428
429 Global Auto Revert mode is a global minor mode that reverts any
430 buffer associated with a file when the file changes on disk. Use
431 `auto-revert-mode' to revert a particular buffer.
432
433 If `global-auto-revert-non-file-buffers' is non-nil, this mode
434 may also revert some non-file buffers, as described in the
435 documentation of that variable. It ignores buffers with modes
436 matching `global-auto-revert-ignore-modes', and buffers with a
437 non-nil vale of `global-auto-revert-ignore-buffer'.
438
439 This function calls the hook `global-auto-revert-mode-hook'.
440 It displays the text that `global-auto-revert-mode-text'
441 specifies in the mode line."
442 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
443 (auto-revert-set-timer)
444 (if global-auto-revert-mode
445 (auto-revert-buffers)
446 (when auto-revert-use-notify
447 (dolist (buf (buffer-list))
448 (with-current-buffer buf
449 (auto-revert-notify-rm-watch))))))
450
451 (defun auto-revert-set-timer ()
452 "Restart or cancel the timer used by Auto-Revert Mode.
453 If such a timer is active, cancel it. Start a new timer if
454 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
455 in some buffer. Restarting the timer ensures that Auto-Revert Mode
456 will use an up-to-date value of `auto-revert-interval'"
457 (interactive)
458 (if (timerp auto-revert-timer)
459 (cancel-timer auto-revert-timer))
460 (setq auto-revert-timer
461 (if (or global-auto-revert-mode auto-revert-buffer-list)
462 (run-with-timer auto-revert-interval
463 auto-revert-interval
464 'auto-revert-buffers))))
465
466 (defun auto-revert-notify-rm-watch ()
467 "Disable file watch for current buffer's associated file."
468 (when auto-revert-notify-watch-descriptor
469 (funcall (if (fboundp 'inotify-rm-watch)
470 'inotify-rm-watch 'w32notify-rm-watch)
471 auto-revert-notify-watch-descriptor)
472 (remhash auto-revert-notify-watch-descriptor
473 auto-revert-notify-watch-descriptor-hash-list))
474 (setq auto-revert-notify-watch-descriptor nil
475 auto-revert-notify-modified-p nil))
476
477 (defun auto-revert-notify-add-watch ()
478 "Enable file watch for current buffer's associated file."
479 (when (and buffer-file-name auto-revert-use-notify)
480 (auto-revert-notify-rm-watch)
481 (let ((func (if (fboundp 'inotify-add-watch)
482 'inotify-add-watch 'w32notify-add-watch))
483 (aspect (if (fboundp 'inotify-add-watch)
484 '(close-write) '(last-write-time))))
485 (setq auto-revert-notify-watch-descriptor
486 (funcall func buffer-file-name aspect 'auto-revert-notify-handler))
487 (puthash auto-revert-notify-watch-descriptor
488 (current-buffer)
489 auto-revert-notify-watch-descriptor-hash-list))))
490
491 (defun auto-revert-notify-handler (event)
492 "Handle an event returned from file watch."
493 (when (listp event)
494 (let ((buffer
495 (gethash (car event) auto-revert-notify-watch-descriptor-hash-list)))
496 (when (bufferp buffer)
497 (with-current-buffer buffer
498 (setq auto-revert-notify-modified-p t))))))
499
500 (defun auto-revert-active-p ()
501 "Check if auto-revert is active (in current buffer or globally)."
502 (or auto-revert-mode
503 auto-revert-tail-mode
504 (and
505 global-auto-revert-mode
506 (not global-auto-revert-ignore-buffer)
507 (not (memq major-mode
508 global-auto-revert-ignore-modes)))))
509
510 (defun auto-revert-handler ()
511 "Revert current buffer, if appropriate.
512 This is an internal function used by Auto-Revert Mode."
513 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
514 (let* ((buffer (current-buffer)) size
515 (revert
516 (or (and buffer-file-name
517 (if auto-revert-tail-mode
518 ;; Tramp caches the file attributes. Setting
519 ;; `remote-file-name-inhibit-cache' forces Tramp
520 ;; to reread the values.
521 (let ((remote-file-name-inhibit-cache t))
522 (and (file-readable-p buffer-file-name)
523 (/= auto-revert-tail-pos
524 (setq size
525 (nth 7 (file-attributes
526 buffer-file-name))))))
527 (if auto-revert-use-notify
528 ;; There are file watches.
529 auto-revert-notify-modified-p
530 (and (not (file-remote-p buffer-file-name))
531 (file-readable-p buffer-file-name)
532 (not (verify-visited-file-modtime buffer))))))
533 (and (or auto-revert-mode
534 global-auto-revert-non-file-buffers)
535 revert-buffer-function
536 (boundp 'buffer-stale-function)
537 (functionp buffer-stale-function)
538 (funcall buffer-stale-function t))))
539 eob eoblist)
540 (when revert
541 (setq auto-revert-notify-modified-p nil)
542 (when (and auto-revert-verbose
543 (not (eq revert 'fast)))
544 (message "Reverting buffer `%s'." (buffer-name)))
545 ;; If point (or a window point) is at the end of the buffer,
546 ;; we want to keep it at the end after reverting. This allows
547 ;; to tail a file.
548 (when buffer-file-name
549 (setq eob (eobp))
550 (walk-windows
551 (lambda (window)
552 (and (eq (window-buffer window) buffer)
553 (= (window-point window) (point-max))
554 (push window eoblist)))
555 'no-mini t))
556 (if auto-revert-tail-mode
557 (auto-revert-tail-handler size)
558 ;; Bind buffer-read-only in case user has done C-x C-q,
559 ;; so as not to forget that. This gives undesirable results
560 ;; when the file's mode changes, but that is less common.
561 (let ((buffer-read-only buffer-read-only))
562 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
563 (when buffer-file-name
564 (when eob (goto-char (point-max)))
565 (dolist (window eoblist)
566 (set-window-point window (point-max)))))
567 ;; `preserve-modes' avoids changing the (minor) modes. But we
568 ;; do want to reset the mode for VC, so we do it manually.
569 (when (or revert auto-revert-check-vc-info)
570 (vc-find-file-hook)))))
571
572 (defun auto-revert-tail-handler (size)
573 (let ((modified (buffer-modified-p))
574 (inhibit-read-only t) ; Ignore.
575 (file buffer-file-name)
576 (buffer-file-name nil)) ; Ignore that file has changed.
577 (when (/= auto-revert-tail-pos size)
578 (run-hooks 'before-revert-hook)
579 (undo-boundary)
580 (save-restriction
581 (widen)
582 (save-excursion
583 (goto-char (point-max))
584 (insert-file-contents file nil
585 (and (< auto-revert-tail-pos size)
586 auto-revert-tail-pos)
587 size)))
588 (run-hooks 'after-revert-hook)
589 (undo-boundary)
590 (setq auto-revert-tail-pos size)
591 (restore-buffer-modified-p modified)))
592 (set-visited-file-modtime))
593
594 (defun auto-revert-buffers ()
595 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
596
597 Should `global-auto-revert-mode' be active all file buffers are checked.
598
599 Should `auto-revert-mode' be active in some buffers, those buffers
600 are checked.
601
602 Non-file buffers that have a custom `revert-buffer-function' and
603 a `buffer-stale-function' are reverted either when Auto-Revert
604 Mode is active in that buffer, or when the variable
605 `global-auto-revert-non-file-buffers' is non-nil and Global
606 Auto-Revert Mode is active.
607
608 This function stops whenever there is user input. The buffers not
609 checked are stored in the variable `auto-revert-remaining-buffers'.
610
611 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
612 are checked first the next time this function is called.
613
614 This function is also responsible for removing buffers no longer in
615 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
616 the timer when no buffers need to be checked."
617 (save-match-data
618 (let ((bufs (if global-auto-revert-mode
619 (buffer-list)
620 auto-revert-buffer-list))
621 (remaining ())
622 (new ()))
623 ;; Partition `bufs' into two halves depending on whether or not
624 ;; the buffers are in `auto-revert-remaining-buffers'. The two
625 ;; halves are then re-joined with the "remaining" buffers at the
626 ;; head of the list.
627 (dolist (buf auto-revert-remaining-buffers)
628 (if (memq buf bufs)
629 (push buf remaining)))
630 (dolist (buf bufs)
631 (if (not (memq buf remaining))
632 (push buf new)))
633 (setq bufs (nreverse (nconc new remaining)))
634 (while (and bufs
635 (not (and auto-revert-stop-on-user-input
636 (input-pending-p))))
637 (let ((buf (car bufs)))
638 (if (buffer-live-p buf)
639 (with-current-buffer buf
640 ;; Test if someone has turned off Auto-Revert Mode in a
641 ;; non-standard way, for example by changing major mode.
642 (if (and (not auto-revert-mode)
643 (not auto-revert-tail-mode)
644 (memq buf auto-revert-buffer-list))
645 (setq auto-revert-buffer-list
646 (delq buf auto-revert-buffer-list)))
647 (when (auto-revert-active-p)
648 ;; Enable file watches.
649 (when (and auto-revert-use-notify buffer-file-name
650 (not auto-revert-notify-watch-descriptor)
651 (auto-revert-notify-add-watch)))
652 (auto-revert-handler)))
653 ;; Remove dead buffer from `auto-revert-buffer-list'.
654 (setq auto-revert-buffer-list
655 (delq buf auto-revert-buffer-list))))
656 (setq bufs (cdr bufs)))
657 (setq auto-revert-remaining-buffers bufs)
658 ;; Check if we should cancel the timer.
659 (when (and (not global-auto-revert-mode)
660 (null auto-revert-buffer-list))
661 (cancel-timer auto-revert-timer)
662 (setq auto-revert-timer nil)))))
663
664
665 ;; The end:
666 (provide 'autorevert)
667
668 (run-hooks 'auto-revert-load-hook)
669
670 ;;; autorevert.el ends here