Add separator.pbm tool-bar image
[emacs.git] / lisp / autorevert.el
blob37ee8eedcfde0f4ca11167a2fd2d236b59bdcc22
1 ;;; autorevert.el --- revert buffers when files on disk change -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-1999, 2001-2015 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.)
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
46 ;; files.
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 Emacs is compiled with file notification support, notifications
55 ;; are used instead of checking the time stamp of the files. You can
56 ;; disable this by setting the user option `auto-revert-use-notify' to
57 ;; nil. Alternatively, a regular expression of directories to be
58 ;; excluded from file notifications can be specified by
59 ;; `auto-revert-notify-exclude-dir-regexp'.
61 ;; After reverting a file buffer, Auto Revert Mode normally puts point
62 ;; at the same position that a regular manual revert would. However,
63 ;; there is one exception to this rule. If point is at the end of the
64 ;; buffer before reverting, it stays at the end. Similarly if point
65 ;; is displayed at the end of a file buffer in any window, it will stay
66 ;; at the end of the buffer in that window, even if the window is not
67 ;; selected. This way, you can use Auto Revert Mode to `tail' a file.
68 ;; Just put point at the end of the buffer and it will stay there.
69 ;; These rules apply to file buffers. For non-file buffers, the
70 ;; behavior may be mode dependent.
72 ;; While you can use Auto Revert Mode to tail a file, this package
73 ;; contains a third minor mode, Auto Revert Tail Mode, which does so
74 ;; more efficiently, as long as you are sure that the file will only
75 ;; change by growing at the end. It only appends the new output,
76 ;; instead of reverting the entire buffer. It does so even if the
77 ;; buffer contains unsaved changes. (Because they will not be lost.)
79 ;; Usage:
81 ;; Go to the appropriate buffer and press either of:
82 ;; M-x auto-revert-mode RET
83 ;; M-x auto-revert-tail-mode RET
85 ;; To activate Global Auto-Revert Mode, press:
86 ;; M-x global-auto-revert-mode RET
88 ;; To activate Global Auto-Revert Mode every time Emacs is started
89 ;; customize the option `global-auto-revert-mode' or the following
90 ;; line could be added to your ~/.emacs:
91 ;; (global-auto-revert-mode 1)
93 ;; The function `turn-on-auto-revert-mode' could be added to any major
94 ;; mode hook to activate Auto-Revert Mode for all buffers in that
95 ;; mode. For example, the following line will activate Auto-Revert
96 ;; Mode in all C mode buffers:
98 ;; (add-hook 'c-mode-hook #'turn-on-auto-revert-mode)
100 ;;; Code:
102 ;; Dependencies:
104 (eval-when-compile (require 'cl-lib))
105 (require 'timer)
106 (require 'filenotify)
108 ;; Custom Group:
110 ;; The two modes will be placed next to Auto Save Mode under the
111 ;; Files group under Emacs.
113 (defgroup auto-revert nil
114 "Revert individual buffers when files on disk change.
115 Auto-Revert mode enables auto-revert in individual buffers.
116 Global Auto-Revert mode does so in all buffers."
117 :group 'files
118 :group 'convenience)
121 ;; Variables:
123 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
124 ;;; What's this?: ;;;###autoload
125 (defvar auto-revert-mode nil
126 "Non-nil when Auto-Revert Mode is active.
127 Never set this variable directly, use the command `auto-revert-mode' instead.")
128 (put 'auto-revert-mode 'permanent-local t)
130 (defvar auto-revert-tail-mode nil
131 "Non-nil when Auto-Revert Tail Mode is active.
132 Never set this variable directly, use the command
133 `auto-revert-tail-mode' instead.")
134 (put 'auto-revert-tail-mode 'permanent-local t)
136 (defvar auto-revert-timer nil
137 "Timer used by Auto-Revert Mode.")
139 (defcustom auto-revert-interval 5
140 "Time, in seconds, between Auto-Revert Mode file checks.
141 The value may be an integer or floating point number.
143 If a timer is already active, there are two ways to make sure
144 that the new value will take effect immediately. You can set
145 this variable through Custom or you can call the command
146 `auto-revert-set-timer' after setting the variable. Otherwise,
147 the new value will take effect the first time Auto Revert Mode
148 calls `auto-revert-set-timer' for internal reasons or in your
149 next editing session."
150 :group 'auto-revert
151 :type 'number
152 :set (lambda (variable value)
153 (set-default variable value)
154 (and (boundp 'auto-revert-timer)
155 auto-revert-timer
156 (auto-revert-set-timer))))
158 (defcustom auto-revert-stop-on-user-input t
159 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
160 With this setting, Auto-Revert Mode checks for user input after
161 handling each buffer and does not process any further buffers
162 \(until the next run of the timer) if user input is available.
163 When nil, Auto-Revert Mode checks files and reverts buffers,
164 with quitting disabled, without paying attention to user input.
165 Thus, with this setting, Emacs might be non-responsive at times."
166 :group 'auto-revert
167 :type 'boolean)
169 (defcustom auto-revert-verbose t
170 "When nil, Auto-Revert Mode does not generate any messages.
171 When non-nil, a message is generated whenever a file is reverted."
172 :group 'auto-revert
173 :type 'boolean)
175 (defcustom auto-revert-mode-text " ARev"
176 "String to display in the mode line when Auto-Revert Mode is active.
178 \(When the string is not empty, make sure that it has a leading space.)"
179 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
180 :group 'auto-revert
181 :type 'string)
183 (defcustom auto-revert-tail-mode-text " Tail"
184 "String to display in the mode line when Auto-Revert Tail Mode is active.
186 \(When the string is not empty, make sure that it has a leading space.)"
187 :group 'auto-revert
188 :type 'string
189 :version "22.1")
191 (defcustom auto-revert-mode-hook nil
192 "Functions to run when Auto-Revert Mode is activated."
193 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
194 :group 'auto-revert
195 :type 'hook)
197 (defcustom global-auto-revert-mode-text ""
198 "String to display when Global Auto-Revert Mode is active.
200 The default is nothing since when this mode is active this text doesn't
201 vary over time, or between buffers. Hence mode line text
202 would only waste precious space."
203 :group 'auto-revert
204 :type 'string)
206 (defcustom global-auto-revert-mode-hook nil
207 "Hook called when Global Auto-Revert Mode is activated."
208 :group 'auto-revert
209 :type 'hook)
211 (defcustom global-auto-revert-non-file-buffers nil
212 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
214 When non-nil, both file buffers and buffers with a custom
215 `revert-buffer-function' and a `buffer-stale-function' are
216 reverted by Global Auto-Revert mode. These include the Buffer
217 List buffer displayed by `buffer-menu', and Dired buffers showing
218 complete local directories. The Buffer List buffer reverts every
219 `auto-revert-interval' seconds; Dired buffers when the file list of
220 the main directory changes. Dired buffers do not auto-revert as
221 a result of changes in subdirectories, or in the contents, size,
222 modes, etc., of files. You may still sometimes want to revert
223 them manually.
225 Use this option with care since it could lead to excessive auto-reverts.
226 For more information, see Info node `(emacs)Autorevert'."
227 :group 'auto-revert
228 :type 'boolean
229 :link '(info-link "(emacs)Autorevert"))
231 (defcustom global-auto-revert-ignore-modes ()
232 "List of major modes Global Auto-Revert Mode should not check."
233 :group 'auto-revert
234 :type '(repeat sexp))
236 (defcustom auto-revert-load-hook nil
237 "Functions to run when Auto-Revert Mode is first loaded."
238 :tag "Load Hook"
239 :group 'auto-revert
240 :type 'hook)
242 (defcustom auto-revert-check-vc-info nil
243 "If non-nil Auto Revert Mode reliably updates version control info.
244 Auto Revert Mode updates version control info whenever the buffer
245 needs reverting, regardless of the value of this variable.
246 However, the version control state can change without changes to
247 the work file. If the change is made from the current Emacs
248 session, all info is updated. But if, for instance, a new
249 version is checked in from outside the current Emacs session, the
250 version control number in the mode line, as well as other version
251 control related information, may not be properly updated. If you
252 are worried about this, set this variable to a non-nil value.
254 This currently works by automatically updating the version
255 control info every `auto-revert-interval' seconds. Nevertheless,
256 it should not cause excessive CPU usage on a reasonably fast
257 machine, if it does not apply to too many version controlled
258 buffers. CPU usage depends on the version control system."
259 :group 'auto-revert
260 :type 'boolean
261 :version "22.1")
263 (defvar-local global-auto-revert-ignore-buffer nil
264 "When non-nil, Global Auto-Revert Mode will not revert this buffer.
265 This variable becomes buffer local when set in any fashion.")
267 (defcustom auto-revert-remote-files nil
268 "If non-nil remote files are also reverted."
269 :group 'auto-revert
270 :type 'boolean
271 :version "24.4")
273 (defcustom auto-revert-use-notify t
274 "If non-nil Auto Revert Mode uses file notification functions.
275 You should set this variable through Custom."
276 :group 'auto-revert
277 :type 'boolean
278 :set (lambda (variable value)
279 (set-default variable value)
280 (unless (symbol-value variable)
281 (dolist (buf (buffer-list))
282 (with-current-buffer buf
283 (when (symbol-value 'auto-revert-notify-watch-descriptor)
284 (auto-revert-notify-rm-watch))))))
285 :initialize 'custom-initialize-default
286 :version "24.4")
288 (defcustom auto-revert-notify-exclude-dir-regexp
289 (concat
290 ;; No mounted file systems.
291 "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
292 ;; No remote files.
293 (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
294 "Regular expression of directories to be excluded from file notifications."
295 :group 'auto-revert
296 :type 'regexp
297 :version "24.4")
299 ;; Internal variables:
301 (defvar auto-revert-buffer-list ()
302 "List of buffers in Auto-Revert Mode.
304 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
305 buffers to this list.
307 The timer function `auto-revert-buffers' is responsible for purging
308 the list of old buffers.")
310 (defvar auto-revert-remaining-buffers ()
311 "Buffers not checked when user input stopped execution.")
313 (defvar auto-revert-tail-pos 0
314 "Position of last known end of file.")
316 (add-hook 'find-file-hook
317 (lambda ()
318 (setq-local auto-revert-tail-pos
319 (nth 7 (file-attributes buffer-file-name)))))
321 (defvar auto-revert-notify-watch-descriptor-hash-list
322 (make-hash-table :test 'equal)
323 "A hash table collecting all file watch descriptors.
324 Hash key is a watch descriptor, hash value is a list of buffers
325 which are related to files being watched and carrying the same
326 default directory.")
328 (defvar-local auto-revert-notify-watch-descriptor nil
329 "The file watch descriptor active for the current buffer.")
330 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
332 (defvar-local auto-revert-notify-modified-p nil
333 "Non-nil when file has been modified on the file system.
334 This has been reported by a file notification event.")
336 ;; Functions:
338 ;;;###autoload
339 (define-minor-mode auto-revert-mode
340 "Toggle reverting buffer when the file changes (Auto Revert mode).
341 With a prefix argument ARG, enable Auto Revert mode if ARG is
342 positive, and disable it otherwise. If called from Lisp, enable
343 the mode if ARG is omitted or nil.
345 Auto Revert mode is a minor mode that affects only the current
346 buffer. When enabled, it reverts the buffer when the file on
347 disk changes.
349 Use `global-auto-revert-mode' to automatically revert all buffers.
350 Use `auto-revert-tail-mode' if you know that the file will only grow
351 without being changed in the part that is already in the buffer."
352 :group 'auto-revert :lighter auto-revert-mode-text
353 (if auto-revert-mode
354 (if (not (memq (current-buffer) auto-revert-buffer-list))
355 (push (current-buffer) auto-revert-buffer-list))
356 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
357 (setq auto-revert-buffer-list
358 (delq (current-buffer) auto-revert-buffer-list)))
359 (auto-revert-set-timer)
360 (when auto-revert-mode
361 (auto-revert-buffers)
362 (setq auto-revert-tail-mode nil)))
365 ;;;###autoload
366 (defun turn-on-auto-revert-mode ()
367 "Turn on Auto-Revert Mode.
369 This function is designed to be added to hooks, for example:
370 (add-hook \\='c-mode-hook #\\='turn-on-auto-revert-mode)"
371 (auto-revert-mode 1))
374 ;;;###autoload
375 (define-minor-mode auto-revert-tail-mode
376 "Toggle reverting tail of buffer when the file grows.
377 With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
378 is positive, and disable it otherwise. If called from Lisp,
379 enable the mode if ARG is omitted or nil.
381 When Auto Revert Tail mode is enabled, the tail of the file is
382 constantly followed, as with the shell command `tail -f'. This
383 means that whenever the file grows on disk (presumably because
384 some background process is appending to it from time to time),
385 this is reflected in the current buffer.
387 You can edit the buffer and turn this mode off and on again as
388 you please. But make sure the background process has stopped
389 writing before you save the file!
391 Use `auto-revert-mode' for changes other than appends!"
392 :group 'find-file :lighter auto-revert-tail-mode-text
393 (when auto-revert-tail-mode
394 (unless buffer-file-name
395 (auto-revert-tail-mode 0)
396 (error "This buffer is not visiting a file"))
397 (if (and (buffer-modified-p)
398 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
399 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
400 (auto-revert-tail-mode 0)
401 ;; a-r-tail-pos stores the size of the file at the time of the
402 ;; last revert. After this package loads, it adds a
403 ;; find-file-hook to set this variable every time a file is
404 ;; loaded. If the package is loaded only _after_ visiting the
405 ;; file to be reverted, then we have no idea what the value of
406 ;; a-r-tail-pos should have been when the file was visited. If
407 ;; the file has changed on disk in the meantime, all we can do
408 ;; is offer to revert the whole thing. If you choose not to
409 ;; revert, then you might miss some output then happened
410 ;; between visiting the file and activating a-r-t-mode.
411 (and (zerop auto-revert-tail-pos)
412 (not (verify-visited-file-modtime (current-buffer)))
413 (y-or-n-p "File changed on disk, content may be missing. \
414 Perform a full revert? ")
415 ;; Use this (not just revert-buffer) for point-preservation.
416 (auto-revert-buffers))
417 ;; else we might reappend our own end when we save
418 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
419 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
420 (setq-local auto-revert-tail-pos
421 (nth 7 (file-attributes buffer-file-name))))
422 ;; let auto-revert-mode set up the mechanism for us if it isn't already
423 (or auto-revert-mode
424 (let ((auto-revert-tail-mode t))
425 (auto-revert-mode 1)))
426 (setq auto-revert-mode nil))))
429 ;;;###autoload
430 (defun turn-on-auto-revert-tail-mode ()
431 "Turn on Auto-Revert Tail mode.
433 This function is designed to be added to hooks, for example:
434 (add-hook \\='my-logfile-mode-hook #\\='turn-on-auto-revert-tail-mode)"
435 (auto-revert-tail-mode 1))
438 ;;;###autoload
439 (define-minor-mode global-auto-revert-mode
440 "Toggle Global Auto Revert mode.
441 With a prefix argument ARG, enable Global Auto Revert mode if ARG
442 is positive, and disable it otherwise. If called from Lisp,
443 enable the mode if ARG is omitted or nil.
445 Global Auto Revert mode is a global minor mode that reverts any
446 buffer associated with a file when the file changes on disk. Use
447 `auto-revert-mode' to revert a particular buffer.
449 If `global-auto-revert-non-file-buffers' is non-nil, this mode
450 may also revert some non-file buffers, as described in the
451 documentation of that variable. It ignores buffers with modes
452 matching `global-auto-revert-ignore-modes', and buffers with a
453 non-nil vale of `global-auto-revert-ignore-buffer'.
455 This function calls the hook `global-auto-revert-mode-hook'.
456 It displays the text that `global-auto-revert-mode-text'
457 specifies in the mode line."
458 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
459 (auto-revert-set-timer)
460 (if global-auto-revert-mode
461 (auto-revert-buffers)
462 (dolist (buf (buffer-list))
463 (with-current-buffer buf
464 (when auto-revert-use-notify
465 (auto-revert-notify-rm-watch))))))
467 (defun auto-revert-set-timer ()
468 "Restart or cancel the timer used by Auto-Revert Mode.
469 If such a timer is active, cancel it. Start a new timer if
470 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
471 in some buffer. Restarting the timer ensures that Auto-Revert Mode
472 will use an up-to-date value of `auto-revert-interval'"
473 (interactive)
474 (if (timerp auto-revert-timer)
475 (cancel-timer auto-revert-timer))
476 (setq auto-revert-timer
477 (if (or global-auto-revert-mode auto-revert-buffer-list)
478 (run-with-timer auto-revert-interval
479 auto-revert-interval
480 'auto-revert-buffers))))
482 (defun auto-revert-notify-rm-watch ()
483 "Disable file notification for current buffer's associated file."
484 (when auto-revert-notify-watch-descriptor
485 (maphash
486 (lambda (key value)
487 (when (equal key auto-revert-notify-watch-descriptor)
488 (setq value (delete (current-buffer) value))
489 (if value
490 (puthash key value auto-revert-notify-watch-descriptor-hash-list)
491 (remhash key auto-revert-notify-watch-descriptor-hash-list)
492 (ignore-errors
493 (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
494 auto-revert-notify-watch-descriptor-hash-list)
495 (remove-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch))
496 (setq auto-revert-notify-watch-descriptor nil
497 auto-revert-notify-modified-p nil))
499 (defun auto-revert-notify-add-watch ()
500 "Enable file notification for current buffer's associated file."
501 ;; We can assume that `buffer-file-name' and
502 ;; `auto-revert-use-notify' are non-nil.
503 (if (or (string-match auto-revert-notify-exclude-dir-regexp
504 (expand-file-name default-directory))
505 (file-symlink-p (or buffer-file-name default-directory)))
507 ;; Fallback to file checks.
508 (setq-local auto-revert-use-notify nil)
510 (when (not auto-revert-notify-watch-descriptor)
511 (setq auto-revert-notify-watch-descriptor
512 (ignore-errors
513 (if buffer-file-name
514 (file-notify-add-watch
515 (expand-file-name buffer-file-name default-directory)
516 '(change attribute-change)
517 'auto-revert-notify-handler)
518 (file-notify-add-watch
519 (expand-file-name default-directory)
520 '(change)
521 'auto-revert-notify-handler))))
522 (if auto-revert-notify-watch-descriptor
523 (progn
524 (puthash
525 auto-revert-notify-watch-descriptor
526 (cons (current-buffer)
527 (gethash auto-revert-notify-watch-descriptor
528 auto-revert-notify-watch-descriptor-hash-list))
529 auto-revert-notify-watch-descriptor-hash-list)
530 (add-hook 'kill-buffer-hook
531 #'auto-revert-notify-rm-watch nil t))
532 ;; Fallback to file checks.
533 (setq-local auto-revert-use-notify nil)))))
535 ;; If we have file notifications, we want to update the auto-revert buffers
536 ;; immediately when a notification occurs. Since file updates can happen very
537 ;; often, we want to skip some revert operations so that we don't spend all our
538 ;; time reverting the buffer.
540 ;; We do this by reverting immediately in response to the first in a flurry of
541 ;; notifications. We suppress subsequent notifications until the next time
542 ;; `auto-revert-buffers' is called (this happens on a timer with a period set by
543 ;; `auto-revert-interval').
544 (defvar auto-revert-buffers-counter 1
545 "Incremented each time `auto-revert-buffers' is called")
546 (defvar-local auto-revert-buffers-counter-lockedout 0
547 "Buffer-local value to indicate whether we should immediately
548 update the buffer on a notification event or not. If
550 (= auto-revert-buffers-counter-lockedout
551 auto-revert-buffers-counter)
553 then the updates are locked out, and we wait until the next call
554 of `auto-revert-buffers' to revert the buffer. If no lockout is
555 present, then we revert immediately and set the lockout, so that
556 no more reverts are possible until the next call of
557 `auto-revert-buffers'")
559 (defun auto-revert-notify-handler (event)
560 "Handle an EVENT returned from file notification."
561 (with-demoted-errors
562 (let* ((descriptor (car event))
563 (action (nth 1 event))
564 (file (nth 2 event))
565 (file1 (nth 3 event)) ;; Target of `renamed'.
566 (buffers (gethash descriptor
567 auto-revert-notify-watch-descriptor-hash-list)))
568 ;; Check, that event is meant for us.
569 (cl-assert descriptor)
570 ;; Since we watch a directory, a file name must be returned.
571 (cl-assert (stringp file))
572 (when (eq action 'renamed) (cl-assert (stringp file1)))
573 ;; Loop over all buffers, in order to find the intended one.
574 (cl-dolist (buffer buffers)
575 (when (buffer-live-p buffer)
576 (with-current-buffer buffer
577 (when (or
578 ;; A buffer associated with a file.
579 (and (stringp buffer-file-name)
581 (and (memq action '(attribute-changed changed created))
582 (string-equal
583 (file-name-nondirectory file)
584 (file-name-nondirectory buffer-file-name)))
585 (and (eq action 'renamed)
586 (string-equal
587 (file-name-nondirectory file1)
588 (file-name-nondirectory buffer-file-name)))))
589 ;; A buffer w/o a file, like dired.
590 (and (null buffer-file-name)
591 (memq action '(created renamed deleted))))
592 ;; Mark buffer modified.
593 (setq auto-revert-notify-modified-p t)
595 ;; Revert the buffer now if we're not locked out.
596 (when (/= auto-revert-buffers-counter-lockedout
597 auto-revert-buffers-counter)
598 (auto-revert-handler)
599 (setq auto-revert-buffers-counter-lockedout
600 auto-revert-buffers-counter))
602 ;; No need to check other buffers.
603 (cl-return))))))))
605 (defun auto-revert-active-p ()
606 "Check if auto-revert is active (in current buffer or globally)."
607 (or auto-revert-mode
608 auto-revert-tail-mode
609 (and
610 global-auto-revert-mode
611 (not global-auto-revert-ignore-buffer)
612 (not (memq major-mode
613 global-auto-revert-ignore-modes)))))
615 (defun auto-revert-handler ()
616 "Revert current buffer, if appropriate.
617 This is an internal function used by Auto-Revert Mode."
618 (let* ((buffer (current-buffer)) size
619 ;; Tramp caches the file attributes. Setting
620 ;; `remote-file-name-inhibit-cache' forces Tramp to reread
621 ;; the values.
622 (remote-file-name-inhibit-cache t)
623 (revert
624 (if buffer-file-name
625 (and (or auto-revert-remote-files
626 (not (file-remote-p buffer-file-name)))
627 (or (not auto-revert-use-notify)
628 auto-revert-notify-modified-p)
629 (if auto-revert-tail-mode
630 (and (file-readable-p buffer-file-name)
631 (/= auto-revert-tail-pos
632 (setq size
633 (nth 7 (file-attributes
634 buffer-file-name)))))
635 (funcall (or buffer-stale-function
636 #'buffer-stale--default-function)
637 t)))
638 (and (or auto-revert-mode
639 global-auto-revert-non-file-buffers)
640 (funcall (or buffer-stale-function
641 #'buffer-stale--default-function)
642 t))))
643 eob eoblist)
644 (setq auto-revert-notify-modified-p nil)
645 (when revert
646 (when (and auto-revert-verbose
647 (not (eq revert 'fast)))
648 (message "Reverting buffer `%s'." (buffer-name)))
649 ;; If point (or a window point) is at the end of the buffer, we
650 ;; want to keep it at the end after reverting. This allows to
651 ;; tail a file.
652 (when buffer-file-name
653 (setq eob (eobp))
654 (walk-windows
655 (lambda (window)
656 (and (eq (window-buffer window) buffer)
657 (= (window-point window) (point-max))
658 (push window eoblist)))
659 'no-mini t))
660 (if auto-revert-tail-mode
661 (auto-revert-tail-handler size)
662 ;; Bind buffer-read-only in case user has done C-x C-q, so as
663 ;; not to forget that. This gives undesirable results when
664 ;; the file's mode changes, but that is less common.
665 (let ((buffer-read-only buffer-read-only))
666 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
667 (when buffer-file-name
668 (when eob (goto-char (point-max)))
669 (dolist (window eoblist)
670 (set-window-point window (point-max)))))
671 ;; `preserve-modes' avoids changing the (minor) modes. But we do
672 ;; want to reset the mode for VC, so we do it manually.
673 (when (or revert auto-revert-check-vc-info)
674 (vc-find-file-hook))))
676 (defun auto-revert-tail-handler (size)
677 (let ((modified (buffer-modified-p))
678 (inhibit-read-only t) ; Ignore.
679 (file buffer-file-name)
680 (buffer-file-name nil)) ; Ignore that file has changed.
681 (when (/= auto-revert-tail-pos size)
682 (run-hooks 'before-revert-hook)
683 (undo-boundary)
684 (save-restriction
685 (widen)
686 (save-excursion
687 (goto-char (point-max))
688 (insert-file-contents file nil
689 (and (< auto-revert-tail-pos size)
690 auto-revert-tail-pos)
691 size)))
692 (run-hooks 'after-revert-hook)
693 (undo-boundary)
694 (setq auto-revert-tail-pos size)
695 (restore-buffer-modified-p modified)))
696 (set-visited-file-modtime))
698 (defun auto-revert-buffers ()
699 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
701 Should `global-auto-revert-mode' be active all file buffers are checked.
703 Should `auto-revert-mode' be active in some buffers, those buffers
704 are checked.
706 Non-file buffers that have a custom `revert-buffer-function' and
707 `buffer-stale-function' are reverted either when Auto-Revert
708 Mode is active in that buffer, or when the variable
709 `global-auto-revert-non-file-buffers' is non-nil and Global
710 Auto-Revert Mode is active.
712 This function stops whenever there is user input. The buffers not
713 checked are stored in the variable `auto-revert-remaining-buffers'.
715 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
716 are checked first the next time this function is called.
718 This function is also responsible for removing buffers no longer in
719 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
720 the timer when no buffers need to be checked."
722 (setq auto-revert-buffers-counter
723 (1+ auto-revert-buffers-counter))
725 (save-match-data
726 (let ((bufs (if global-auto-revert-mode
727 (buffer-list)
728 auto-revert-buffer-list))
729 remaining new)
730 ;; Partition `bufs' into two halves depending on whether or not
731 ;; the buffers are in `auto-revert-remaining-buffers'. The two
732 ;; halves are then re-joined with the "remaining" buffers at the
733 ;; head of the list.
734 (dolist (buf auto-revert-remaining-buffers)
735 (if (memq buf bufs)
736 (push buf remaining)))
737 (dolist (buf bufs)
738 (if (not (memq buf remaining))
739 (push buf new)))
740 (setq bufs (nreverse (nconc new remaining)))
741 (while (and bufs
742 (not (and auto-revert-stop-on-user-input
743 (input-pending-p))))
744 (let ((buf (car bufs)))
745 (if (buffer-live-p buf)
746 (with-current-buffer buf
747 ;; Test if someone has turned off Auto-Revert Mode in a
748 ;; non-standard way, for example by changing major mode.
749 (if (and (not auto-revert-mode)
750 (not auto-revert-tail-mode)
751 (memq buf auto-revert-buffer-list))
752 (setq auto-revert-buffer-list
753 (delq buf auto-revert-buffer-list)))
754 (when (auto-revert-active-p)
755 ;; Enable file notification.
756 (when (and auto-revert-use-notify
757 (not auto-revert-notify-watch-descriptor))
758 (auto-revert-notify-add-watch))
759 (auto-revert-handler)))
760 ;; Remove dead buffer from `auto-revert-buffer-list'.
761 (setq auto-revert-buffer-list
762 (delq buf auto-revert-buffer-list))))
763 (setq bufs (cdr bufs)))
764 (setq auto-revert-remaining-buffers bufs)
765 ;; Check if we should cancel the timer.
766 (when (and (not global-auto-revert-mode)
767 (null auto-revert-buffer-list))
768 (cancel-timer auto-revert-timer)
769 (setq auto-revert-timer nil)))))
772 ;; The end:
773 (provide 'autorevert)
775 (run-hooks 'auto-revert-load-hook)
777 ;;; autorevert.el ends here