Add cl-struct specific optimizations to pcase.
[emacs.git] / lisp / autorevert.el
blob357916c6b4d7dcacb7431a618f1c3e713b96c6ec
1 ;;; autorevert.el --- revert buffers when files on disk change
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 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.")
266 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
268 (defcustom auto-revert-remote-files nil
269 "If non-nil remote files are also reverted."
270 :group 'auto-revert
271 :type 'boolean
272 :version "24.4")
274 (defcustom auto-revert-use-notify t
275 "If non-nil Auto Revert Mode uses file notification functions.
276 You should set this variable through Custom."
277 :group 'auto-revert
278 :type 'boolean
279 :set (lambda (variable value)
280 (set-default variable value)
281 (unless (symbol-value variable)
282 (dolist (buf (buffer-list))
283 (with-current-buffer buf
284 (when (symbol-value 'auto-revert-notify-watch-descriptor)
285 (auto-revert-notify-rm-watch))))))
286 :initialize 'custom-initialize-default
287 :version "24.4")
289 (defcustom auto-revert-notify-exclude-dir-regexp
290 (concat
291 ;; No mounted file systems.
292 "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
293 ;; No remote files.
294 (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
295 "Regular expression of directories to be excluded from file notifications."
296 :group 'auto-revert
297 :type 'regexp
298 :version "24.4")
300 ;; Internal variables:
302 (defvar auto-revert-buffer-list ()
303 "List of buffers in Auto-Revert Mode.
305 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
306 buffers to this list.
308 The timer function `auto-revert-buffers' is responsible for purging
309 the list of old buffers.")
311 (defvar auto-revert-remaining-buffers ()
312 "Buffers not checked when user input stopped execution.")
314 (defvar auto-revert-tail-pos 0
315 "Position of last known end of file.")
317 (add-hook 'find-file-hook
318 (lambda ()
319 (set (make-local-variable 'auto-revert-tail-pos)
320 (nth 7 (file-attributes buffer-file-name)))))
322 (defvar auto-revert-notify-watch-descriptor-hash-list
323 (make-hash-table :test 'equal)
324 "A hash table collecting all file watch descriptors.
325 Hash key is a watch descriptor, hash value is a list of buffers
326 which are related to files being watched and carrying the same
327 default directory.")
329 (defvar auto-revert-notify-watch-descriptor nil
330 "The file watch descriptor active for the current buffer.")
331 (make-variable-buffer-local 'auto-revert-notify-watch-descriptor)
332 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
334 (defvar auto-revert-notify-modified-p nil
335 "Non-nil when file has been modified on the file system.
336 This has been reported by a file notification event.")
337 (make-variable-buffer-local 'auto-revert-notify-modified-p)
339 ;; Functions:
341 ;;;###autoload
342 (define-minor-mode auto-revert-mode
343 "Toggle reverting buffer when the file changes (Auto Revert mode).
344 With a prefix argument ARG, enable Auto Revert mode if ARG is
345 positive, and disable it otherwise. If called from Lisp, enable
346 the mode if ARG is omitted or nil.
348 Auto Revert mode is a minor mode that affects only the current
349 buffer. When enabled, it reverts the buffer when the file on
350 disk changes.
352 Use `global-auto-revert-mode' to automatically revert all buffers.
353 Use `auto-revert-tail-mode' if you know that the file will only grow
354 without being changed in the part that is already in the buffer."
355 :group 'auto-revert :lighter auto-revert-mode-text
356 (if auto-revert-mode
357 (if (not (memq (current-buffer) auto-revert-buffer-list))
358 (push (current-buffer) auto-revert-buffer-list))
359 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
360 (setq auto-revert-buffer-list
361 (delq (current-buffer) auto-revert-buffer-list)))
362 (auto-revert-set-timer)
363 (when auto-revert-mode
364 (auto-revert-buffers)
365 (setq auto-revert-tail-mode nil)))
368 ;;;###autoload
369 (defun turn-on-auto-revert-mode ()
370 "Turn on Auto-Revert Mode.
372 This function is designed to be added to hooks, for example:
373 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
374 (auto-revert-mode 1))
377 ;;;###autoload
378 (define-minor-mode auto-revert-tail-mode
379 "Toggle reverting tail of buffer when the file grows.
380 With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
381 is positive, and disable it otherwise. If called from Lisp,
382 enable the mode if ARG is omitted or nil.
384 When Auto Revert Tail mode is enabled, the tail of the file is
385 constantly followed, as with the shell command `tail -f'. This
386 means that whenever the file grows on disk (presumably because
387 some background process is appending to it from time to time),
388 this is reflected in the current buffer.
390 You can edit the buffer and turn this mode off and on again as
391 you please. But make sure the background process has stopped
392 writing before you save the file!
394 Use `auto-revert-mode' for changes other than appends!"
395 :group 'find-file :lighter auto-revert-tail-mode-text
396 (when auto-revert-tail-mode
397 (unless buffer-file-name
398 (auto-revert-tail-mode 0)
399 (error "This buffer is not visiting a file"))
400 (if (and (buffer-modified-p)
401 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
402 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
403 (auto-revert-tail-mode 0)
404 ;; a-r-tail-pos stores the size of the file at the time of the
405 ;; last revert. After this package loads, it adds a
406 ;; find-file-hook to set this variable every time a file is
407 ;; loaded. If the package is loaded only _after_ visiting the
408 ;; file to be reverted, then we have no idea what the value of
409 ;; a-r-tail-pos should have been when the file was visited. If
410 ;; the file has changed on disk in the meantime, all we can do
411 ;; is offer to revert the whole thing. If you choose not to
412 ;; revert, then you might miss some output then happened
413 ;; between visiting the file and activating a-r-t-mode.
414 (and (zerop auto-revert-tail-pos)
415 (not (verify-visited-file-modtime (current-buffer)))
416 (y-or-n-p "File changed on disk, content may be missing. \
417 Perform a full revert? ")
418 ;; Use this (not just revert-buffer) for point-preservation.
419 (auto-revert-buffers))
420 ;; else we might reappend our own end when we save
421 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
422 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
423 (set (make-local-variable 'auto-revert-tail-pos)
424 (nth 7 (file-attributes buffer-file-name))))
425 ;; let auto-revert-mode set up the mechanism for us if it isn't already
426 (or auto-revert-mode
427 (let ((auto-revert-tail-mode t))
428 (auto-revert-mode 1)))
429 (setq auto-revert-mode nil))))
432 ;;;###autoload
433 (defun turn-on-auto-revert-tail-mode ()
434 "Turn on Auto-Revert Tail mode.
436 This function is designed to be added to hooks, for example:
437 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
438 (auto-revert-tail-mode 1))
441 ;;;###autoload
442 (define-minor-mode global-auto-revert-mode
443 "Toggle Global Auto Revert mode.
444 With a prefix argument ARG, enable Global Auto Revert mode if ARG
445 is positive, and disable it otherwise. If called from Lisp,
446 enable the mode if ARG is omitted or nil.
448 Global Auto Revert mode is a global minor mode that reverts any
449 buffer associated with a file when the file changes on disk. Use
450 `auto-revert-mode' to revert a particular buffer.
452 If `global-auto-revert-non-file-buffers' is non-nil, this mode
453 may also revert some non-file buffers, as described in the
454 documentation of that variable. It ignores buffers with modes
455 matching `global-auto-revert-ignore-modes', and buffers with a
456 non-nil vale of `global-auto-revert-ignore-buffer'.
458 This function calls the hook `global-auto-revert-mode-hook'.
459 It displays the text that `global-auto-revert-mode-text'
460 specifies in the mode line."
461 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
462 (auto-revert-set-timer)
463 (if global-auto-revert-mode
464 (auto-revert-buffers)
465 (dolist (buf (buffer-list))
466 (with-current-buffer buf
467 (when auto-revert-use-notify
468 (auto-revert-notify-rm-watch))))))
470 (defun auto-revert-set-timer ()
471 "Restart or cancel the timer used by Auto-Revert Mode.
472 If such a timer is active, cancel it. Start a new timer if
473 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
474 in some buffer. Restarting the timer ensures that Auto-Revert Mode
475 will use an up-to-date value of `auto-revert-interval'"
476 (interactive)
477 (if (timerp auto-revert-timer)
478 (cancel-timer auto-revert-timer))
479 (setq auto-revert-timer
480 (if (or global-auto-revert-mode auto-revert-buffer-list)
481 (run-with-timer auto-revert-interval
482 auto-revert-interval
483 'auto-revert-buffers))))
485 (defun auto-revert-notify-rm-watch ()
486 "Disable file notification for current buffer's associated file."
487 (when auto-revert-notify-watch-descriptor
488 (maphash
489 (lambda (key value)
490 (when (equal key auto-revert-notify-watch-descriptor)
491 (setq value (delete (current-buffer) value))
492 (if value
493 (puthash key value auto-revert-notify-watch-descriptor-hash-list)
494 (remhash key auto-revert-notify-watch-descriptor-hash-list)
495 (ignore-errors
496 (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
497 auto-revert-notify-watch-descriptor-hash-list)
498 (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch))
499 (setq auto-revert-notify-watch-descriptor nil
500 auto-revert-notify-modified-p nil))
502 (defun auto-revert-notify-add-watch ()
503 "Enable file notification for current buffer's associated file."
504 ;; We can assume that `buffer-file-name' and
505 ;; `auto-revert-use-notify' are non-nil.
506 (if (or (string-match auto-revert-notify-exclude-dir-regexp
507 (expand-file-name default-directory))
508 (file-symlink-p (or buffer-file-name default-directory)))
510 ;; Fallback to file checks.
511 (set (make-local-variable 'auto-revert-use-notify) nil)
513 (when (not auto-revert-notify-watch-descriptor)
514 (setq auto-revert-notify-watch-descriptor
515 (ignore-errors
516 (if buffer-file-name
517 (file-notify-add-watch
518 (expand-file-name buffer-file-name default-directory)
519 '(change attribute-change)
520 'auto-revert-notify-handler)
521 (file-notify-add-watch
522 (expand-file-name default-directory)
523 '(change)
524 'auto-revert-notify-handler))))
525 (if auto-revert-notify-watch-descriptor
526 (progn
527 (puthash
528 auto-revert-notify-watch-descriptor
529 (cons (current-buffer)
530 (gethash auto-revert-notify-watch-descriptor
531 auto-revert-notify-watch-descriptor-hash-list))
532 auto-revert-notify-watch-descriptor-hash-list)
533 (add-hook (make-local-variable 'kill-buffer-hook)
534 'auto-revert-notify-rm-watch))
535 ;; Fallback to file checks.
536 (set (make-local-variable 'auto-revert-use-notify) nil)))))
538 ;; If we have file notifications, we want to update the auto-revert buffers
539 ;; immediately when a notification occurs. Since file updates can happen very
540 ;; often, we want to skip some revert operations so that we don't spend all our
541 ;; time reverting the buffer.
543 ;; We do this by reverting immediately in response to the first in a flurry of
544 ;; notifications. We suppress subsequent notifications until the next time
545 ;; `auto-revert-buffers' is called (this happens on a timer with a period set by
546 ;; `auto-revert-interval').
547 (defvar auto-revert-buffers-counter 1
548 "Incremented each time `auto-revert-buffers' is called")
549 (defvar-local auto-revert-buffers-counter-lockedout 0
550 "Buffer-local value to indicate whether we should immediately
551 update the buffer on a notification event or not. If
553 (= auto-revert-buffers-counter-lockedout
554 auto-revert-buffers-counter)
556 then the updates are locked out, and we wait until the next call
557 of `auto-revert-buffers' to revert the buffer. If no lockout is
558 present, then we revert immediately and set the lockout, so that
559 no more reverts are possible until the next call of
560 `auto-revert-buffers'")
562 (defun auto-revert-notify-handler (event)
563 "Handle an EVENT returned from file notification."
564 (with-demoted-errors
565 (let* ((descriptor (car event))
566 (action (nth 1 event))
567 (file (nth 2 event))
568 (file1 (nth 3 event)) ;; Target of `renamed'.
569 (buffers (gethash descriptor
570 auto-revert-notify-watch-descriptor-hash-list)))
571 ;; Check, that event is meant for us.
572 (cl-assert descriptor)
573 ;; Since we watch a directory, a file name must be returned.
574 (cl-assert (stringp file))
575 (when (eq action 'renamed) (cl-assert (stringp file1)))
576 ;; Loop over all buffers, in order to find the intended one.
577 (cl-dolist (buffer buffers)
578 (when (buffer-live-p buffer)
579 (with-current-buffer buffer
580 (when (or
581 ;; A buffer associated with a file.
582 (and (stringp buffer-file-name)
584 (and (memq action '(attribute-changed changed created))
585 (string-equal
586 (file-name-nondirectory file)
587 (file-name-nondirectory buffer-file-name)))
588 (and (eq action 'renamed)
589 (string-equal
590 (file-name-nondirectory file1)
591 (file-name-nondirectory buffer-file-name)))))
592 ;; A buffer w/o a file, like dired.
593 (and (null buffer-file-name)
594 (memq action '(created renamed deleted))))
595 ;; Mark buffer modified.
596 (setq auto-revert-notify-modified-p t)
598 ;; Revert the buffer now if we're not locked out.
599 (when (/= auto-revert-buffers-counter-lockedout
600 auto-revert-buffers-counter)
601 (auto-revert-handler)
602 (setq auto-revert-buffers-counter-lockedout
603 auto-revert-buffers-counter))
605 ;; No need to check other buffers.
606 (cl-return))))))))
608 (defun auto-revert-active-p ()
609 "Check if auto-revert is active (in current buffer or globally)."
610 (or auto-revert-mode
611 auto-revert-tail-mode
612 (and
613 global-auto-revert-mode
614 (not global-auto-revert-ignore-buffer)
615 (not (memq major-mode
616 global-auto-revert-ignore-modes)))))
618 (defun auto-revert-handler ()
619 "Revert current buffer, if appropriate.
620 This is an internal function used by Auto-Revert Mode."
621 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
622 (let* ((buffer (current-buffer)) size
623 ;; Tramp caches the file attributes. Setting
624 ;; `remote-file-name-inhibit-cache' forces Tramp to reread
625 ;; the values.
626 (remote-file-name-inhibit-cache t)
627 (revert
628 (if buffer-file-name
629 (and (or auto-revert-remote-files
630 (not (file-remote-p buffer-file-name)))
631 (or (not auto-revert-use-notify)
632 auto-revert-notify-modified-p)
633 (if auto-revert-tail-mode
634 (and (file-readable-p buffer-file-name)
635 (/= auto-revert-tail-pos
636 (setq size
637 (nth 7 (file-attributes
638 buffer-file-name)))))
639 (funcall (or buffer-stale-function
640 #'buffer-stale--default-function)
641 t)))
642 (and (or auto-revert-mode
643 global-auto-revert-non-file-buffers)
644 (funcall (or buffer-stale-function
645 #'buffer-stale--default-function)
646 t))))
647 eob eoblist)
648 (setq auto-revert-notify-modified-p nil)
649 (when revert
650 (when (and auto-revert-verbose
651 (not (eq revert 'fast)))
652 (message "Reverting buffer `%s'." (buffer-name)))
653 ;; If point (or a window point) is at the end of the buffer,
654 ;; we want to keep it at the end after reverting. This allows
655 ;; to tail a file.
656 (when buffer-file-name
657 (setq eob (eobp))
658 (walk-windows
659 (lambda (window)
660 (and (eq (window-buffer window) buffer)
661 (= (window-point window) (point-max))
662 (push window eoblist)))
663 'no-mini t))
664 (if auto-revert-tail-mode
665 (auto-revert-tail-handler size)
666 ;; Bind buffer-read-only in case user has done C-x C-q,
667 ;; so as not to forget that. This gives undesirable results
668 ;; when the file's mode changes, but that is less common.
669 (let ((buffer-read-only buffer-read-only))
670 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
671 (when buffer-file-name
672 (when eob (goto-char (point-max)))
673 (dolist (window eoblist)
674 (set-window-point window (point-max)))))
675 ;; `preserve-modes' avoids changing the (minor) modes. But we
676 ;; do want to reset the mode for VC, so we do it manually.
677 (when (or revert auto-revert-check-vc-info)
678 (vc-find-file-hook)))))
680 (defun auto-revert-tail-handler (size)
681 (let ((modified (buffer-modified-p))
682 (inhibit-read-only t) ; Ignore.
683 (file buffer-file-name)
684 (buffer-file-name nil)) ; Ignore that file has changed.
685 (when (/= auto-revert-tail-pos size)
686 (run-hooks 'before-revert-hook)
687 (undo-boundary)
688 (save-restriction
689 (widen)
690 (save-excursion
691 (goto-char (point-max))
692 (insert-file-contents file nil
693 (and (< auto-revert-tail-pos size)
694 auto-revert-tail-pos)
695 size)))
696 (run-hooks 'after-revert-hook)
697 (undo-boundary)
698 (setq auto-revert-tail-pos size)
699 (restore-buffer-modified-p modified)))
700 (set-visited-file-modtime))
702 (defun auto-revert-buffers ()
703 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
705 Should `global-auto-revert-mode' be active all file buffers are checked.
707 Should `auto-revert-mode' be active in some buffers, those buffers
708 are checked.
710 Non-file buffers that have a custom `revert-buffer-function' and
711 `buffer-stale-function' are reverted either when Auto-Revert
712 Mode is active in that buffer, or when the variable
713 `global-auto-revert-non-file-buffers' is non-nil and Global
714 Auto-Revert Mode is active.
716 This function stops whenever there is user input. The buffers not
717 checked are stored in the variable `auto-revert-remaining-buffers'.
719 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
720 are checked first the next time this function is called.
722 This function is also responsible for removing buffers no longer in
723 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
724 the timer when no buffers need to be checked."
726 (setq auto-revert-buffers-counter
727 (1+ auto-revert-buffers-counter))
729 (save-match-data
730 (let ((bufs (if global-auto-revert-mode
731 (buffer-list)
732 auto-revert-buffer-list))
733 remaining new)
734 ;; Partition `bufs' into two halves depending on whether or not
735 ;; the buffers are in `auto-revert-remaining-buffers'. The two
736 ;; halves are then re-joined with the "remaining" buffers at the
737 ;; head of the list.
738 (dolist (buf auto-revert-remaining-buffers)
739 (if (memq buf bufs)
740 (push buf remaining)))
741 (dolist (buf bufs)
742 (if (not (memq buf remaining))
743 (push buf new)))
744 (setq bufs (nreverse (nconc new remaining)))
745 (while (and bufs
746 (not (and auto-revert-stop-on-user-input
747 (input-pending-p))))
748 (let ((buf (car bufs)))
749 (if (buffer-live-p buf)
750 (with-current-buffer buf
751 ;; Test if someone has turned off Auto-Revert Mode in a
752 ;; non-standard way, for example by changing major mode.
753 (if (and (not auto-revert-mode)
754 (not auto-revert-tail-mode)
755 (memq buf auto-revert-buffer-list))
756 (setq auto-revert-buffer-list
757 (delq buf auto-revert-buffer-list)))
758 (when (auto-revert-active-p)
759 ;; Enable file notification.
760 (when (and auto-revert-use-notify
761 (not auto-revert-notify-watch-descriptor))
762 (auto-revert-notify-add-watch))
763 (auto-revert-handler)))
764 ;; Remove dead buffer from `auto-revert-buffer-list'.
765 (setq auto-revert-buffer-list
766 (delq buf auto-revert-buffer-list))))
767 (setq bufs (cdr bufs)))
768 (setq auto-revert-remaining-buffers bufs)
769 ;; Check if we should cancel the timer.
770 (when (and (not global-auto-revert-mode)
771 (null auto-revert-buffer-list))
772 (cancel-timer auto-revert-timer)
773 (setq auto-revert-timer nil)))))
776 ;; The end:
777 (provide 'autorevert)
779 (run-hooks 'auto-revert-load-hook)
781 ;;; autorevert.el ends here