* autorevert.el (auto-revert-use-notify): In the :set function, do
[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 (eval-when-compile (require 'cl-lib))
101 (require 'timer)
102
103 ;; Custom Group:
104 ;;
105 ;; The two modes will be placed next to Auto Save Mode under the
106 ;; Files group under Emacs.
107
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)
114
115
116 ;; Variables:
117
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)
124
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)
130
131 (defvar auto-revert-timer nil
132 "Timer used by Auto-Revert Mode.")
133
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.
137
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))))
152
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)
163
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)
169
170 (defcustom auto-revert-mode-text " ARev"
171 "String to display in the mode line when Auto-Revert Mode is active.
172
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)
177
178 (defcustom auto-revert-tail-mode-text " Tail"
179 "String to display in the mode line when Auto-Revert Tail Mode is active.
180
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")
185
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)
191
192 (defcustom global-auto-revert-mode-text ""
193 "String to display when Global Auto-Revert Mode is active.
194
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)
200
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)
205
206 (defcustom global-auto-revert-non-file-buffers nil
207 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
208
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.
219
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"))
225
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))
230
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)
236
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.
248
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")
257
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)
262
263 (defconst auto-revert-notify-enabled
264 (or (featurep 'inotify) (featurep 'w32notify))
265 "Non-nil when Emacs has been compiled with file watch support.")
266
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 (unless (symbol-value variable)
277 (when auto-revert-notify-enabled
278 (dolist (buf (buffer-list))
279 (with-current-buffer buf
280 (when (symbol-value 'auto-revert-notify-watch-descriptor)
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 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
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 (ignore-errors
470 (funcall (if (fboundp 'inotify-rm-watch)
471 'inotify-rm-watch 'w32notify-rm-watch)
472 auto-revert-notify-watch-descriptor))
473 (remhash auto-revert-notify-watch-descriptor
474 auto-revert-notify-watch-descriptor-hash-list)
475 (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch))
476 (setq auto-revert-notify-watch-descriptor nil
477 auto-revert-notify-modified-p nil))
478
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 (not auto-revert-notify-watch-descriptor))
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 (progn
493 (puthash auto-revert-notify-watch-descriptor
494 (current-buffer)
495 auto-revert-notify-watch-descriptor-hash-list)
496 (add-hook (make-local-variable 'kill-buffer-hook)
497 'auto-revert-notify-rm-watch))
498 ;; Fallback to file checks.
499 (set (make-local-variable 'auto-revert-use-notify) nil)))))
500
501 (defun auto-revert-notify-event-p (event)
502 "Check that event is a file watch event."
503 (cond ((featurep 'inotify)
504 (and (listp event) (= (length event) 4)))
505 ((featurep 'w32notify)
506 (and (listp event) (= (length event) 3) (stringp (nth 2 event))))))
507
508 (defun auto-revert-notify-event-descriptor (event)
509 "Return watch descriptor of notification event, or nil."
510 (and (auto-revert-notify-event-p event) (car event)))
511
512 (defun auto-revert-notify-event-action (event)
513 "Return action of notification event, or nil."
514 (and (auto-revert-notify-event-p event) (nth 1 event)))
515
516 (defun auto-revert-notify-event-file-name (event)
517 "Return file name of notification event, or nil."
518 (and (auto-revert-notify-event-p event)
519 (cond ((featurep 'inotify) (nth 3 event))
520 ((featurep 'w32notify) (nth 2 event)))))
521
522 (defun auto-revert-notify-handler (event)
523 "Handle an event returned from file watch."
524 (when (auto-revert-notify-event-p event)
525 (let* ((descriptor (auto-revert-notify-event-descriptor event))
526 (action (auto-revert-notify-event-action event))
527 (file (auto-revert-notify-event-file-name event))
528 (buffer (gethash descriptor
529 auto-revert-notify-watch-descriptor-hash-list)))
530 (ignore-errors
531 ;; Check, that event is meant for us.
532 ;; TODO: Filter events which stop watching, like `move' or `removed'.
533 (cl-assert descriptor)
534 (when (featurep 'inotify) (cl-assert (memq 'modify action)))
535 (when (featurep 'w32notify) (cl-assert (eq 'modified action)))
536 (cl-assert (bufferp buffer))
537 (with-current-buffer buffer
538 (when (and (stringp file) (stringp buffer-file-name))
539 ;; w32notify returns the basename of the file without its
540 ;; leading directories; inotify returns its full absolute
541 ;; file name.
542 (cl-assert (file-equal-p file buffer-file-name)))
543
544 ;; Mark buffer modified.
545 (setq auto-revert-notify-modified-p t))))))
546
547 (defun auto-revert-active-p ()
548 "Check if auto-revert is active (in current buffer or globally)."
549 (or auto-revert-mode
550 auto-revert-tail-mode
551 (and
552 global-auto-revert-mode
553 (not global-auto-revert-ignore-buffer)
554 (not (memq major-mode
555 global-auto-revert-ignore-modes)))))
556
557 (defun auto-revert-handler ()
558 "Revert current buffer, if appropriate.
559 This is an internal function used by Auto-Revert Mode."
560 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
561 (let* ((buffer (current-buffer)) size
562 (revert
563 (or (and buffer-file-name
564 (or (not auto-revert-use-notify)
565 auto-revert-notify-modified-p)
566 (if auto-revert-tail-mode
567 ;; Tramp caches the file attributes. Setting
568 ;; `remote-file-name-inhibit-cache' forces Tramp
569 ;; to reread the values.
570 (let ((remote-file-name-inhibit-cache t))
571 (and (file-readable-p buffer-file-name)
572 (/= auto-revert-tail-pos
573 (setq size
574 (nth 7 (file-attributes
575 buffer-file-name))))))
576 (and (not (file-remote-p buffer-file-name))
577 (file-readable-p buffer-file-name)
578 (not (verify-visited-file-modtime buffer)))))
579 (and (or auto-revert-mode
580 global-auto-revert-non-file-buffers)
581 revert-buffer-function
582 (boundp 'buffer-stale-function)
583 (functionp buffer-stale-function)
584 (funcall buffer-stale-function t))))
585 eob eoblist)
586 (when revert
587 (setq auto-revert-notify-modified-p nil)
588 (when (and auto-revert-verbose
589 (not (eq revert 'fast)))
590 (message "Reverting buffer `%s'." (buffer-name)))
591 ;; If point (or a window point) is at the end of the buffer,
592 ;; we want to keep it at the end after reverting. This allows
593 ;; to tail a file.
594 (when buffer-file-name
595 (setq eob (eobp))
596 (walk-windows
597 (lambda (window)
598 (and (eq (window-buffer window) buffer)
599 (= (window-point window) (point-max))
600 (push window eoblist)))
601 'no-mini t))
602 (if auto-revert-tail-mode
603 (auto-revert-tail-handler size)
604 ;; Bind buffer-read-only in case user has done C-x C-q,
605 ;; so as not to forget that. This gives undesirable results
606 ;; when the file's mode changes, but that is less common.
607 (let ((buffer-read-only buffer-read-only))
608 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
609 (when buffer-file-name
610 (when eob (goto-char (point-max)))
611 (dolist (window eoblist)
612 (set-window-point window (point-max)))))
613 ;; `preserve-modes' avoids changing the (minor) modes. But we
614 ;; do want to reset the mode for VC, so we do it manually.
615 (when (or revert auto-revert-check-vc-info)
616 (vc-find-file-hook)))))
617
618 (defun auto-revert-tail-handler (size)
619 (let ((modified (buffer-modified-p))
620 (inhibit-read-only t) ; Ignore.
621 (file buffer-file-name)
622 (buffer-file-name nil)) ; Ignore that file has changed.
623 (when (/= auto-revert-tail-pos size)
624 (run-hooks 'before-revert-hook)
625 (undo-boundary)
626 (save-restriction
627 (widen)
628 (save-excursion
629 (goto-char (point-max))
630 (insert-file-contents file nil
631 (and (< auto-revert-tail-pos size)
632 auto-revert-tail-pos)
633 size)))
634 (run-hooks 'after-revert-hook)
635 (undo-boundary)
636 (setq auto-revert-tail-pos size)
637 (restore-buffer-modified-p modified)))
638 (set-visited-file-modtime))
639
640 (defun auto-revert-buffers ()
641 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
642
643 Should `global-auto-revert-mode' be active all file buffers are checked.
644
645 Should `auto-revert-mode' be active in some buffers, those buffers
646 are checked.
647
648 Non-file buffers that have a custom `revert-buffer-function' and
649 a `buffer-stale-function' are reverted either when Auto-Revert
650 Mode is active in that buffer, or when the variable
651 `global-auto-revert-non-file-buffers' is non-nil and Global
652 Auto-Revert Mode is active.
653
654 This function stops whenever there is user input. The buffers not
655 checked are stored in the variable `auto-revert-remaining-buffers'.
656
657 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
658 are checked first the next time this function is called.
659
660 This function is also responsible for removing buffers no longer in
661 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
662 the timer when no buffers need to be checked."
663 (save-match-data
664 (let ((bufs (if global-auto-revert-mode
665 (buffer-list)
666 auto-revert-buffer-list))
667 (remaining ())
668 (new ()))
669 ;; Partition `bufs' into two halves depending on whether or not
670 ;; the buffers are in `auto-revert-remaining-buffers'. The two
671 ;; halves are then re-joined with the "remaining" buffers at the
672 ;; head of the list.
673 (dolist (buf auto-revert-remaining-buffers)
674 (if (memq buf bufs)
675 (push buf remaining)))
676 (dolist (buf bufs)
677 (if (not (memq buf remaining))
678 (push buf new)))
679 (setq bufs (nreverse (nconc new remaining)))
680 (while (and bufs
681 (not (and auto-revert-stop-on-user-input
682 (input-pending-p))))
683 (let ((buf (car bufs)))
684 (if (buffer-live-p buf)
685 (with-current-buffer buf
686 ;; Test if someone has turned off Auto-Revert Mode in a
687 ;; non-standard way, for example by changing major mode.
688 (if (and (not auto-revert-mode)
689 (not auto-revert-tail-mode)
690 (memq buf auto-revert-buffer-list))
691 (setq auto-revert-buffer-list
692 (delq buf auto-revert-buffer-list)))
693 (when (auto-revert-active-p)
694 ;; Enable file watches.
695 (when (and auto-revert-use-notify buffer-file-name
696 (not auto-revert-notify-watch-descriptor)
697 (auto-revert-notify-add-watch)))
698 (auto-revert-handler)))
699 ;; Remove dead buffer from `auto-revert-buffer-list'.
700 (setq auto-revert-buffer-list
701 (delq buf auto-revert-buffer-list))))
702 (setq bufs (cdr bufs)))
703 (setq auto-revert-remaining-buffers bufs)
704 ;; Check if we should cancel the timer.
705 (when (and (not global-auto-revert-mode)
706 (null auto-revert-buffer-list))
707 (cancel-timer auto-revert-timer)
708 (setq auto-revert-timer nil)))))
709
710
711 ;; The end:
712 (provide 'autorevert)
713
714 (run-hooks 'auto-revert-load-hook)
715
716 ;;; autorevert.el ends here