* autorevert.el (auto-revert-notify-watch-descriptor): Give it
[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 (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")
284
285 ;; Internal variables:
286
287 (defvar auto-revert-buffer-list ()
288 "List of buffers in Auto-Revert Mode.
289
290 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
291 buffers to this list.
292
293 The timer function `auto-revert-buffers' is responsible for purging
294 the list of old buffers.")
295
296 (defvar auto-revert-remaining-buffers ()
297 "Buffers not checked when user input stopped execution.")
298
299 (defvar auto-revert-tail-pos 0
300 "Position of last known end of file.")
301
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)))))
306
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.")
311
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)
315
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)
320
321 ;; Functions:
322
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.
329
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.
333
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)))
348
349
350 ;;;###autoload
351 (defun turn-on-auto-revert-mode ()
352 "Turn on Auto-Revert Mode.
353
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))
357
358
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.
365
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.
371
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!
375
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))))
412
413
414 ;;;###autoload
415 (defun turn-on-auto-revert-tail-mode ()
416 "Turn on Auto-Revert Tail mode.
417
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))
421
422
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.
429
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.
433
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'.
439
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))))))
451
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))))
466
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))
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 (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)))))
497
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))))))
504
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)))
508
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)))
512
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)))))
518
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 (when (stringp file)
535 (cl-assert (string-equal
536 ;; w32notify returns the basename of the file
537 ;; without its leading directories; inotify
538 ;; returns its full absolute file name.
539 (file-name-nondirectory (directory-file-name file))
540 (file-name-nondirectory (directory-file-name
541 (buffer-file-name buffer))))))
542
543 ;; Mark buffer modified.
544 (with-current-buffer buffer
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