Merge from emacs-24; up to 2012-12-10T20:27:33Z!eggert@cs.ucla.edu
[emacs.git] / lisp / autorevert.el
blobc9180482cd92faf98dfea7b38726f366f4a5d431
1 ;;; autorevert.el --- revert buffers when files on disk change
3 ;; Copyright (C) 1997-1999, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Keywords: convenience
7 ;; Created: 1997-06-01
8 ;; Date: 1999-11-30
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 ;; Introduction:
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.
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.
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.
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).
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.
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.
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.
75 ;; Usage:
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
81 ;; To activate Global Auto-Revert Mode, press:
82 ;; M-x global-auto-revert-mode RET
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)
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:
94 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
96 ;;; Code:
98 ;; Dependencies:
100 (eval-when-compile (require 'cl-lib))
101 (require 'timer)
103 ;; Custom Group:
105 ;; The two modes will be placed next to Auto Save Mode under the
106 ;; Files group under Emacs.
108 (defgroup auto-revert nil
109 "Revert individual buffers when files on disk change.
110 Auto-Revert mode enables auto-revert in individual buffers.
111 Global Auto-Revert mode does so in all buffers."
112 :group 'files
113 :group 'convenience)
116 ;; Variables:
118 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
119 ;;; What's this?: ;;;###autoload
120 (defvar auto-revert-mode nil
121 "Non-nil when Auto-Revert Mode is active.
122 Never set this variable directly, use the command `auto-revert-mode' instead.")
123 (put 'auto-revert-mode 'permanent-local t)
125 (defvar auto-revert-tail-mode nil
126 "Non-nil when Auto-Revert Tail Mode is active.
127 Never set this variable directly, use the command
128 `auto-revert-tail-mode' instead.")
129 (put 'auto-revert-tail-mode 'permanent-local t)
131 (defvar auto-revert-timer nil
132 "Timer used by Auto-Revert Mode.")
134 (defcustom auto-revert-interval 5
135 "Time, in seconds, between Auto-Revert Mode file checks.
136 The value may be an integer or floating point number.
138 If a timer is already active, there are two ways to make sure
139 that the new value will take effect immediately. You can set
140 this variable through Custom or you can call the command
141 `auto-revert-set-timer' after setting the variable. Otherwise,
142 the new value will take effect the first time Auto Revert Mode
143 calls `auto-revert-set-timer' for internal reasons or in your
144 next editing session."
145 :group 'auto-revert
146 :type 'number
147 :set (lambda (variable value)
148 (set-default variable value)
149 (and (boundp 'auto-revert-timer)
150 auto-revert-timer
151 (auto-revert-set-timer))))
153 (defcustom auto-revert-stop-on-user-input t
154 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
155 With this setting, Auto-Revert Mode checks for user input after
156 handling each buffer and does not process any further buffers
157 \(until the next run of the timer) if user input is available.
158 When nil, Auto-Revert Mode checks files and reverts buffers,
159 with quitting disabled, without paying attention to user input.
160 Thus, with this setting, Emacs might be non-responsive at times."
161 :group 'auto-revert
162 :type 'boolean)
164 (defcustom auto-revert-verbose t
165 "When nil, Auto-Revert Mode does not generate any messages.
166 When non-nil, a message is generated whenever a file is reverted."
167 :group 'auto-revert
168 :type 'boolean)
170 (defcustom auto-revert-mode-text " ARev"
171 "String to display in the mode line when Auto-Revert Mode is active.
173 \(When the string is not empty, make sure that it has a leading space.)"
174 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
175 :group 'auto-revert
176 :type 'string)
178 (defcustom auto-revert-tail-mode-text " Tail"
179 "String to display in the mode line when Auto-Revert Tail Mode is active.
181 \(When the string is not empty, make sure that it has a leading space.)"
182 :group 'auto-revert
183 :type 'string
184 :version "22.1")
186 (defcustom auto-revert-mode-hook nil
187 "Functions to run when Auto-Revert Mode is activated."
188 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
189 :group 'auto-revert
190 :type 'hook)
192 (defcustom global-auto-revert-mode-text ""
193 "String to display when Global Auto-Revert Mode is active.
195 The default is nothing since when this mode is active this text doesn't
196 vary over time, or between buffers. Hence mode line text
197 would only waste precious space."
198 :group 'auto-revert
199 :type 'string)
201 (defcustom global-auto-revert-mode-hook nil
202 "Hook called when Global Auto-Revert Mode is activated."
203 :group 'auto-revert
204 :type 'hook)
206 (defcustom global-auto-revert-non-file-buffers nil
207 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
209 When non-nil, both file buffers and buffers with a custom
210 `revert-buffer-function' and a `buffer-stale-function' are
211 reverted by Global Auto-Revert mode. These include the Buffer
212 List buffer displayed by `buffer-menu', and Dired buffers showing
213 complete local directories. The Buffer List buffer reverts every
214 `auto-revert-interval' seconds; Dired buffers when the file list of
215 the main directory changes. Dired buffers do not auto-revert as
216 a result of changes in subdirectories, or in the contents, size,
217 modes, etc., of files. You may still sometimes want to revert
218 them manually.
220 Use this option with care since it could lead to excessive auto-reverts.
221 For more information, see Info node `(emacs)Autorevert'."
222 :group 'auto-revert
223 :type 'boolean
224 :link '(info-link "(emacs)Autorevert"))
226 (defcustom global-auto-revert-ignore-modes ()
227 "List of major modes Global Auto-Revert Mode should not check."
228 :group 'auto-revert
229 :type '(repeat sexp))
231 (defcustom auto-revert-load-hook nil
232 "Functions to run when Auto-Revert Mode is first loaded."
233 :tag "Load Hook"
234 :group 'auto-revert
235 :type 'hook)
237 (defcustom auto-revert-check-vc-info nil
238 "If non-nil Auto Revert Mode reliably updates version control info.
239 Auto Revert Mode updates version control info whenever the buffer
240 needs reverting, regardless of the value of this variable.
241 However, the version control state can change without changes to
242 the work file. If the change is made from the current Emacs
243 session, all info is updated. But if, for instance, a new
244 version is checked in from outside the current Emacs session, the
245 version control number in the mode line, as well as other version
246 control related information, may not be properly updated. If you
247 are worried about this, set this variable to a non-nil value.
249 This currently works by automatically updating the version
250 control info every `auto-revert-interval' seconds. Nevertheless,
251 it should not cause excessive CPU usage on a reasonably fast
252 machine, if it does not apply to too many version controlled
253 buffers. CPU usage depends on the version control system."
254 :group 'auto-revert
255 :type 'boolean
256 :version "22.1")
258 (defvar global-auto-revert-ignore-buffer nil
259 "When non-nil, Global Auto-Revert Mode will not revert this buffer.
260 This variable becomes buffer local when set in any fashion.")
261 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
263 (defconst auto-revert-notify-enabled
264 (or (featurep 'inotify) (featurep 'w32notify))
265 "Non-nil when Emacs has been compiled with file watch support.")
267 (defcustom auto-revert-use-notify auto-revert-notify-enabled
268 "If non-nil Auto Revert Mode uses file watch functions.
269 This requires Emacs being compiled with file watch support (see
270 `auto-revert-notify-enabled'). You should set this variable
271 through Custom only."
272 :group 'auto-revert
273 :type 'boolean
274 :set (lambda (variable value)
275 (set-default variable (and auto-revert-notify-enabled value))
276 (if (symbol-value variable)
277 (add-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch)
278 (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch)
279 (when auto-revert-notify-enabled
280 (dolist (buf (buffer-list))
281 (with-current-buffer buf
282 (auto-revert-notify-rm-watch))))))
283 :version "24.4")
285 ;; Internal variables:
287 (defvar auto-revert-buffer-list ()
288 "List of buffers in Auto-Revert Mode.
290 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
291 buffers to this list.
293 The timer function `auto-revert-buffers' is responsible for purging
294 the list of old buffers.")
296 (defvar auto-revert-remaining-buffers ()
297 "Buffers not checked when user input stopped execution.")
299 (defvar auto-revert-tail-pos 0
300 "Position of last known end of file.")
302 (add-hook 'find-file-hook
303 (lambda ()
304 (set (make-local-variable 'auto-revert-tail-pos)
305 (nth 7 (file-attributes buffer-file-name)))))
307 (defvar auto-revert-notify-watch-descriptor-hash-list
308 (make-hash-table :test 'equal)
309 "A hash table collecting all file watch descriptors.
310 Hash key is a watch descriptor, hash value is the corresponding buffer.")
312 (defvar auto-revert-notify-watch-descriptor nil
313 "The file watch descriptor active for the current buffer.")
314 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
316 (defvar auto-revert-notify-modified-p nil
317 "Non-nil when file has been modified on the file system.
318 This has been reported by a file watch event.")
319 (make-variable-buffer-local 'auto-revert-notify-modified-p)
321 ;; Functions:
323 ;;;###autoload
324 (define-minor-mode auto-revert-mode
325 "Toggle reverting buffer when the file changes (Auto Revert mode).
326 With a prefix argument ARG, enable Auto Revert mode if ARG is
327 positive, and disable it otherwise. If called from Lisp, enable
328 the mode if ARG is omitted or nil.
330 Auto Revert mode is a minor mode that affects only the current
331 buffer. When enabled, it reverts the buffer when the file on
332 disk changes.
334 Use `global-auto-revert-mode' to automatically revert all buffers.
335 Use `auto-revert-tail-mode' if you know that the file will only grow
336 without being changed in the part that is already in the buffer."
337 :group 'auto-revert :lighter auto-revert-mode-text
338 (if auto-revert-mode
339 (if (not (memq (current-buffer) auto-revert-buffer-list))
340 (push (current-buffer) auto-revert-buffer-list))
341 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
342 (setq auto-revert-buffer-list
343 (delq (current-buffer) auto-revert-buffer-list)))
344 (auto-revert-set-timer)
345 (when auto-revert-mode
346 (auto-revert-buffers)
347 (setq auto-revert-tail-mode nil)))
350 ;;;###autoload
351 (defun turn-on-auto-revert-mode ()
352 "Turn on Auto-Revert Mode.
354 This function is designed to be added to hooks, for example:
355 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
356 (auto-revert-mode 1))
359 ;;;###autoload
360 (define-minor-mode auto-revert-tail-mode
361 "Toggle reverting tail of buffer when the file grows.
362 With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
363 is positive, and disable it otherwise. If called from Lisp,
364 enable the mode if ARG is omitted or nil.
366 When Auto Revert Tail mode is enabled, the tail of the file is
367 constantly followed, as with the shell command `tail -f'. This
368 means that whenever the file grows on disk (presumably because
369 some background process is appending to it from time to time),
370 this is reflected in the current buffer.
372 You can edit the buffer and turn this mode off and on again as
373 you please. But make sure the background process has stopped
374 writing before you save the file!
376 Use `auto-revert-mode' for changes other than appends!"
377 :group 'find-file :lighter auto-revert-tail-mode-text
378 (when auto-revert-tail-mode
379 (unless buffer-file-name
380 (auto-revert-tail-mode 0)
381 (error "This buffer is not visiting a file"))
382 (if (and (buffer-modified-p)
383 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
384 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
385 (auto-revert-tail-mode 0)
386 ;; a-r-tail-pos stores the size of the file at the time of the
387 ;; last revert. After this package loads, it adds a
388 ;; find-file-hook to set this variable every time a file is
389 ;; loaded. If the package is loaded only _after_ visiting the
390 ;; file to be reverted, then we have no idea what the value of
391 ;; a-r-tail-pos should have been when the file was visited. If
392 ;; the file has changed on disk in the meantime, all we can do
393 ;; is offer to revert the whole thing. If you choose not to
394 ;; revert, then you might miss some output then happened
395 ;; between visiting the file and activating a-r-t-mode.
396 (and (zerop auto-revert-tail-pos)
397 (not (verify-visited-file-modtime (current-buffer)))
398 (y-or-n-p "File changed on disk, content may be missing. \
399 Perform a full revert? ")
400 ;; Use this (not just revert-buffer) for point-preservation.
401 (auto-revert-handler))
402 ;; else we might reappend our own end when we save
403 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
404 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
405 (set (make-local-variable 'auto-revert-tail-pos)
406 (nth 7 (file-attributes buffer-file-name))))
407 ;; let auto-revert-mode set up the mechanism for us if it isn't already
408 (or auto-revert-mode
409 (let ((auto-revert-tail-mode t))
410 (auto-revert-mode 1)))
411 (setq auto-revert-mode nil))))
414 ;;;###autoload
415 (defun turn-on-auto-revert-tail-mode ()
416 "Turn on Auto-Revert Tail mode.
418 This function is designed to be added to hooks, for example:
419 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
420 (auto-revert-tail-mode 1))
423 ;;;###autoload
424 (define-minor-mode global-auto-revert-mode
425 "Toggle Global Auto Revert mode.
426 With a prefix argument ARG, enable Global Auto Revert mode if ARG
427 is positive, and disable it otherwise. If called from Lisp,
428 enable the mode if ARG is omitted or nil.
430 Global Auto Revert mode is a global minor mode that reverts any
431 buffer associated with a file when the file changes on disk. Use
432 `auto-revert-mode' to revert a particular buffer.
434 If `global-auto-revert-non-file-buffers' is non-nil, this mode
435 may also revert some non-file buffers, as described in the
436 documentation of that variable. It ignores buffers with modes
437 matching `global-auto-revert-ignore-modes', and buffers with a
438 non-nil vale of `global-auto-revert-ignore-buffer'.
440 This function calls the hook `global-auto-revert-mode-hook'.
441 It displays the text that `global-auto-revert-mode-text'
442 specifies in the mode line."
443 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
444 (auto-revert-set-timer)
445 (if global-auto-revert-mode
446 (auto-revert-buffers)
447 (when auto-revert-use-notify
448 (dolist (buf (buffer-list))
449 (with-current-buffer buf
450 (auto-revert-notify-rm-watch))))))
452 (defun auto-revert-set-timer ()
453 "Restart or cancel the timer used by Auto-Revert Mode.
454 If such a timer is active, cancel it. Start a new timer if
455 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
456 in some buffer. Restarting the timer ensures that Auto-Revert Mode
457 will use an up-to-date value of `auto-revert-interval'"
458 (interactive)
459 (if (timerp auto-revert-timer)
460 (cancel-timer auto-revert-timer))
461 (setq auto-revert-timer
462 (if (or global-auto-revert-mode auto-revert-buffer-list)
463 (run-with-timer auto-revert-interval
464 auto-revert-interval
465 'auto-revert-buffers))))
467 (defun auto-revert-notify-rm-watch ()
468 "Disable file watch for current buffer's associated file."
469 (when auto-revert-notify-watch-descriptor
470 (ignore-errors
471 (funcall (if (fboundp 'inotify-rm-watch)
472 'inotify-rm-watch 'w32notify-rm-watch)
473 auto-revert-notify-watch-descriptor))
474 (remhash auto-revert-notify-watch-descriptor
475 auto-revert-notify-watch-descriptor-hash-list))
476 (setq auto-revert-notify-watch-descriptor nil
477 auto-revert-notify-modified-p nil))
479 (defun auto-revert-notify-add-watch ()
480 "Enable file watch for current buffer's associated file."
481 (when (and buffer-file-name auto-revert-use-notify)
482 (auto-revert-notify-rm-watch)
483 (let ((func (if (fboundp 'inotify-add-watch)
484 'inotify-add-watch 'w32notify-add-watch))
485 (aspect (if (fboundp 'inotify-add-watch)
486 '(modify) '(size last-write-time))))
487 (setq auto-revert-notify-watch-descriptor
488 (ignore-errors
489 (funcall
490 func buffer-file-name aspect 'auto-revert-notify-handler)))
491 (if auto-revert-notify-watch-descriptor
492 (puthash auto-revert-notify-watch-descriptor
493 (current-buffer)
494 auto-revert-notify-watch-descriptor-hash-list)
495 ;; Fallback to file checks.
496 (set (make-local-variable 'auto-revert-use-notify) nil)))))
498 (defun auto-revert-notify-event-p (event)
499 "Check that event is a file watch event."
500 (cond ((featurep 'inotify)
501 (and (listp event) (= (length event) 4)))
502 ((featurep 'w32notify)
503 (and (listp event) (= (length event) 3) (stringp (nth 2 event))))))
505 (defun auto-revert-notify-event-descriptor (event)
506 "Return watch descriptor of notification event, or nil."
507 (and (auto-revert-notify-event-p event) (car event)))
509 (defun auto-revert-notify-event-action (event)
510 "Return action of notification event, or nil."
511 (and (auto-revert-notify-event-p event) (nth 1 event)))
513 (defun auto-revert-notify-event-file-name (event)
514 "Return file name of notification event, or nil."
515 (and (auto-revert-notify-event-p event)
516 (cond ((featurep 'inotify) (nth 3 event))
517 ((featurep 'w32notify) (nth 2 event)))))
519 (defun auto-revert-notify-handler (event)
520 "Handle an event returned from file watch."
521 (when (auto-revert-notify-event-p event)
522 (let* ((descriptor (auto-revert-notify-event-descriptor event))
523 (action (auto-revert-notify-event-action event))
524 (file (auto-revert-notify-event-file-name event))
525 (buffer (gethash descriptor
526 auto-revert-notify-watch-descriptor-hash-list)))
527 (ignore-errors
528 ;; Check, that event is meant for us.
529 ;; TODO: Filter events which stop watching, like `move' or `removed'.
530 (cl-assert descriptor)
531 (when (featurep 'inotify) (cl-assert (memq 'modify action)))
532 (when (featurep 'w32notify) (cl-assert (eq 'modified action)))
533 (cl-assert (bufferp buffer))
534 (with-current-buffer buffer
535 (when (and (stringp file) (stringp buffer-file-name))
536 ;; w32notify returns the basename of the file without its
537 ;; leading directories; inotify returns its full absolute
538 ;; file name.
539 (cl-assert (file-equal-p file buffer-file-name)))
541 ;; Mark buffer modified.
542 (setq auto-revert-notify-modified-p t))))))
544 (defun auto-revert-active-p ()
545 "Check if auto-revert is active (in current buffer or globally)."
546 (or auto-revert-mode
547 auto-revert-tail-mode
548 (and
549 global-auto-revert-mode
550 (not global-auto-revert-ignore-buffer)
551 (not (memq major-mode
552 global-auto-revert-ignore-modes)))))
554 (defun auto-revert-handler ()
555 "Revert current buffer, if appropriate.
556 This is an internal function used by Auto-Revert Mode."
557 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
558 (let* ((buffer (current-buffer)) size
559 (revert
560 (or (and buffer-file-name
561 (or (not auto-revert-use-notify)
562 auto-revert-notify-modified-p)
563 (if auto-revert-tail-mode
564 ;; Tramp caches the file attributes. Setting
565 ;; `remote-file-name-inhibit-cache' forces Tramp
566 ;; to reread the values.
567 (let ((remote-file-name-inhibit-cache t))
568 (and (file-readable-p buffer-file-name)
569 (/= auto-revert-tail-pos
570 (setq size
571 (nth 7 (file-attributes
572 buffer-file-name))))))
573 (and (not (file-remote-p buffer-file-name))
574 (file-readable-p buffer-file-name)
575 (not (verify-visited-file-modtime buffer)))))
576 (and (or auto-revert-mode
577 global-auto-revert-non-file-buffers)
578 revert-buffer-function
579 (boundp 'buffer-stale-function)
580 (functionp buffer-stale-function)
581 (funcall buffer-stale-function t))))
582 eob eoblist)
583 (when revert
584 (setq auto-revert-notify-modified-p nil)
585 (when (and auto-revert-verbose
586 (not (eq revert 'fast)))
587 (message "Reverting buffer `%s'." (buffer-name)))
588 ;; If point (or a window point) is at the end of the buffer,
589 ;; we want to keep it at the end after reverting. This allows
590 ;; to tail a file.
591 (when buffer-file-name
592 (setq eob (eobp))
593 (walk-windows
594 (lambda (window)
595 (and (eq (window-buffer window) buffer)
596 (= (window-point window) (point-max))
597 (push window eoblist)))
598 'no-mini t))
599 (if auto-revert-tail-mode
600 (auto-revert-tail-handler size)
601 ;; Bind buffer-read-only in case user has done C-x C-q,
602 ;; so as not to forget that. This gives undesirable results
603 ;; when the file's mode changes, but that is less common.
604 (let ((buffer-read-only buffer-read-only))
605 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
606 (when buffer-file-name
607 (when eob (goto-char (point-max)))
608 (dolist (window eoblist)
609 (set-window-point window (point-max)))))
610 ;; `preserve-modes' avoids changing the (minor) modes. But we
611 ;; do want to reset the mode for VC, so we do it manually.
612 (when (or revert auto-revert-check-vc-info)
613 (vc-find-file-hook)))))
615 (defun auto-revert-tail-handler (size)
616 (let ((modified (buffer-modified-p))
617 (inhibit-read-only t) ; Ignore.
618 (file buffer-file-name)
619 (buffer-file-name nil)) ; Ignore that file has changed.
620 (when (/= auto-revert-tail-pos size)
621 (run-hooks 'before-revert-hook)
622 (undo-boundary)
623 (save-restriction
624 (widen)
625 (save-excursion
626 (goto-char (point-max))
627 (insert-file-contents file nil
628 (and (< auto-revert-tail-pos size)
629 auto-revert-tail-pos)
630 size)))
631 (run-hooks 'after-revert-hook)
632 (undo-boundary)
633 (setq auto-revert-tail-pos size)
634 (restore-buffer-modified-p modified)))
635 (set-visited-file-modtime))
637 (defun auto-revert-buffers ()
638 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
640 Should `global-auto-revert-mode' be active all file buffers are checked.
642 Should `auto-revert-mode' be active in some buffers, those buffers
643 are checked.
645 Non-file buffers that have a custom `revert-buffer-function' and
646 a `buffer-stale-function' are reverted either when Auto-Revert
647 Mode is active in that buffer, or when the variable
648 `global-auto-revert-non-file-buffers' is non-nil and Global
649 Auto-Revert Mode is active.
651 This function stops whenever there is user input. The buffers not
652 checked are stored in the variable `auto-revert-remaining-buffers'.
654 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
655 are checked first the next time this function is called.
657 This function is also responsible for removing buffers no longer in
658 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
659 the timer when no buffers need to be checked."
660 (save-match-data
661 (let ((bufs (if global-auto-revert-mode
662 (buffer-list)
663 auto-revert-buffer-list))
664 (remaining ())
665 (new ()))
666 ;; Partition `bufs' into two halves depending on whether or not
667 ;; the buffers are in `auto-revert-remaining-buffers'. The two
668 ;; halves are then re-joined with the "remaining" buffers at the
669 ;; head of the list.
670 (dolist (buf auto-revert-remaining-buffers)
671 (if (memq buf bufs)
672 (push buf remaining)))
673 (dolist (buf bufs)
674 (if (not (memq buf remaining))
675 (push buf new)))
676 (setq bufs (nreverse (nconc new remaining)))
677 (while (and bufs
678 (not (and auto-revert-stop-on-user-input
679 (input-pending-p))))
680 (let ((buf (car bufs)))
681 (if (buffer-live-p buf)
682 (with-current-buffer buf
683 ;; Test if someone has turned off Auto-Revert Mode in a
684 ;; non-standard way, for example by changing major mode.
685 (if (and (not auto-revert-mode)
686 (not auto-revert-tail-mode)
687 (memq buf auto-revert-buffer-list))
688 (setq auto-revert-buffer-list
689 (delq buf auto-revert-buffer-list)))
690 (when (auto-revert-active-p)
691 ;; Enable file watches.
692 (when (and auto-revert-use-notify buffer-file-name
693 (not auto-revert-notify-watch-descriptor)
694 (auto-revert-notify-add-watch)))
695 (auto-revert-handler)))
696 ;; Remove dead buffer from `auto-revert-buffer-list'.
697 (setq auto-revert-buffer-list
698 (delq buf auto-revert-buffer-list))))
699 (setq bufs (cdr bufs)))
700 (setq auto-revert-remaining-buffers bufs)
701 ;; Check if we should cancel the timer.
702 (when (and (not global-auto-revert-mode)
703 (null auto-revert-buffer-list))
704 (cancel-timer auto-revert-timer)
705 (setq auto-revert-timer nil)))))
708 ;; The end:
709 (provide 'autorevert)
711 (run-hooks 'auto-revert-load-hook)
713 ;;; autorevert.el ends here