Merge from trunk.
[bpt/emacs.git] / lisp / autorevert.el
1 ;;; autorevert.el --- revert buffers when files on disk change
2
3 ;; Copyright (C) 1997-1999, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Keywords: convenience
7 ;; Created: 1997-06-01
8 ;; Date: 1999-11-30
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Introduction:
28 ;;
29 ;; Whenever a file that Emacs is editing has been changed by another
30 ;; program the user normally has to execute the command `revert-buffer'
31 ;; to load the new content of the file into Emacs.
32 ;;
33 ;; This package contains two minor modes: Global Auto-Revert Mode and
34 ;; Auto-Revert Mode. Both modes automatically revert buffers
35 ;; whenever the corresponding files have been changed on disk and the
36 ;; buffer contains no unsaved changes.
37 ;;
38 ;; Auto-Revert Mode can be activated for individual buffers. Global
39 ;; Auto-Revert Mode applies to all file buffers. (If the user option
40 ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies
41 ;; to some non-file buffers. This option is disabled by default.)
42 ;;
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.
47 ;;
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).
53 ;;
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'.
60 ;;
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.
71 ;;
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.)
78
79 ;; Usage:
80 ;;
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
84 ;;
85 ;; To activate Global Auto-Revert Mode, press:
86 ;; M-x global-auto-revert-mode RET
87 ;;
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)
92 ;;
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:
97 ;;
98 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
99
100 ;;; Code:
101
102 ;; Dependencies:
103
104 (eval-when-compile (require 'cl-lib))
105 (require 'timer)
106
107 ;; Custom Group:
108 ;;
109 ;; The two modes will be placed next to Auto Save Mode under the
110 ;; Files group under Emacs.
111
112 (defgroup auto-revert nil
113 "Revert individual buffers when files on disk change.
114 Auto-Revert mode enables auto-revert in individual buffers.
115 Global Auto-Revert mode does so in all buffers."
116 :group 'files
117 :group 'convenience)
118
119
120 ;; Variables:
121
122 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
123 ;;; What's this?: ;;;###autoload
124 (defvar auto-revert-mode nil
125 "Non-nil when Auto-Revert Mode is active.
126 Never set this variable directly, use the command `auto-revert-mode' instead.")
127 (put 'auto-revert-mode 'permanent-local t)
128
129 (defvar auto-revert-tail-mode nil
130 "Non-nil when Auto-Revert Tail Mode is active.
131 Never set this variable directly, use the command
132 `auto-revert-tail-mode' instead.")
133 (put 'auto-revert-tail-mode 'permanent-local t)
134
135 (defvar auto-revert-timer nil
136 "Timer used by Auto-Revert Mode.")
137
138 (defcustom auto-revert-interval 5
139 "Time, in seconds, between Auto-Revert Mode file checks.
140 The value may be an integer or floating point number.
141
142 If a timer is already active, there are two ways to make sure
143 that the new value will take effect immediately. You can set
144 this variable through Custom or you can call the command
145 `auto-revert-set-timer' after setting the variable. Otherwise,
146 the new value will take effect the first time Auto Revert Mode
147 calls `auto-revert-set-timer' for internal reasons or in your
148 next editing session."
149 :group 'auto-revert
150 :type 'number
151 :set (lambda (variable value)
152 (set-default variable value)
153 (and (boundp 'auto-revert-timer)
154 auto-revert-timer
155 (auto-revert-set-timer))))
156
157 (defcustom auto-revert-stop-on-user-input t
158 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
159 With this setting, Auto-Revert Mode checks for user input after
160 handling each buffer and does not process any further buffers
161 \(until the next run of the timer) if user input is available.
162 When nil, Auto-Revert Mode checks files and reverts buffers,
163 with quitting disabled, without paying attention to user input.
164 Thus, with this setting, Emacs might be non-responsive at times."
165 :group 'auto-revert
166 :type 'boolean)
167
168 (defcustom auto-revert-verbose t
169 "When nil, Auto-Revert Mode does not generate any messages.
170 When non-nil, a message is generated whenever a file is reverted."
171 :group 'auto-revert
172 :type 'boolean)
173
174 (defcustom auto-revert-mode-text " ARev"
175 "String to display in the mode line when Auto-Revert Mode is active.
176
177 \(When the string is not empty, make sure that it has a leading space.)"
178 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
179 :group 'auto-revert
180 :type 'string)
181
182 (defcustom auto-revert-tail-mode-text " Tail"
183 "String to display in the mode line when Auto-Revert Tail Mode is active.
184
185 \(When the string is not empty, make sure that it has a leading space.)"
186 :group 'auto-revert
187 :type 'string
188 :version "22.1")
189
190 (defcustom auto-revert-mode-hook nil
191 "Functions to run when Auto-Revert Mode is activated."
192 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
193 :group 'auto-revert
194 :type 'hook)
195
196 (defcustom global-auto-revert-mode-text ""
197 "String to display when Global Auto-Revert Mode is active.
198
199 The default is nothing since when this mode is active this text doesn't
200 vary over time, or between buffers. Hence mode line text
201 would only waste precious space."
202 :group 'auto-revert
203 :type 'string)
204
205 (defcustom global-auto-revert-mode-hook nil
206 "Hook called when Global Auto-Revert Mode is activated."
207 :group 'auto-revert
208 :type 'hook)
209
210 (defcustom global-auto-revert-non-file-buffers nil
211 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
212
213 When non-nil, both file buffers and buffers with a custom
214 `revert-buffer-function' and a `buffer-stale-function' are
215 reverted by Global Auto-Revert mode. These include the Buffer
216 List buffer displayed by `buffer-menu', and Dired buffers showing
217 complete local directories. The Buffer List buffer reverts every
218 `auto-revert-interval' seconds; Dired buffers when the file list of
219 the main directory changes. Dired buffers do not auto-revert as
220 a result of changes in subdirectories, or in the contents, size,
221 modes, etc., of files. You may still sometimes want to revert
222 them manually.
223
224 Use this option with care since it could lead to excessive auto-reverts.
225 For more information, see Info node `(emacs)Autorevert'."
226 :group 'auto-revert
227 :type 'boolean
228 :link '(info-link "(emacs)Autorevert"))
229
230 (defcustom global-auto-revert-ignore-modes ()
231 "List of major modes Global Auto-Revert Mode should not check."
232 :group 'auto-revert
233 :type '(repeat sexp))
234
235 (defcustom auto-revert-load-hook nil
236 "Functions to run when Auto-Revert Mode is first loaded."
237 :tag "Load Hook"
238 :group 'auto-revert
239 :type 'hook)
240
241 (defcustom auto-revert-check-vc-info nil
242 "If non-nil Auto Revert Mode reliably updates version control info.
243 Auto Revert Mode updates version control info whenever the buffer
244 needs reverting, regardless of the value of this variable.
245 However, the version control state can change without changes to
246 the work file. If the change is made from the current Emacs
247 session, all info is updated. But if, for instance, a new
248 version is checked in from outside the current Emacs session, the
249 version control number in the mode line, as well as other version
250 control related information, may not be properly updated. If you
251 are worried about this, set this variable to a non-nil value.
252
253 This currently works by automatically updating the version
254 control info every `auto-revert-interval' seconds. Nevertheless,
255 it should not cause excessive CPU usage on a reasonably fast
256 machine, if it does not apply to too many version controlled
257 buffers. CPU usage depends on the version control system."
258 :group 'auto-revert
259 :type 'boolean
260 :version "22.1")
261
262 (defvar global-auto-revert-ignore-buffer nil
263 "When non-nil, Global Auto-Revert Mode will not revert this buffer.
264 This variable becomes buffer local when set in any fashion.")
265 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
266
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")
272
273 (defconst auto-revert-notify-enabled
274 (or (featurep 'gfilenotify) (featurep 'inotify) (featurep 'w32notify))
275 "Non-nil when Emacs has been compiled with file notification support.")
276
277 (defcustom auto-revert-use-notify auto-revert-notify-enabled
278 "If non-nil Auto Revert Mode uses file notification functions.
279 This requires Emacs being compiled with file notification
280 support (see `auto-revert-notify-enabled'). You should set this
281 variable through Custom."
282 :group 'auto-revert
283 :type 'boolean
284 :set (lambda (variable value)
285 (set-default variable (and auto-revert-notify-enabled value))
286 (unless (symbol-value variable)
287 (when auto-revert-notify-enabled
288 (dolist (buf (buffer-list))
289 (with-current-buffer buf
290 (when (symbol-value 'auto-revert-notify-watch-descriptor)
291 (auto-revert-notify-rm-watch)))))))
292 :initialize 'custom-initialize-default
293 :version "24.4")
294
295 (defcustom auto-revert-notify-exclude-dir-regexp
296 (concat
297 ;; No mounted file systems.
298 "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
299 ;; No remote files.
300 (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
301 "Regular expression of directories to be excluded from file notifications."
302 :group 'auto-revert
303 :type 'regexp
304 :version "24.4")
305
306 ;; Internal variables:
307
308 (defvar auto-revert-buffer-list ()
309 "List of buffers in Auto-Revert Mode.
310
311 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
312 buffers to this list.
313
314 The timer function `auto-revert-buffers' is responsible for purging
315 the list of old buffers.")
316
317 (defvar auto-revert-remaining-buffers ()
318 "Buffers not checked when user input stopped execution.")
319
320 (defvar auto-revert-tail-pos 0
321 "Position of last known end of file.")
322
323 (add-hook 'find-file-hook
324 (lambda ()
325 (set (make-local-variable 'auto-revert-tail-pos)
326 (nth 7 (file-attributes buffer-file-name)))))
327
328 (defvar auto-revert-notify-watch-descriptor-hash-list
329 (make-hash-table :test 'equal)
330 "A hash table collecting all file watch descriptors.
331 Hash key is a watch descriptor, hash value is a list of buffers
332 which are related to files being watched and carrying the same
333 default directory.")
334
335 (defvar auto-revert-notify-watch-descriptor nil
336 "The file watch descriptor active for the current buffer.")
337 (make-variable-buffer-local 'auto-revert-notify-watch-descriptor)
338 (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
339
340 (defvar 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.")
343 (make-variable-buffer-local 'auto-revert-notify-modified-p)
344
345 ;; Functions:
346
347 ;;;###autoload
348 (define-minor-mode auto-revert-mode
349 "Toggle reverting buffer when the file changes (Auto Revert mode).
350 With a prefix argument ARG, enable Auto Revert mode if ARG is
351 positive, and disable it otherwise. If called from Lisp, enable
352 the mode if ARG is omitted or nil.
353
354 Auto Revert mode is a minor mode that affects only the current
355 buffer. When enabled, it reverts the buffer when the file on
356 disk changes.
357
358 Use `global-auto-revert-mode' to automatically revert all buffers.
359 Use `auto-revert-tail-mode' if you know that the file will only grow
360 without being changed in the part that is already in the buffer."
361 :group 'auto-revert :lighter auto-revert-mode-text
362 (if auto-revert-mode
363 (if (not (memq (current-buffer) auto-revert-buffer-list))
364 (push (current-buffer) auto-revert-buffer-list))
365 (when auto-revert-use-notify (auto-revert-notify-rm-watch))
366 (setq auto-revert-buffer-list
367 (delq (current-buffer) auto-revert-buffer-list)))
368 (auto-revert-set-timer)
369 (when auto-revert-mode
370 (let (auto-revert-use-notify)
371 (auto-revert-buffers)
372 (setq auto-revert-tail-mode nil))))
373
374
375 ;;;###autoload
376 (defun turn-on-auto-revert-mode ()
377 "Turn on Auto-Revert Mode.
378
379 This function is designed to be added to hooks, for example:
380 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
381 (auto-revert-mode 1))
382
383
384 ;;;###autoload
385 (define-minor-mode auto-revert-tail-mode
386 "Toggle reverting tail of buffer when the file grows.
387 With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
388 is positive, and disable it otherwise. If called from Lisp,
389 enable the mode if ARG is omitted or nil.
390
391 When Auto Revert Tail mode is enabled, the tail of the file is
392 constantly followed, as with the shell command `tail -f'. This
393 means that whenever the file grows on disk (presumably because
394 some background process is appending to it from time to time),
395 this is reflected in the current buffer.
396
397 You can edit the buffer and turn this mode off and on again as
398 you please. But make sure the background process has stopped
399 writing before you save the file!
400
401 Use `auto-revert-mode' for changes other than appends!"
402 :group 'find-file :lighter auto-revert-tail-mode-text
403 (when auto-revert-tail-mode
404 (unless buffer-file-name
405 (auto-revert-tail-mode 0)
406 (error "This buffer is not visiting a file"))
407 (if (and (buffer-modified-p)
408 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
409 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
410 (auto-revert-tail-mode 0)
411 ;; a-r-tail-pos stores the size of the file at the time of the
412 ;; last revert. After this package loads, it adds a
413 ;; find-file-hook to set this variable every time a file is
414 ;; loaded. If the package is loaded only _after_ visiting the
415 ;; file to be reverted, then we have no idea what the value of
416 ;; a-r-tail-pos should have been when the file was visited. If
417 ;; the file has changed on disk in the meantime, all we can do
418 ;; is offer to revert the whole thing. If you choose not to
419 ;; revert, then you might miss some output then happened
420 ;; between visiting the file and activating a-r-t-mode.
421 (and (zerop auto-revert-tail-pos)
422 (not (verify-visited-file-modtime (current-buffer)))
423 (y-or-n-p "File changed on disk, content may be missing. \
424 Perform a full revert? ")
425 ;; Use this (not just revert-buffer) for point-preservation.
426 (let (auto-revert-use-notify)
427 (auto-revert-handler)))
428 ;; else we might reappend our own end when we save
429 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
430 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
431 (set (make-local-variable 'auto-revert-tail-pos)
432 (nth 7 (file-attributes buffer-file-name))))
433 ;; let auto-revert-mode set up the mechanism for us if it isn't already
434 (or auto-revert-mode
435 (let ((auto-revert-tail-mode t))
436 (auto-revert-mode 1)))
437 (setq auto-revert-mode nil))))
438
439
440 ;;;###autoload
441 (defun turn-on-auto-revert-tail-mode ()
442 "Turn on Auto-Revert Tail mode.
443
444 This function is designed to be added to hooks, for example:
445 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
446 (auto-revert-tail-mode 1))
447
448
449 ;;;###autoload
450 (define-minor-mode global-auto-revert-mode
451 "Toggle Global Auto Revert mode.
452 With a prefix argument ARG, enable Global Auto Revert mode if ARG
453 is positive, and disable it otherwise. If called from Lisp,
454 enable the mode if ARG is omitted or nil.
455
456 Global Auto Revert mode is a global minor mode that reverts any
457 buffer associated with a file when the file changes on disk. Use
458 `auto-revert-mode' to revert a particular buffer.
459
460 If `global-auto-revert-non-file-buffers' is non-nil, this mode
461 may also revert some non-file buffers, as described in the
462 documentation of that variable. It ignores buffers with modes
463 matching `global-auto-revert-ignore-modes', and buffers with a
464 non-nil vale of `global-auto-revert-ignore-buffer'.
465
466 This function calls the hook `global-auto-revert-mode-hook'.
467 It displays the text that `global-auto-revert-mode-text'
468 specifies in the mode line."
469 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
470 (auto-revert-set-timer)
471 (if global-auto-revert-mode
472 (let (auto-revert-use-notify)
473 (auto-revert-buffers))
474 (dolist (buf (buffer-list))
475 (with-current-buffer buf
476 (when auto-revert-use-notify
477 (auto-revert-notify-rm-watch))))))
478
479 (defun auto-revert-set-timer ()
480 "Restart or cancel the timer used by Auto-Revert Mode.
481 If such a timer is active, cancel it. Start a new timer if
482 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
483 in some buffer. Restarting the timer ensures that Auto-Revert Mode
484 will use an up-to-date value of `auto-revert-interval'"
485 (interactive)
486 (if (timerp auto-revert-timer)
487 (cancel-timer auto-revert-timer))
488 (setq auto-revert-timer
489 (if (or global-auto-revert-mode auto-revert-buffer-list)
490 (run-with-timer auto-revert-interval
491 auto-revert-interval
492 'auto-revert-buffers))))
493
494 (defun auto-revert-notify-rm-watch ()
495 "Disable file notification for current buffer's associated file."
496 (when auto-revert-notify-watch-descriptor
497 (maphash
498 (lambda (key value)
499 (when (equal key auto-revert-notify-watch-descriptor)
500 (setq value (delete (current-buffer) value))
501 (if value
502 (puthash key value auto-revert-notify-watch-descriptor-hash-list)
503 (remhash key auto-revert-notify-watch-descriptor-hash-list)
504 (ignore-errors
505 (funcall
506 (cond
507 ((fboundp 'gfile-rm-watch) 'gfile-rm-watch)
508 ((fboundp 'inotify-rm-watch) 'inotify-rm-watch)
509 ((fboundp 'w32notify-rm-watch) 'w32notify-rm-watch))
510 auto-revert-notify-watch-descriptor)))))
511 auto-revert-notify-watch-descriptor-hash-list)
512 (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch))
513 (setq auto-revert-notify-watch-descriptor nil
514 auto-revert-notify-modified-p nil))
515
516 (defun auto-revert-notify-add-watch ()
517 "Enable file notification for current buffer's associated file."
518 (when (string-match auto-revert-notify-exclude-dir-regexp
519 (expand-file-name default-directory))
520 ;; Fallback to file checks.
521 (set (make-local-variable 'auto-revert-use-notify) nil))
522
523 (when (and buffer-file-name auto-revert-use-notify
524 (not auto-revert-notify-watch-descriptor))
525 (let ((func
526 (cond
527 ((fboundp 'gfile-add-watch) 'gfile-add-watch)
528 ((fboundp 'inotify-add-watch) 'inotify-add-watch)
529 ((fboundp 'w32notify-add-watch) 'w32notify-add-watch)))
530 (aspect
531 (cond
532 ((fboundp 'gfile-add-watch) '(watch-mounts))
533 ;; `attrib' is needed for file modification time.
534 ((fboundp 'inotify-add-watch) '(attrib create modify moved-to))
535 ((fboundp 'w32notify-add-watch) '(size last-write-time))))
536 (file (if (or (fboundp 'gfile-add-watch) (fboundp 'inotify-add-watch))
537 (directory-file-name (expand-file-name default-directory))
538 (buffer-file-name))))
539 (setq auto-revert-notify-watch-descriptor
540 (ignore-errors
541 (funcall func file aspect 'auto-revert-notify-handler)))
542 (if auto-revert-notify-watch-descriptor
543 (progn
544 (puthash
545 auto-revert-notify-watch-descriptor
546 (cons (current-buffer)
547 (gethash auto-revert-notify-watch-descriptor
548 auto-revert-notify-watch-descriptor-hash-list))
549 auto-revert-notify-watch-descriptor-hash-list)
550 (add-hook (make-local-variable 'kill-buffer-hook)
551 'auto-revert-notify-rm-watch))
552 ;; Fallback to file checks.
553 (set (make-local-variable 'auto-revert-use-notify) nil)))))
554
555 (defun auto-revert-notify-event-p (event)
556 "Check that event is a file notification event."
557 (and (listp event)
558 (cond ((featurep 'gfilenotify)
559 (and (>= (length event) 3) (stringp (nth 2 event))))
560 ((featurep 'inotify)
561 (= (length event) 4))
562 ((featurep 'w32notify)
563 (and (= (length event) 3) (stringp (nth 2 event)))))))
564
565 (defun auto-revert-notify-event-descriptor (event)
566 "Return watch descriptor of file notification event, or nil."
567 (and (auto-revert-notify-event-p event) (car event)))
568
569 (defun auto-revert-notify-event-action (event)
570 "Return action of file notification event, or nil."
571 (and (auto-revert-notify-event-p event) (nth 1 event)))
572
573 (defun auto-revert-notify-event-file-name (event)
574 "Return file name of file notification event, or nil."
575 (and (auto-revert-notify-event-p event)
576 (cond ((featurep 'gfilenotify) (nth 2 event))
577 ((featurep 'inotify) (nth 3 event))
578 ((featurep 'w32notify) (nth 2 event)))))
579
580 (defun auto-revert-notify-handler (event)
581 "Handle an EVENT returned from file notification."
582 (when (auto-revert-notify-event-p event)
583 (let* ((descriptor (auto-revert-notify-event-descriptor event))
584 (action (auto-revert-notify-event-action event))
585 (file (auto-revert-notify-event-file-name event))
586 (buffers (gethash descriptor
587 auto-revert-notify-watch-descriptor-hash-list)))
588 (ignore-errors
589 ;; Check, that event is meant for us.
590 ;; TODO: Filter events which stop watching, like `move' or `removed'.
591 (cl-assert descriptor)
592 (cond
593 ((featurep 'gfilenotify)
594 (cl-assert (memq action '(attribute-changed changed created deleted
595 ;; FIXME: I keep getting this action, so I
596 ;; added it here, but I have no idea what
597 ;; I'm doing. --Stef
598 changes-done-hint))
599 t))
600 ((featurep 'inotify)
601 (cl-assert (or (memq 'attrib action)
602 (memq 'create action)
603 (memq 'modify action)
604 (memq 'moved-to action))))
605 ((featurep 'w32notify) (cl-assert (eq 'modified action))))
606 ;; Since we watch a directory, a file name must be returned.
607 (cl-assert (stringp file))
608 (dolist (buffer buffers)
609 (when (buffer-live-p buffer)
610 (with-current-buffer buffer
611 (when (and (stringp buffer-file-name)
612 (string-equal
613 (file-name-nondirectory file)
614 (file-name-nondirectory buffer-file-name)))
615 ;; Mark buffer modified.
616 (setq auto-revert-notify-modified-p t)
617 ;; No need to check other buffers.
618 (cl-return)))))))))
619
620 (defun auto-revert-active-p ()
621 "Check if auto-revert is active (in current buffer or globally)."
622 (or auto-revert-mode
623 auto-revert-tail-mode
624 (and
625 global-auto-revert-mode
626 (not global-auto-revert-ignore-buffer)
627 (not (memq major-mode
628 global-auto-revert-ignore-modes)))))
629
630 (defun auto-revert-handler ()
631 "Revert current buffer, if appropriate.
632 This is an internal function used by Auto-Revert Mode."
633 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
634 (let* ((buffer (current-buffer)) size
635 ;; Tramp caches the file attributes. Setting
636 ;; `remote-file-name-inhibit-cache' forces Tramp to reread
637 ;; the values.
638 (remote-file-name-inhibit-cache t)
639 (revert
640 (or (and buffer-file-name
641 (or auto-revert-remote-files
642 (not (file-remote-p buffer-file-name)))
643 (or (not auto-revert-use-notify)
644 auto-revert-notify-modified-p)
645 (if auto-revert-tail-mode
646 (and (file-readable-p buffer-file-name)
647 (/= auto-revert-tail-pos
648 (setq size
649 (nth 7 (file-attributes
650 buffer-file-name)))))
651 (and (file-readable-p buffer-file-name)
652 (not (verify-visited-file-modtime buffer)))))
653 (and (or auto-revert-mode
654 global-auto-revert-non-file-buffers)
655 revert-buffer-function
656 (boundp 'buffer-stale-function)
657 (functionp buffer-stale-function)
658 (funcall buffer-stale-function t))))
659 eob eoblist)
660 (setq auto-revert-notify-modified-p nil)
661 (when revert
662 (when (and auto-revert-verbose
663 (not (eq revert 'fast)))
664 (message "Reverting buffer `%s'." (buffer-name)))
665 ;; If point (or a window point) is at the end of the buffer,
666 ;; we want to keep it at the end after reverting. This allows
667 ;; to tail a file.
668 (when buffer-file-name
669 (setq eob (eobp))
670 (walk-windows
671 (lambda (window)
672 (and (eq (window-buffer window) buffer)
673 (= (window-point window) (point-max))
674 (push window eoblist)))
675 'no-mini t))
676 (if auto-revert-tail-mode
677 (auto-revert-tail-handler size)
678 ;; Bind buffer-read-only in case user has done C-x C-q,
679 ;; so as not to forget that. This gives undesirable results
680 ;; when the file's mode changes, but that is less common.
681 (let ((buffer-read-only buffer-read-only))
682 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
683 (when buffer-file-name
684 (when eob (goto-char (point-max)))
685 (dolist (window eoblist)
686 (set-window-point window (point-max)))))
687 ;; `preserve-modes' avoids changing the (minor) modes. But we
688 ;; do want to reset the mode for VC, so we do it manually.
689 (when (or revert auto-revert-check-vc-info)
690 (vc-find-file-hook)))))
691
692 (defun auto-revert-tail-handler (size)
693 (let ((modified (buffer-modified-p))
694 (inhibit-read-only t) ; Ignore.
695 (file buffer-file-name)
696 (buffer-file-name nil)) ; Ignore that file has changed.
697 (when (/= auto-revert-tail-pos size)
698 (run-hooks 'before-revert-hook)
699 (undo-boundary)
700 (save-restriction
701 (widen)
702 (save-excursion
703 (goto-char (point-max))
704 (insert-file-contents file nil
705 (and (< auto-revert-tail-pos size)
706 auto-revert-tail-pos)
707 size)))
708 (run-hooks 'after-revert-hook)
709 (undo-boundary)
710 (setq auto-revert-tail-pos size)
711 (restore-buffer-modified-p modified)))
712 (set-visited-file-modtime))
713
714 (defun auto-revert-buffers ()
715 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
716
717 Should `global-auto-revert-mode' be active all file buffers are checked.
718
719 Should `auto-revert-mode' be active in some buffers, those buffers
720 are checked.
721
722 Non-file buffers that have a custom `revert-buffer-function' and
723 a `buffer-stale-function' are reverted either when Auto-Revert
724 Mode is active in that buffer, or when the variable
725 `global-auto-revert-non-file-buffers' is non-nil and Global
726 Auto-Revert Mode is active.
727
728 This function stops whenever there is user input. The buffers not
729 checked are stored in the variable `auto-revert-remaining-buffers'.
730
731 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
732 are checked first the next time this function is called.
733
734 This function is also responsible for removing buffers no longer in
735 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
736 the timer when no buffers need to be checked."
737 (save-match-data
738 (let ((bufs (if global-auto-revert-mode
739 (buffer-list)
740 auto-revert-buffer-list))
741 (remaining ())
742 (new ()))
743 ;; Partition `bufs' into two halves depending on whether or not
744 ;; the buffers are in `auto-revert-remaining-buffers'. The two
745 ;; halves are then re-joined with the "remaining" buffers at the
746 ;; head of the list.
747 (dolist (buf auto-revert-remaining-buffers)
748 (if (memq buf bufs)
749 (push buf remaining)))
750 (dolist (buf bufs)
751 (if (not (memq buf remaining))
752 (push buf new)))
753 (setq bufs (nreverse (nconc new remaining)))
754 (while (and bufs
755 (not (and auto-revert-stop-on-user-input
756 (input-pending-p))))
757 (let ((buf (car bufs)))
758 (if (buffer-live-p buf)
759 (with-current-buffer buf
760 ;; Test if someone has turned off Auto-Revert Mode in a
761 ;; non-standard way, for example by changing major mode.
762 (if (and (not auto-revert-mode)
763 (not auto-revert-tail-mode)
764 (memq buf auto-revert-buffer-list))
765 (setq auto-revert-buffer-list
766 (delq buf auto-revert-buffer-list)))
767 (when (auto-revert-active-p)
768 ;; Enable file notification.
769 (when (and auto-revert-use-notify buffer-file-name
770 (not auto-revert-notify-watch-descriptor))
771 (auto-revert-notify-add-watch))
772 (auto-revert-handler)))
773 ;; Remove dead buffer from `auto-revert-buffer-list'.
774 (setq auto-revert-buffer-list
775 (delq buf auto-revert-buffer-list))))
776 (setq bufs (cdr bufs)))
777 (setq auto-revert-remaining-buffers bufs)
778 ;; Check if we should cancel the timer.
779 (when (and (not global-auto-revert-mode)
780 (null auto-revert-buffer-list))
781 (cancel-timer auto-revert-timer)
782 (setq auto-revert-timer nil)))))
783
784
785 ;; The end:
786 (provide 'autorevert)
787
788 (run-hooks 'auto-revert-load-hook)
789
790 ;;; autorevert.el ends here