Fix Rmail editing with reapplying encoding to message body
[emacs.git] / lisp / autorevert.el
bloba15386aa1af82035e3ec0cf7d92c6ea2228c07e4
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
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 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.)
85 ;; Usage:
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)
106 ;;; Code:
108 ;; Dependencies:
110 (eval-when-compile (require 'cl-lib))
111 (require 'timer)
112 (require 'filenotify)
114 ;; Custom Group:
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."
123 :group 'files
124 :group 'convenience)
127 ;; Variables:
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."
156 :group 'auto-revert
157 :type 'number
158 :set (lambda (variable value)
159 (set-default variable value)
160 (and (boundp 'auto-revert-timer)
161 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."
172 :group 'auto-revert
173 :type 'boolean)
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."
178 :group 'auto-revert
179 :type 'boolean)
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-...'
186 :group 'auto-revert
187 :type 'string)
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.)"
193 :group 'auto-revert
194 :type 'string
195 :version "22.1")
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-...'
200 :group 'auto-revert
201 :type 'hook)
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."
209 :group 'auto-revert
210 :type 'string)
212 (defcustom global-auto-revert-mode-hook nil
213 "Hook called when Global Auto-Revert Mode is activated."
214 :group 'auto-revert
215 :type 'hook)
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
229 them manually.
231 Use this option with care since it could lead to excessive auto-reverts.
232 For more information, see Info node `(emacs)Autorevert'."
233 :group 'auto-revert
234 :type 'boolean
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."
239 :group 'auto-revert
240 :type '(repeat sexp))
242 (defcustom auto-revert-load-hook nil
243 "Functions to run when Auto-Revert Mode is first loaded."
244 :tag "Load Hook"
245 :group 'auto-revert
246 :type 'hook)
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."
265 :group 'auto-revert
266 :type 'boolean
267 :version "22.1")
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."
275 :group 'auto-revert
276 :type 'boolean
277 :version "24.4")
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."
282 :group 'auto-revert
283 :type 'boolean
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
292 :version "24.4")
294 (defcustom auto-revert-notify-exclude-dir-regexp
295 (concat
296 ;; No mounted file systems.
297 "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
298 ;; No remote files.
299 (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
300 "Regular expression of directories to be excluded from file notifications."
301 :group 'auto-revert
302 :type 'regexp
303 :version "24.4")
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 (defun auto-revert-find-file-function ()
323 (setq-local auto-revert-tail-pos
324 (nth 7 (file-attributes buffer-file-name))))
326 (add-hook 'find-file-hook
327 #'auto-revert-find-file-function)
329 (defvar auto-revert-notify-watch-descriptor-hash-list
330 (make-hash-table :test 'equal)
331 "A hash table collecting all file watch descriptors.
332 Hash key is a watch descriptor, hash value is a list of buffers
333 which are related to files being watched and carrying the same
334 default directory.")
336 (defvar-local auto-revert-notify-watch-descriptor nil
337 "The file watch descriptor active for the current buffer.")
338 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
340 (defvar-local auto-revert-notify-modified-p nil
341 "Non-nil when file has been modified on the file system.
342 This has been reported by a file notification event.")
344 ;; Functions:
346 (defun auto-revert-remove-current-buffer ()
347 "Remove dead buffer from `auto-revert-buffer-list'."
348 (setq auto-revert-buffer-list
349 (delq (current-buffer) auto-revert-buffer-list)))
351 ;;;###autoload
352 (define-minor-mode auto-revert-mode
353 "Toggle reverting buffer when the file changes (Auto-Revert Mode).
354 With a prefix argument ARG, enable Auto-Revert Mode if ARG is
355 positive, and disable it otherwise. If called from Lisp, enable
356 the mode if ARG is omitted or nil.
358 Auto-Revert Mode is a minor mode that affects only the current
359 buffer. When enabled, it reverts the buffer when the file on
360 disk changes.
362 When a buffer is reverted, a message is generated. This can be
363 suppressed by setting `auto-revert-verbose' to nil.
365 Use `global-auto-revert-mode' to automatically revert all buffers.
366 Use `auto-revert-tail-mode' if you know that the file will only grow
367 without being changed in the part that is already in the buffer."
368 :group 'auto-revert :lighter auto-revert-mode-text
369 (if auto-revert-mode
370 (when (not (memq (current-buffer) auto-revert-buffer-list))
371 (push (current-buffer) auto-revert-buffer-list)
372 (add-hook
373 'kill-buffer-hook
374 #'auto-revert-remove-current-buffer
375 nil t))
376 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
377 (auto-revert-remove-current-buffer))
378 (auto-revert-set-timer)
379 (when auto-revert-mode
380 (auto-revert-buffers)
381 (setq auto-revert-tail-mode nil)))
384 ;;;###autoload
385 (defun turn-on-auto-revert-mode ()
386 "Turn on Auto-Revert Mode.
388 This function is designed to be added to hooks, for example:
389 (add-hook \\='c-mode-hook #\\='turn-on-auto-revert-mode)"
390 (auto-revert-mode 1))
393 ;;;###autoload
394 (define-minor-mode auto-revert-tail-mode
395 "Toggle reverting tail of buffer when the file grows.
396 With a prefix argument ARG, enable Auto-Revert Tail Mode if ARG
397 is positive, and disable it otherwise. If called from Lisp,
398 enable the mode if ARG is omitted or nil.
400 When Auto-Revert Tail Mode is enabled, the tail of the file is
401 constantly followed, as with the shell command `tail -f'. This
402 means that whenever the file grows on disk (presumably because
403 some background process is appending to it from time to time),
404 this is reflected in the current buffer.
406 You can edit the buffer and turn this mode off and on again as
407 you please. But make sure the background process has stopped
408 writing before you save the file!
410 When a buffer is reverted, a message is generated. This can be
411 suppressed by setting `auto-revert-verbose' to nil.
413 Use `auto-revert-mode' for changes other than appends!"
414 :group 'find-file :lighter auto-revert-tail-mode-text
415 (when auto-revert-tail-mode
416 (unless buffer-file-name
417 (auto-revert-tail-mode 0)
418 (error "This buffer is not visiting a file"))
419 (if (and (buffer-modified-p)
420 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
421 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
422 (auto-revert-tail-mode 0)
423 ;; a-r-tail-pos stores the size of the file at the time of the
424 ;; last revert. After this package loads, it adds a
425 ;; find-file-hook to set this variable every time a file is
426 ;; loaded. If the package is loaded only _after_ visiting the
427 ;; file to be reverted, then we have no idea what the value of
428 ;; a-r-tail-pos should have been when the file was visited. If
429 ;; the file has changed on disk in the meantime, all we can do
430 ;; is offer to revert the whole thing. If you choose not to
431 ;; revert, then you might miss some output then happened
432 ;; between visiting the file and activating a-r-t-mode.
433 (and (zerop auto-revert-tail-pos)
434 (not (verify-visited-file-modtime (current-buffer)))
435 (y-or-n-p "File changed on disk, content may be missing. \
436 Perform a full revert? ")
437 ;; Use this (not just revert-buffer) for point-preservation.
438 (auto-revert-buffers))
439 ;; else we might reappend our own end when we save
440 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
441 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
442 (setq-local auto-revert-tail-pos
443 (nth 7 (file-attributes buffer-file-name))))
444 ;; let auto-revert-mode set up the mechanism for us if it isn't already
445 (or auto-revert-mode
446 (let ((auto-revert-tail-mode t))
447 (auto-revert-mode 1)))
448 (setq auto-revert-mode nil))))
451 ;;;###autoload
452 (defun turn-on-auto-revert-tail-mode ()
453 "Turn on Auto-Revert Tail Mode.
455 This function is designed to be added to hooks, for example:
456 (add-hook \\='my-logfile-mode-hook #\\='turn-on-auto-revert-tail-mode)"
457 (auto-revert-tail-mode 1))
460 ;;;###autoload
461 (define-minor-mode global-auto-revert-mode
462 "Toggle Global Auto-Revert Mode.
463 With a prefix argument ARG, enable Global Auto-Revert Mode if ARG
464 is positive, and disable it otherwise. If called from Lisp,
465 enable the mode if ARG is omitted or nil.
467 Global Auto-Revert Mode is a global minor mode that reverts any
468 buffer associated with a file when the file changes on disk. Use
469 `auto-revert-mode' to revert a particular buffer.
471 If `global-auto-revert-non-file-buffers' is non-nil, this mode
472 may also revert some non-file buffers, as described in the
473 documentation of that variable. It ignores buffers with modes
474 matching `global-auto-revert-ignore-modes', and buffers with a
475 non-nil vale of `global-auto-revert-ignore-buffer'.
477 When a buffer is reverted, a message is generated. This can be
478 suppressed by setting `auto-revert-verbose' to nil.
480 This function calls the hook `global-auto-revert-mode-hook'.
481 It displays the text that `global-auto-revert-mode-text'
482 specifies in the mode line."
483 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
484 (auto-revert-set-timer)
485 (if global-auto-revert-mode
486 (auto-revert-buffers)
487 (dolist (buf (buffer-list))
488 (with-current-buffer buf
489 (when auto-revert-use-notify
490 (auto-revert-notify-rm-watch))))))
492 (defun auto-revert-set-timer ()
493 "Restart or cancel the timer used by Auto-Revert Mode.
494 If such a timer is active, cancel it. Start a new timer if
495 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
496 in some buffer. Restarting the timer ensures that Auto-Revert Mode
497 will use an up-to-date value of `auto-revert-interval'"
498 (interactive)
499 (if (timerp auto-revert-timer)
500 (cancel-timer auto-revert-timer))
501 (setq auto-revert-timer
502 (if (or global-auto-revert-mode auto-revert-buffer-list)
503 (run-with-timer auto-revert-interval
504 auto-revert-interval
505 'auto-revert-buffers))))
507 (defun auto-revert-notify-rm-watch ()
508 "Disable file notification for current buffer's associated file."
509 (when auto-revert-notify-watch-descriptor
510 (maphash
511 (lambda (key value)
512 (when (equal key auto-revert-notify-watch-descriptor)
513 (setq value (delete (current-buffer) value))
514 (if value
515 (puthash key value auto-revert-notify-watch-descriptor-hash-list)
516 (remhash key auto-revert-notify-watch-descriptor-hash-list)
517 (ignore-errors
518 (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
519 auto-revert-notify-watch-descriptor-hash-list)
520 (remove-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch))
521 (setq auto-revert-notify-watch-descriptor nil
522 auto-revert-notify-modified-p nil))
524 (defun auto-revert-notify-add-watch ()
525 "Enable file notification for current buffer's associated file."
526 ;; We can assume that `buffer-file-name' and
527 ;; `auto-revert-use-notify' are non-nil.
528 (if (or (string-match auto-revert-notify-exclude-dir-regexp
529 (expand-file-name default-directory))
530 (file-symlink-p (or buffer-file-name default-directory)))
532 ;; Fallback to file checks.
533 (setq-local auto-revert-use-notify nil)
535 (when (not auto-revert-notify-watch-descriptor)
536 (setq auto-revert-notify-watch-descriptor
537 (ignore-errors
538 (if buffer-file-name
539 (file-notify-add-watch
540 (expand-file-name buffer-file-name default-directory)
541 '(change attribute-change)
542 'auto-revert-notify-handler)
543 (file-notify-add-watch
544 (expand-file-name default-directory)
545 '(change)
546 'auto-revert-notify-handler))))
547 (if auto-revert-notify-watch-descriptor
548 (progn
549 (puthash
550 auto-revert-notify-watch-descriptor
551 (cons (current-buffer)
552 (gethash auto-revert-notify-watch-descriptor
553 auto-revert-notify-watch-descriptor-hash-list))
554 auto-revert-notify-watch-descriptor-hash-list)
555 (add-hook 'kill-buffer-hook
556 #'auto-revert-notify-rm-watch nil t))
557 ;; Fallback to file checks.
558 (setq-local auto-revert-use-notify nil)))))
560 ;; If we have file notifications, we want to update the auto-revert buffers
561 ;; immediately when a notification occurs. Since file updates can happen very
562 ;; often, we want to skip some revert operations so that we don't spend all our
563 ;; time reverting the buffer.
565 ;; We do this by reverting immediately in response to the first in a flurry of
566 ;; notifications. We suppress subsequent notifications until the next time
567 ;; `auto-revert-buffers' is called (this happens on a timer with a period set by
568 ;; `auto-revert-interval').
569 (defvar auto-revert-buffers-counter 1
570 "Incremented each time `auto-revert-buffers' is called")
571 (defvar-local auto-revert-buffers-counter-lockedout 0
572 "Buffer-local value to indicate whether we should immediately
573 update the buffer on a notification event or not. If
575 (= auto-revert-buffers-counter-lockedout
576 auto-revert-buffers-counter)
578 then the updates are locked out, and we wait until the next call
579 of `auto-revert-buffers' to revert the buffer. If no lockout is
580 present, then we revert immediately and set the lockout, so that
581 no more reverts are possible until the next call of
582 `auto-revert-buffers'")
584 (defun auto-revert-notify-handler (event)
585 "Handle an EVENT returned from file notification."
586 (with-demoted-errors
587 (let* ((descriptor (car event))
588 (action (nth 1 event))
589 (file (nth 2 event))
590 (file1 (nth 3 event)) ;; Target of `renamed'.
591 (buffers (gethash descriptor
592 auto-revert-notify-watch-descriptor-hash-list)))
593 ;; Check, that event is meant for us.
594 (cl-assert descriptor)
595 ;; Since we watch a directory, a file name must be returned.
596 (cl-assert (stringp file))
597 (when (eq action 'renamed) (cl-assert (stringp file1)))
599 (if (eq action 'stopped)
600 ;; File notification has stopped. Continue with polling.
601 (cl-dolist (buffer
602 (if global-auto-revert-mode
603 (buffer-list) auto-revert-buffer-list))
604 (with-current-buffer buffer
605 (when (and (equal descriptor auto-revert-notify-watch-descriptor)
607 ;; A buffer associated with a file.
608 (and (stringp buffer-file-name)
609 (string-equal
610 (file-name-nondirectory file)
611 (file-name-nondirectory buffer-file-name)))
612 ;; A buffer w/o a file, like dired.
613 (null buffer-file-name)))
614 (auto-revert-notify-rm-watch)
615 (setq-local auto-revert-use-notify nil))))
617 ;; Loop over all buffers, in order to find the intended one.
618 (cl-dolist (buffer buffers)
619 (when (buffer-live-p buffer)
620 (with-current-buffer buffer
621 (when (or
622 ;; A buffer associated with a file.
623 (and (stringp buffer-file-name)
625 (and (memq
626 action '(attribute-changed changed created))
627 (string-equal
628 (file-name-nondirectory file)
629 (file-name-nondirectory buffer-file-name)))
630 (and (eq action 'renamed)
631 (string-equal
632 (file-name-nondirectory file1)
633 (file-name-nondirectory buffer-file-name)))))
634 ;; A buffer w/o a file, like dired.
635 (and (null buffer-file-name)
636 (memq action '(created renamed deleted))))
637 ;; Mark buffer modified.
638 (setq auto-revert-notify-modified-p t)
640 ;; Revert the buffer now if we're not locked out.
641 (when (/= auto-revert-buffers-counter-lockedout
642 auto-revert-buffers-counter)
643 (auto-revert-handler)
644 (setq auto-revert-buffers-counter-lockedout
645 auto-revert-buffers-counter))
647 ;; No need to check other buffers.
648 (cl-return)))))))))
650 (defun auto-revert-active-p ()
651 "Check if auto-revert is active (in current buffer or globally)."
652 (or auto-revert-mode
653 auto-revert-tail-mode
654 (and
655 global-auto-revert-mode
656 (not global-auto-revert-ignore-buffer)
657 (not (memq major-mode
658 global-auto-revert-ignore-modes)))))
660 (defun auto-revert-handler ()
661 "Revert current buffer, if appropriate.
662 This is an internal function used by Auto-Revert Mode."
663 (let* ((buffer (current-buffer)) size
664 ;; Tramp caches the file attributes. Setting
665 ;; `remote-file-name-inhibit-cache' forces Tramp to reread
666 ;; the values.
667 (remote-file-name-inhibit-cache t)
668 (revert
669 (if buffer-file-name
670 (and (or auto-revert-remote-files
671 (not (file-remote-p buffer-file-name)))
672 (or (not auto-revert-use-notify)
673 auto-revert-notify-modified-p)
674 (if auto-revert-tail-mode
675 (and (file-readable-p buffer-file-name)
676 (/= auto-revert-tail-pos
677 (setq size
678 (nth 7 (file-attributes
679 buffer-file-name)))))
680 (funcall (or buffer-stale-function
681 #'buffer-stale--default-function)
682 t)))
683 (and (or auto-revert-mode
684 global-auto-revert-non-file-buffers)
685 (funcall (or buffer-stale-function
686 #'buffer-stale--default-function)
687 t))))
688 eob eoblist)
689 (setq auto-revert-notify-modified-p nil)
690 (when revert
691 (when (and auto-revert-verbose
692 (not (eq revert 'fast)))
693 (message "Reverting buffer `%s'." (buffer-name)))
694 ;; If point (or a window point) is at the end of the buffer, we
695 ;; want to keep it at the end after reverting. This allows one
696 ;; to tail a file.
697 (when buffer-file-name
698 (setq eob (eobp))
699 (walk-windows
700 (lambda (window)
701 (and (eq (window-buffer window) buffer)
702 (= (window-point window) (point-max))
703 (push window eoblist)))
704 'no-mini t))
705 (if auto-revert-tail-mode
706 (auto-revert-tail-handler size)
707 ;; Bind buffer-read-only in case user has done C-x C-q, so as
708 ;; not to forget that. This gives undesirable results when
709 ;; the file's mode changes, but that is less common.
710 (let ((buffer-read-only buffer-read-only))
711 ;; Bug#23276: When the file has been deleted, keep the
712 ;; buffer unchanged.
713 (ignore-errors
714 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes))))
715 (when buffer-file-name
716 (when eob (goto-char (point-max)))
717 (dolist (window eoblist)
718 (set-window-point window (point-max)))))
719 ;; `preserve-modes' avoids changing the (minor) modes. But we do
720 ;; want to reset the mode for VC, so we do it manually.
721 (when (or revert auto-revert-check-vc-info)
722 (vc-refresh-state))))
724 (defun auto-revert-tail-handler (size)
725 (let ((modified (buffer-modified-p))
726 (inhibit-read-only t) ; Ignore.
727 (file buffer-file-name)
728 (buffer-file-name nil)) ; Ignore that file has changed.
729 (when (/= auto-revert-tail-pos size)
730 (run-hooks 'before-revert-hook)
731 (undo-boundary)
732 (save-restriction
733 (widen)
734 (save-excursion
735 (goto-char (point-max))
736 (insert-file-contents file nil
737 (and (< auto-revert-tail-pos size)
738 auto-revert-tail-pos)
739 size)))
740 (run-hooks 'after-revert-hook)
741 (undo-boundary)
742 (setq auto-revert-tail-pos size)
743 (restore-buffer-modified-p modified)))
744 (set-visited-file-modtime))
746 (defun auto-revert-buffers ()
747 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
749 Should `global-auto-revert-mode' be active all file buffers are checked.
751 Should `auto-revert-mode' be active in some buffers, those buffers
752 are checked.
754 Non-file buffers that have a custom `revert-buffer-function' and
755 `buffer-stale-function' are reverted either when Auto-Revert
756 Mode is active in that buffer, or when the variable
757 `global-auto-revert-non-file-buffers' is non-nil and Global
758 Auto-Revert Mode is active.
760 This function stops whenever there is user input. The buffers not
761 checked are stored in the variable `auto-revert-remaining-buffers'.
763 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
764 are checked first the next time this function is called.
766 This function is also responsible for removing buffers no longer in
767 Auto-Revert Mode from `auto-revert-buffer-list', and for canceling
768 the timer when no buffers need to be checked."
770 (setq auto-revert-buffers-counter
771 (1+ auto-revert-buffers-counter))
773 (save-match-data
774 (let ((bufs (if global-auto-revert-mode
775 (buffer-list)
776 auto-revert-buffer-list))
777 remaining new)
778 ;; Partition `bufs' into two halves depending on whether or not
779 ;; the buffers are in `auto-revert-remaining-buffers'. The two
780 ;; halves are then re-joined with the "remaining" buffers at the
781 ;; head of the list.
782 (dolist (buf auto-revert-remaining-buffers)
783 (if (memq buf bufs)
784 (push buf remaining)))
785 (dolist (buf bufs)
786 (if (not (memq buf remaining))
787 (push buf new)))
788 (setq bufs (nreverse (nconc new remaining)))
789 (while (and bufs
790 (not (and auto-revert-stop-on-user-input
791 (input-pending-p))))
792 (let ((buf (car bufs)))
793 (with-current-buffer buf
794 (if (buffer-live-p buf)
795 (progn
796 ;; Test if someone has turned off Auto-Revert Mode
797 ;; in a non-standard way, for example by changing
798 ;; major mode.
799 (if (and (not auto-revert-mode)
800 (not auto-revert-tail-mode)
801 (memq buf auto-revert-buffer-list))
802 (auto-revert-remove-current-buffer))
803 (when (auto-revert-active-p)
804 ;; Enable file notification.
805 (when (and auto-revert-use-notify
806 (not auto-revert-notify-watch-descriptor))
807 (auto-revert-notify-add-watch))
808 (auto-revert-handler)))
809 ;; Remove dead buffer from `auto-revert-buffer-list'.
810 (auto-revert-remove-current-buffer))))
811 (setq bufs (cdr bufs)))
812 (setq auto-revert-remaining-buffers bufs)
813 ;; Check if we should cancel the timer.
814 (when (and (not global-auto-revert-mode)
815 (null auto-revert-buffer-list))
816 (cancel-timer auto-revert-timer)
817 (setq auto-revert-timer nil)))))
820 ;; The end:
821 (provide 'autorevert)
823 (run-hooks 'auto-revert-load-hook)
825 ;;; autorevert.el ends here