1 ;;; autorevert.el --- revert buffers when files on disk change -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-1999, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren
6 ;; Keywords: convenience
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/>.
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.)
43 ;; Since checking a remote file is slow, these modes check or revert
44 ;; remote files only if the user option `auto-revert-remote-files' is
45 ;; non-nil. It is recommended to disable version control for remote
48 ;; Both modes operate by checking the time stamp of all files at
49 ;; intervals of `auto-revert-interval'. The default is every five
50 ;; seconds. The check is aborted whenever the user actually uses
51 ;; Emacs. You should never even notice that this package is active
52 ;; (except that your buffers will be reverted, of course).
54 ;; If the file exists, Auto-Revert Mode updates the buffer based on
55 ;; its (possibly empty) contents. If the file no longer exists, then
56 ;; there is nothing to revert, so it does not modify the buffer. Once
57 ;; a deleted file corresponding to a buffer in Auto-Revert Mode
58 ;; reappears, Auto-Revert Mode continues to work.
60 ;; If Emacs is compiled with file notification support, notifications
61 ;; are used instead of checking the time stamp of the files. You can
62 ;; disable this by setting the user option `auto-revert-use-notify' to
63 ;; nil. Alternatively, a regular expression of directories to be
64 ;; excluded from file notifications can be specified by
65 ;; `auto-revert-notify-exclude-dir-regexp'.
67 ;; After reverting a file buffer, Auto-Revert Mode normally puts point
68 ;; at the same position that a regular manual revert would. However,
69 ;; there is one exception to this rule. If point is at the end of the
70 ;; buffer before reverting, it stays at the end. Similarly if point
71 ;; is displayed at the end of a file buffer in any window, it will stay
72 ;; at the end of the buffer in that window, even if the window is not
73 ;; selected. This way, you can use Auto-Revert Mode to `tail' a file.
74 ;; Just put point at the end of the buffer and it will stay there.
75 ;; These rules apply to file buffers. For non-file buffers, the
76 ;; behavior may be mode dependent.
78 ;; While you can use Auto-Revert Mode to tail a file, this package
79 ;; contains a third minor mode, Auto-Revert Tail Mode, which does so
80 ;; more efficiently, as long as you are sure that the file will only
81 ;; change by growing at the end. It only appends the new output,
82 ;; instead of reverting the entire buffer. It does so even if the
83 ;; buffer contains unsaved changes. (Because they will not be lost.)
87 ;; Go to the appropriate buffer and press either of:
88 ;; M-x auto-revert-mode RET
89 ;; M-x auto-revert-tail-mode RET
91 ;; To activate Global Auto-Revert Mode, press:
92 ;; M-x global-auto-revert-mode RET
94 ;; To activate Global Auto-Revert Mode every time Emacs is started
95 ;; customize the option `global-auto-revert-mode' or the following
96 ;; line could be added to your ~/.emacs:
97 ;; (global-auto-revert-mode 1)
99 ;; The function `turn-on-auto-revert-mode' could be added to any major
100 ;; mode hook to activate Auto-Revert Mode for all buffers in that
101 ;; mode. For example, the following line will activate Auto-Revert
102 ;; Mode in all C mode buffers:
104 ;; (add-hook 'c-mode-hook #'turn-on-auto-revert-mode)
110 (eval-when-compile (require 'cl-lib
))
112 (require 'filenotify
)
116 ;; The two modes will be placed next to Auto Save Mode under the
117 ;; Files group under Emacs.
119 (defgroup auto-revert nil
120 "Revert individual buffers when files on disk change.
121 Auto-Revert Mode enables auto-revert in individual buffers.
122 Global Auto-Revert Mode does so in all buffers."
129 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
130 ;;; What's this?: ;;;###autoload
131 (defvar auto-revert-mode nil
132 "Non-nil when Auto-Revert Mode is active.
133 Never set this variable directly, use the command `auto-revert-mode' instead.")
134 (put 'auto-revert-mode
'permanent-local t
)
136 (defvar auto-revert-tail-mode nil
137 "Non-nil when Auto-Revert Tail Mode is active.
138 Never set this variable directly, use the command
139 `auto-revert-tail-mode' instead.")
140 (put 'auto-revert-tail-mode
'permanent-local t
)
142 (defvar auto-revert-timer nil
143 "Timer used by Auto-Revert Mode.")
145 (defcustom auto-revert-interval
5
146 "Time, in seconds, between Auto-Revert Mode file checks.
147 The value may be an integer or floating point number.
149 If a timer is already active, there are two ways to make sure
150 that the new value will take effect immediately. You can set
151 this variable through Custom or you can call the command
152 `auto-revert-set-timer' after setting the variable. Otherwise,
153 the new value will take effect the first time Auto-Revert Mode
154 calls `auto-revert-set-timer' for internal reasons or in your
155 next editing session."
158 :set
(lambda (variable value
)
159 (set-default variable value
)
160 (and (boundp 'auto-revert-timer
)
162 (auto-revert-set-timer))))
164 (defcustom auto-revert-stop-on-user-input t
165 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
166 With this setting, Auto-Revert Mode checks for user input after
167 handling each buffer and does not process any further buffers
168 \(until the next run of the timer) if user input is available.
169 When nil, Auto-Revert Mode checks files and reverts buffers,
170 with quitting disabled, without paying attention to user input.
171 Thus, with this setting, Emacs might be non-responsive at times."
175 (defcustom auto-revert-verbose t
176 "When nil, Auto-Revert Mode does not generate any messages.
177 When non-nil, a message is generated whenever a buffer is reverted."
181 (defcustom auto-revert-mode-text
" ARev"
182 "String to display in the mode line when Auto-Revert Mode is active.
184 \(When the string is not empty, make sure that it has a leading space.)"
185 :tag
"Auto-Revert Mode Text" ; To separate it from `global-...'
189 (defcustom auto-revert-tail-mode-text
" Tail"
190 "String to display in the mode line when Auto-Revert Tail Mode is active.
192 \(When the string is not empty, make sure that it has a leading space.)"
197 (defcustom auto-revert-mode-hook nil
198 "Functions to run when Auto-Revert Mode is activated."
199 :tag
"Auto-Revert Mode Hook" ; To separate it from `global-...'
203 (defcustom global-auto-revert-mode-text
""
204 "String to display when Global Auto-Revert Mode is active.
206 The default is nothing since when this mode is active this text doesn't
207 vary over time, or between buffers. Hence mode line text
208 would only waste precious space."
212 (defcustom global-auto-revert-mode-hook nil
213 "Hook called when Global Auto-Revert Mode is activated."
217 (defcustom global-auto-revert-non-file-buffers nil
218 "When nil, Global Auto-Revert Mode operates only on file-visiting buffers.
220 When non-nil, both file buffers and buffers with a custom
221 `revert-buffer-function' and a `buffer-stale-function' are
222 reverted by Global Auto-Revert Mode. These include the Buffer
223 List buffer displayed by `buffer-menu', and Dired buffers showing
224 complete local directories. The Buffer List buffer reverts every
225 `auto-revert-interval' seconds; Dired buffers when the file list of
226 the main directory changes. Dired buffers do not auto-revert as
227 a result of changes in subdirectories, or in the contents, size,
228 modes, etc., of files. You may still sometimes want to revert
231 Use this option with care since it could lead to excessive auto-reverts.
232 For more information, see Info node `(emacs)Autorevert'."
235 :link
'(info-link "(emacs)Autorevert"))
237 (defcustom global-auto-revert-ignore-modes
()
238 "List of major modes Global Auto-Revert Mode should not check."
240 :type
'(repeat sexp
))
242 (defcustom auto-revert-load-hook nil
243 "Functions to run when Auto-Revert Mode is first loaded."
248 (defcustom auto-revert-check-vc-info nil
249 "If non-nil Auto-Revert Mode reliably updates version control info.
250 Auto-Revert Mode updates version control info whenever the buffer
251 needs reverting, regardless of the value of this variable.
252 However, the version control state can change without changes to
253 the work file. If the change is made from the current Emacs
254 session, all info is updated. But if, for instance, a new
255 version is checked in from outside the current Emacs session, the
256 version control number in the mode line, as well as other version
257 control related information, may not be properly updated. If you
258 are worried about this, set this variable to a non-nil value.
260 This currently works by automatically updating the version
261 control info every `auto-revert-interval' seconds. Nevertheless,
262 it should not cause excessive CPU usage on a reasonably fast
263 machine, if it does not apply to too many version controlled
264 buffers. CPU usage depends on the version control system."
269 (defvar-local global-auto-revert-ignore-buffer nil
270 "When non-nil, Global Auto-Revert Mode will not revert this buffer.
271 This variable becomes buffer local when set in any fashion.")
273 (defcustom auto-revert-remote-files nil
274 "If non-nil remote files are also reverted."
279 (defcustom auto-revert-use-notify t
280 "If non-nil Auto-Revert Mode uses file notification functions.
281 You should set this variable through Custom."
284 :set
(lambda (variable value
)
285 (set-default variable value
)
286 (unless (symbol-value variable
)
287 (dolist (buf (buffer-list))
288 (with-current-buffer buf
289 (when (symbol-value 'auto-revert-notify-watch-descriptor
)
290 (auto-revert-notify-rm-watch))))))
291 :initialize
'custom-initialize-default
294 (defcustom auto-revert-notify-exclude-dir-regexp
296 ;; No mounted file systems.
297 "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
299 (unless auto-revert-remote-files
"\\|^/[^/|:][^/|]+:"))
300 "Regular expression of directories to be excluded from file notifications."
305 ;; Internal variables:
307 (defvar auto-revert-buffer-list
()
308 "List of buffers in Auto-Revert Mode.
310 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
311 buffers to this list.
313 The timer function `auto-revert-buffers' is responsible for purging
314 the list of old buffers.")
316 (defvar auto-revert-remaining-buffers
()
317 "Buffers not checked when user input stopped execution.")
319 (defvar auto-revert-tail-pos
0
320 "Position of last known end of file.")
322 (add-hook 'find-file-hook
324 (setq-local auto-revert-tail-pos
325 (nth 7 (file-attributes buffer-file-name
)))))
327 (defvar auto-revert-notify-watch-descriptor-hash-list
328 (make-hash-table :test
'equal
)
329 "A hash table collecting all file watch descriptors.
330 Hash key is a watch descriptor, hash value is a list of buffers
331 which are related to files being watched and carrying the same
334 (defvar-local auto-revert-notify-watch-descriptor nil
335 "The file watch descriptor active for the current buffer.")
336 (put 'auto-revert-notify-watch-descriptor
'permanent-local t
)
338 (defvar-local auto-revert-notify-modified-p nil
339 "Non-nil when file has been modified on the file system.
340 This has been reported by a file notification event.")
345 (define-minor-mode auto-revert-mode
346 "Toggle reverting buffer when the file changes (Auto-Revert Mode).
347 With a prefix argument ARG, enable Auto-Revert Mode if ARG is
348 positive, and disable it otherwise. If called from Lisp, enable
349 the mode if ARG is omitted or nil.
351 Auto-Revert Mode is a minor mode that affects only the current
352 buffer. When enabled, it reverts the buffer when the file on
355 When a buffer is reverted, a message is generated. This can be
356 suppressed by setting `auto-revert-verbose' to nil.
358 Use `global-auto-revert-mode' to automatically revert all buffers.
359 Use `auto-revert-tail-mode' if you know that the file will only grow
360 without being changed in the part that is already in the buffer."
361 :group
'auto-revert
:lighter auto-revert-mode-text
363 (when (not (memq (current-buffer) auto-revert-buffer-list
))
364 (push (current-buffer) auto-revert-buffer-list
)
368 (setq auto-revert-buffer-list
369 (delq (current-buffer) auto-revert-buffer-list
)))
371 (when auto-revert-use-notify
(auto-revert-notify-rm-watch))
372 (setq auto-revert-buffer-list
373 (delq (current-buffer) auto-revert-buffer-list
)))
374 (auto-revert-set-timer)
375 (when auto-revert-mode
376 (auto-revert-buffers)
377 (setq auto-revert-tail-mode nil
)))
381 (defun turn-on-auto-revert-mode ()
382 "Turn on Auto-Revert Mode.
384 This function is designed to be added to hooks, for example:
385 (add-hook \\='c-mode-hook #\\='turn-on-auto-revert-mode)"
386 (auto-revert-mode 1))
390 (define-minor-mode auto-revert-tail-mode
391 "Toggle reverting tail of buffer when the file grows.
392 With a prefix argument ARG, enable Auto-Revert Tail Mode if ARG
393 is positive, and disable it otherwise. If called from Lisp,
394 enable the mode if ARG is omitted or nil.
396 When Auto-Revert Tail Mode is enabled, the tail of the file is
397 constantly followed, as with the shell command `tail -f'. This
398 means that whenever the file grows on disk (presumably because
399 some background process is appending to it from time to time),
400 this is reflected in the current buffer.
402 You can edit the buffer and turn this mode off and on again as
403 you please. But make sure the background process has stopped
404 writing before you save the file!
406 When a buffer is reverted, a message is generated. This can be
407 suppressed by setting `auto-revert-verbose' to nil.
409 Use `auto-revert-mode' for changes other than appends!"
410 :group
'find-file
:lighter auto-revert-tail-mode-text
411 (when auto-revert-tail-mode
412 (unless buffer-file-name
413 (auto-revert-tail-mode 0)
414 (error "This buffer is not visiting a file"))
415 (if (and (buffer-modified-p)
416 (zerop auto-revert-tail-pos
) ; library was loaded only after finding file
417 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
418 (auto-revert-tail-mode 0)
419 ;; a-r-tail-pos stores the size of the file at the time of the
420 ;; last revert. After this package loads, it adds a
421 ;; find-file-hook to set this variable every time a file is
422 ;; loaded. If the package is loaded only _after_ visiting the
423 ;; file to be reverted, then we have no idea what the value of
424 ;; a-r-tail-pos should have been when the file was visited. If
425 ;; the file has changed on disk in the meantime, all we can do
426 ;; is offer to revert the whole thing. If you choose not to
427 ;; revert, then you might miss some output then happened
428 ;; between visiting the file and activating a-r-t-mode.
429 (and (zerop auto-revert-tail-pos
)
430 (not (verify-visited-file-modtime (current-buffer)))
431 (y-or-n-p "File changed on disk, content may be missing. \
432 Perform a full revert? ")
433 ;; Use this (not just revert-buffer) for point-preservation.
434 (auto-revert-buffers))
435 ;; else we might reappend our own end when we save
436 (add-hook 'before-save-hook
(lambda () (auto-revert-tail-mode 0)) nil t
)
437 (or (local-variable-p 'auto-revert-tail-pos
) ; don't lose prior position
438 (setq-local auto-revert-tail-pos
439 (nth 7 (file-attributes buffer-file-name
))))
440 ;; let auto-revert-mode set up the mechanism for us if it isn't already
442 (let ((auto-revert-tail-mode t
))
443 (auto-revert-mode 1)))
444 (setq auto-revert-mode nil
))))
448 (defun turn-on-auto-revert-tail-mode ()
449 "Turn on Auto-Revert Tail Mode.
451 This function is designed to be added to hooks, for example:
452 (add-hook \\='my-logfile-mode-hook #\\='turn-on-auto-revert-tail-mode)"
453 (auto-revert-tail-mode 1))
457 (define-minor-mode global-auto-revert-mode
458 "Toggle Global Auto-Revert Mode.
459 With a prefix argument ARG, enable Global Auto-Revert Mode if ARG
460 is positive, and disable it otherwise. If called from Lisp,
461 enable the mode if ARG is omitted or nil.
463 Global Auto-Revert Mode is a global minor mode that reverts any
464 buffer associated with a file when the file changes on disk. Use
465 `auto-revert-mode' to revert a particular buffer.
467 If `global-auto-revert-non-file-buffers' is non-nil, this mode
468 may also revert some non-file buffers, as described in the
469 documentation of that variable. It ignores buffers with modes
470 matching `global-auto-revert-ignore-modes', and buffers with a
471 non-nil vale of `global-auto-revert-ignore-buffer'.
473 When a buffer is reverted, a message is generated. This can be
474 suppressed by setting `auto-revert-verbose' to nil.
476 This function calls the hook `global-auto-revert-mode-hook'.
477 It displays the text that `global-auto-revert-mode-text'
478 specifies in the mode line."
479 :global t
:group
'auto-revert
:lighter global-auto-revert-mode-text
480 (auto-revert-set-timer)
481 (if global-auto-revert-mode
482 (auto-revert-buffers)
483 (dolist (buf (buffer-list))
484 (with-current-buffer buf
485 (when auto-revert-use-notify
486 (auto-revert-notify-rm-watch))))))
488 (defun auto-revert-set-timer ()
489 "Restart or cancel the timer used by Auto-Revert Mode.
490 If such a timer is active, cancel it. Start a new timer if
491 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
492 in some buffer. Restarting the timer ensures that Auto-Revert Mode
493 will use an up-to-date value of `auto-revert-interval'"
495 (if (timerp auto-revert-timer
)
496 (cancel-timer auto-revert-timer
))
497 (setq auto-revert-timer
498 (if (or global-auto-revert-mode auto-revert-buffer-list
)
499 (run-with-timer auto-revert-interval
501 'auto-revert-buffers
))))
503 (defun auto-revert-notify-rm-watch ()
504 "Disable file notification for current buffer's associated file."
505 (when auto-revert-notify-watch-descriptor
508 (when (equal key auto-revert-notify-watch-descriptor
)
509 (setq value
(delete (current-buffer) value
))
511 (puthash key value auto-revert-notify-watch-descriptor-hash-list
)
512 (remhash key auto-revert-notify-watch-descriptor-hash-list
)
514 (file-notify-rm-watch auto-revert-notify-watch-descriptor
)))))
515 auto-revert-notify-watch-descriptor-hash-list
)
516 (remove-hook 'kill-buffer-hook
#'auto-revert-notify-rm-watch
))
517 (setq auto-revert-notify-watch-descriptor nil
518 auto-revert-notify-modified-p nil
))
520 (defun auto-revert-notify-add-watch ()
521 "Enable file notification for current buffer's associated file."
522 ;; We can assume that `buffer-file-name' and
523 ;; `auto-revert-use-notify' are non-nil.
524 (if (or (string-match auto-revert-notify-exclude-dir-regexp
525 (expand-file-name default-directory
))
526 (file-symlink-p (or buffer-file-name default-directory
)))
528 ;; Fallback to file checks.
529 (setq-local auto-revert-use-notify nil
)
531 (when (not auto-revert-notify-watch-descriptor
)
532 (setq auto-revert-notify-watch-descriptor
535 (file-notify-add-watch
536 (expand-file-name buffer-file-name default-directory
)
537 '(change attribute-change
)
538 'auto-revert-notify-handler
)
539 (file-notify-add-watch
540 (expand-file-name default-directory
)
542 'auto-revert-notify-handler
))))
543 (if auto-revert-notify-watch-descriptor
546 auto-revert-notify-watch-descriptor
547 (cons (current-buffer)
548 (gethash auto-revert-notify-watch-descriptor
549 auto-revert-notify-watch-descriptor-hash-list
))
550 auto-revert-notify-watch-descriptor-hash-list
)
551 (add-hook 'kill-buffer-hook
552 #'auto-revert-notify-rm-watch nil t
))
553 ;; Fallback to file checks.
554 (setq-local auto-revert-use-notify nil
)))))
556 ;; If we have file notifications, we want to update the auto-revert buffers
557 ;; immediately when a notification occurs. Since file updates can happen very
558 ;; often, we want to skip some revert operations so that we don't spend all our
559 ;; time reverting the buffer.
561 ;; We do this by reverting immediately in response to the first in a flurry of
562 ;; notifications. We suppress subsequent notifications until the next time
563 ;; `auto-revert-buffers' is called (this happens on a timer with a period set by
564 ;; `auto-revert-interval').
565 (defvar auto-revert-buffers-counter
1
566 "Incremented each time `auto-revert-buffers' is called")
567 (defvar-local auto-revert-buffers-counter-lockedout
0
568 "Buffer-local value to indicate whether we should immediately
569 update the buffer on a notification event or not. If
571 (= auto-revert-buffers-counter-lockedout
572 auto-revert-buffers-counter)
574 then the updates are locked out, and we wait until the next call
575 of `auto-revert-buffers' to revert the buffer. If no lockout is
576 present, then we revert immediately and set the lockout, so that
577 no more reverts are possible until the next call of
578 `auto-revert-buffers'")
580 (defun auto-revert-notify-handler (event)
581 "Handle an EVENT returned from file notification."
583 (let* ((descriptor (car event
))
584 (action (nth 1 event
))
586 (file1 (nth 3 event
)) ;; Target of `renamed'.
587 (buffers (gethash descriptor
588 auto-revert-notify-watch-descriptor-hash-list
)))
589 ;; Check, that event is meant for us.
590 (cl-assert descriptor
)
591 ;; Since we watch a directory, a file name must be returned.
592 (cl-assert (stringp file
))
593 (when (eq action
'renamed
) (cl-assert (stringp file1
)))
595 (if (eq action
'stopped
)
596 ;; File notification has stopped. Continue with polling.
598 (if global-auto-revert-mode
599 (buffer-list) auto-revert-buffer-list
))
600 (with-current-buffer buffer
601 (when (and (equal descriptor auto-revert-notify-watch-descriptor
)
603 ;; A buffer associated with a file.
604 (and (stringp buffer-file-name
)
606 (file-name-nondirectory file
)
607 (file-name-nondirectory buffer-file-name
)))
608 ;; A buffer w/o a file, like dired.
609 (null buffer-file-name
)))
610 (auto-revert-notify-rm-watch)
611 (setq-local auto-revert-use-notify nil
))))
613 ;; Loop over all buffers, in order to find the intended one.
614 (cl-dolist (buffer buffers
)
615 (when (buffer-live-p buffer
)
616 (with-current-buffer buffer
618 ;; A buffer associated with a file.
619 (and (stringp buffer-file-name
)
622 action
'(attribute-changed changed created
))
624 (file-name-nondirectory file
)
625 (file-name-nondirectory buffer-file-name
)))
626 (and (eq action
'renamed
)
628 (file-name-nondirectory file1
)
629 (file-name-nondirectory buffer-file-name
)))))
630 ;; A buffer w/o a file, like dired.
631 (and (null buffer-file-name
)
632 (memq action
'(created renamed deleted
))))
633 ;; Mark buffer modified.
634 (setq auto-revert-notify-modified-p t
)
636 ;; Revert the buffer now if we're not locked out.
637 (when (/= auto-revert-buffers-counter-lockedout
638 auto-revert-buffers-counter
)
639 (auto-revert-handler)
640 (setq auto-revert-buffers-counter-lockedout
641 auto-revert-buffers-counter
))
643 ;; No need to check other buffers.
646 (defun auto-revert-active-p ()
647 "Check if auto-revert is active (in current buffer or globally)."
649 auto-revert-tail-mode
651 global-auto-revert-mode
652 (not global-auto-revert-ignore-buffer
)
653 (not (memq major-mode
654 global-auto-revert-ignore-modes
)))))
656 (defun auto-revert-handler ()
657 "Revert current buffer, if appropriate.
658 This is an internal function used by Auto-Revert Mode."
659 (let* ((buffer (current-buffer)) size
660 ;; Tramp caches the file attributes. Setting
661 ;; `remote-file-name-inhibit-cache' forces Tramp to reread
663 (remote-file-name-inhibit-cache t
)
666 (and (or auto-revert-remote-files
667 (not (file-remote-p buffer-file-name
)))
668 (or (not auto-revert-use-notify
)
669 auto-revert-notify-modified-p
)
670 (if auto-revert-tail-mode
671 (and (file-readable-p buffer-file-name
)
672 (/= auto-revert-tail-pos
674 (nth 7 (file-attributes
675 buffer-file-name
)))))
676 (funcall (or buffer-stale-function
677 #'buffer-stale--default-function
)
679 (and (or auto-revert-mode
680 global-auto-revert-non-file-buffers
)
681 (funcall (or buffer-stale-function
682 #'buffer-stale--default-function
)
685 (setq auto-revert-notify-modified-p nil
)
687 (when (and auto-revert-verbose
688 (not (eq revert
'fast
)))
689 (message "Reverting buffer `%s'." (buffer-name)))
690 ;; If point (or a window point) is at the end of the buffer, we
691 ;; want to keep it at the end after reverting. This allows one
693 (when buffer-file-name
697 (and (eq (window-buffer window
) buffer
)
698 (= (window-point window
) (point-max))
699 (push window eoblist
)))
701 (if auto-revert-tail-mode
702 (auto-revert-tail-handler size
)
703 ;; Bind buffer-read-only in case user has done C-x C-q, so as
704 ;; not to forget that. This gives undesirable results when
705 ;; the file's mode changes, but that is less common.
706 (let ((buffer-read-only buffer-read-only
))
707 ;; Bug#23276: When the file has been deleted, keep the
710 (revert-buffer 'ignore-auto
'dont-ask
'preserve-modes
))))
711 (when buffer-file-name
712 (when eob
(goto-char (point-max)))
713 (dolist (window eoblist
)
714 (set-window-point window
(point-max)))))
715 ;; `preserve-modes' avoids changing the (minor) modes. But we do
716 ;; want to reset the mode for VC, so we do it manually.
717 (when (or revert auto-revert-check-vc-info
)
718 (vc-refresh-state))))
720 (defun auto-revert-tail-handler (size)
721 (let ((modified (buffer-modified-p))
722 (inhibit-read-only t
) ; Ignore.
723 (file buffer-file-name
)
724 (buffer-file-name nil
)) ; Ignore that file has changed.
725 (when (/= auto-revert-tail-pos size
)
726 (run-hooks 'before-revert-hook
)
731 (goto-char (point-max))
732 (insert-file-contents file nil
733 (and (< auto-revert-tail-pos size
)
734 auto-revert-tail-pos
)
736 (run-hooks 'after-revert-hook
)
738 (setq auto-revert-tail-pos size
)
739 (restore-buffer-modified-p modified
)))
740 (set-visited-file-modtime))
742 (defun auto-revert-buffers ()
743 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
745 Should `global-auto-revert-mode' be active all file buffers are checked.
747 Should `auto-revert-mode' be active in some buffers, those buffers
750 Non-file buffers that have a custom `revert-buffer-function' and
751 `buffer-stale-function' are reverted either when Auto-Revert
752 Mode is active in that buffer, or when the variable
753 `global-auto-revert-non-file-buffers' is non-nil and Global
754 Auto-Revert Mode is active.
756 This function stops whenever there is user input. The buffers not
757 checked are stored in the variable `auto-revert-remaining-buffers'.
759 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
760 are checked first the next time this function is called.
762 This function is also responsible for removing buffers no longer in
763 Auto-Revert Mode from `auto-revert-buffer-list', and for canceling
764 the timer when no buffers need to be checked."
766 (setq auto-revert-buffers-counter
767 (1+ auto-revert-buffers-counter
))
770 (let ((bufs (if global-auto-revert-mode
772 auto-revert-buffer-list
))
774 ;; Partition `bufs' into two halves depending on whether or not
775 ;; the buffers are in `auto-revert-remaining-buffers'. The two
776 ;; halves are then re-joined with the "remaining" buffers at the
778 (dolist (buf auto-revert-remaining-buffers
)
780 (push buf remaining
)))
782 (if (not (memq buf remaining
))
784 (setq bufs
(nreverse (nconc new remaining
)))
786 (not (and auto-revert-stop-on-user-input
788 (let ((buf (car bufs
)))
789 (if (buffer-live-p buf
)
790 (with-current-buffer buf
791 ;; Test if someone has turned off Auto-Revert Mode in a
792 ;; non-standard way, for example by changing major mode.
793 (if (and (not auto-revert-mode
)
794 (not auto-revert-tail-mode
)
795 (memq buf auto-revert-buffer-list
))
796 (setq auto-revert-buffer-list
797 (delq buf auto-revert-buffer-list
)))
798 (when (auto-revert-active-p)
799 ;; Enable file notification.
800 (when (and auto-revert-use-notify
801 (not auto-revert-notify-watch-descriptor
))
802 (auto-revert-notify-add-watch))
803 (auto-revert-handler)))
804 ;; Remove dead buffer from `auto-revert-buffer-list'.
805 (setq auto-revert-buffer-list
806 (delq buf auto-revert-buffer-list
))))
807 (setq bufs
(cdr bufs
)))
808 (setq auto-revert-remaining-buffers bufs
)
809 ;; Check if we should cancel the timer.
810 (when (and (not global-auto-revert-mode
)
811 (null auto-revert-buffer-list
))
812 (cancel-timer auto-revert-timer
)
813 (setq auto-revert-timer nil
)))))
817 (provide 'autorevert
)
819 (run-hooks 'auto-revert-load-hook
)
821 ;;; autorevert.el ends here