Nuke arch-tags.
[emacs.git] / lisp / autorevert.el
blob4caf0cafc55b55e6d99f342025e0f4553860b4e2
1 ;;; autorevert.el --- revert buffers when files on disk change
3 ;; Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Anders Lindgren <andersl@andersl.com>
7 ;; Keywords: convenience
8 ;; Created: 1997-06-01
9 ;; Date: 1999-11-30
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Introduction:
30 ;; Whenever a file that Emacs is editing has been changed by another
31 ;; program the user normally has to execute the command `revert-buffer'
32 ;; to load the new content of the file into Emacs.
34 ;; This package contains two minor modes: Global Auto-Revert Mode and
35 ;; Auto-Revert Mode. Both modes automatically revert buffers
36 ;; whenever the corresponding files have been changed on disk and the
37 ;; buffer contains no unsaved changes.
39 ;; Auto-Revert Mode can be activated for individual buffers. Global
40 ;; Auto-Revert Mode applies to all file buffers. (If the user option
41 ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies
42 ;; to some non-file buffers. This option is disabled by default.)
43 ;; Since checking a remote file is too slow, these modes do not check
44 ;; or revert remote files.
46 ;; Both modes operate by checking the time stamp of all files at
47 ;; intervals of `auto-revert-interval'. The default is every five
48 ;; seconds. The check is aborted whenever the user actually uses
49 ;; Emacs. You should never even notice that this package is active
50 ;; (except that your buffers will be reverted, of course).
52 ;; After reverting a file buffer, Auto Revert Mode normally puts point
53 ;; at the same position that a regular manual revert would. However,
54 ;; there is one exception to this rule. If point is at the end of the
55 ;; buffer before reverting, it stays at the end. Similarly if point
56 ;; is displayed at the end of a file buffer in any window, it will stay
57 ;; at the end of the buffer in that window, even if the window is not
58 ;; selected. This way, you can use Auto Revert Mode to `tail' a file.
59 ;; Just put point at the end of the buffer and it will stay there.
60 ;; These rules apply to file buffers. For non-file buffers, the
61 ;; behavior may be mode dependent.
63 ;; While you can use Auto Revert Mode to tail a file, this package
64 ;; contains a third minor mode, Auto Revert Tail Mode, which does so
65 ;; more efficiently, as long as you are sure that the file will only
66 ;; change by growing at the end. It only appends the new output,
67 ;; instead of reverting the entire buffer. It does so even if the
68 ;; buffer contains unsaved changes. (Because they will not be lost.)
69 ;; Auto Revert Tail Mode works also for remote files.
71 ;; Usage:
73 ;; Go to the appropriate buffer and press either of:
74 ;; M-x auto-revert-mode RET
75 ;; M-x auto-revert-tail-mode RET
77 ;; To activate Global Auto-Revert Mode, press:
78 ;; M-x global-auto-revert-mode RET
80 ;; To activate Global Auto-Revert Mode every time Emacs is started
81 ;; customise the option `global-auto-revert-mode' or the following
82 ;; line could be added to your ~/.emacs:
83 ;; (global-auto-revert-mode 1)
85 ;; The function `turn-on-auto-revert-mode' could be added to any major
86 ;; mode hook to activate Auto-Revert Mode for all buffers in that
87 ;; mode. For example, the following line will activate Auto-Revert
88 ;; Mode in all C mode buffers:
90 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
92 ;;; Code:
94 ;; Dependencies:
96 (require 'timer)
98 (eval-when-compile (require 'cl))
101 ;; Custom Group:
103 ;; The two modes will be placed next to Auto Save Mode under the
104 ;; Files group under Emacs.
106 (defgroup auto-revert nil
107 "Revert individual buffers when files on disk change.
109 Auto-Revert Mode can be activated for individual buffer.
110 Global Auto-Revert Mode applies to all buffers."
111 :group 'files
112 :group 'convenience)
115 ;; Variables:
117 ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
118 ;;; What's this?: ;;;###autoload
119 (defvar auto-revert-mode nil
120 "*Non-nil when Auto-Revert Mode is active.
121 Never set this variable directly, use the command `auto-revert-mode' instead.")
122 (put 'auto-revert-mode 'permanent-local t)
124 (defvar auto-revert-tail-mode nil
125 "*Non-nil when Auto-Revert Tail Mode is active.
126 Never set this variable directly, use the command
127 `auto-revert-tail-mode' instead.")
128 (put 'auto-revert-tail-mode 'permanent-local t)
130 (defvar auto-revert-timer nil
131 "Timer used by Auto-Revert Mode.")
133 (defcustom auto-revert-interval 5
134 "Time, in seconds, between Auto-Revert Mode file checks.
135 The value may be an integer or floating point number.
137 If a timer is already active, there are two ways to make sure
138 that the new value will take effect immediately. You can set
139 this variable through Custom or you can call the command
140 `auto-revert-set-timer' after setting the variable. Otherwise,
141 the new value will take effect the first time Auto Revert Mode
142 calls `auto-revert-set-timer' for internal reasons or in your
143 next editing session."
144 :group 'auto-revert
145 :type 'number
146 :set (lambda (variable value)
147 (set-default variable value)
148 (and (boundp 'auto-revert-timer)
149 auto-revert-timer
150 (auto-revert-set-timer))))
152 (defcustom auto-revert-stop-on-user-input t
153 "When non-nil, user input temporarily interrupts Auto-Revert Mode.
154 With this setting, Auto-Revert Mode checks for user input after
155 handling each buffer and does not process any further buffers
156 \(until the next run of the timer) if user input is available.
157 When nil, Auto-Revert Mode checks files and reverts buffers,
158 with quitting disabled, without paying attention to user input.
159 Thus, with this setting, Emacs might be non-responsive at times."
160 :group 'auto-revert
161 :type 'boolean)
163 (defcustom auto-revert-verbose t
164 "When nil, Auto-Revert Mode does not generate any messages.
165 When non-nil, a message is generated whenever a file is reverted."
166 :group 'auto-revert
167 :type 'boolean)
169 (defcustom auto-revert-mode-text " ARev"
170 "String to display in the mode line when Auto-Revert Mode is active.
172 \(When the string is not empty, make sure that it has a leading space.)"
173 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
174 :group 'auto-revert
175 :type 'string)
177 (defcustom auto-revert-tail-mode-text " Tail"
178 "String to display in the mode line when Auto-Revert Tail Mode is active.
180 \(When the string is not empty, make sure that it has a leading space.)"
181 :group 'auto-revert
182 :type 'string
183 :version "22.1")
185 (defcustom auto-revert-mode-hook nil
186 "Functions to run when Auto-Revert Mode is activated."
187 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
188 :group 'auto-revert
189 :type 'hook)
191 (defcustom global-auto-revert-mode-text ""
192 "String to display when Global Auto-Revert Mode is active.
194 The default is nothing since when this mode is active this text doesn't
195 vary over time, or between buffers. Hence mode line text
196 would only waste precious space."
197 :group 'auto-revert
198 :type 'string)
200 (defcustom global-auto-revert-mode-hook nil
201 "Hook called when Global Auto-Revert Mode is activated."
202 :group 'auto-revert
203 :type 'hook)
205 (defcustom global-auto-revert-non-file-buffers nil
206 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
208 When non-nil, both file buffers and buffers with a custom
209 `revert-buffer-function' and a `buffer-stale-function' are
210 reverted by Global Auto-Revert mode. These include the Buffer
211 List buffer displayed by `buffer-menu', and Dired buffers showing
212 complete local directories. The Buffer List buffer reverts every
213 `auto-revert-interval' seconds; Dired buffers when the file list of
214 the main directory changes. Dired buffers do not auto-revert as
215 a result of changes in subdirectories, or in the contents, size,
216 modes, etc., of files. You may still sometimes want to revert
217 them manually.
219 Use this option with care since it could lead to excessive auto-reverts.
220 For more information, see Info node `(emacs)Autorevert'."
221 :group 'auto-revert
222 :type 'boolean
223 :link '(info-link "(emacs)Autorevert"))
225 (defcustom global-auto-revert-ignore-modes ()
226 "List of major modes Global Auto-Revert Mode should not check."
227 :group 'auto-revert
228 :type '(repeat sexp))
230 (defcustom auto-revert-load-hook nil
231 "Functions to run when Auto-Revert Mode is first loaded."
232 :tag "Load Hook"
233 :group 'auto-revert
234 :type 'hook)
236 (defcustom auto-revert-check-vc-info nil
237 "If non-nil Auto Revert Mode reliably updates version control info.
238 Auto Revert Mode updates version control info whenever the buffer
239 needs reverting, regardless of the value of this variable.
240 However, the version control state can change without changes to
241 the work file. If the change is made from the current Emacs
242 session, all info is updated. But if, for instance, a new
243 version is checked in from outside the current Emacs session, the
244 version control number in the mode line, as well as other version
245 control related information, may not be properly updated. If you
246 are worried about this, set this variable to a non-nil value.
248 This currently works by automatically updating the version
249 control info every `auto-revert-interval' seconds. Nevertheless,
250 it should not cause excessive CPU usage on a reasonably fast
251 machine, if it does not apply to too many version controlled
252 buffers. CPU usage depends on the version control system."
253 :group 'auto-revert
254 :type 'boolean
255 :version "22.1")
257 (defvar global-auto-revert-ignore-buffer nil
258 "*When non-nil, Global Auto-Revert Mode will not revert this buffer.
260 This variable becomes buffer local when set in any fashion.")
261 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
263 ;; Internal variables:
265 (defvar auto-revert-buffer-list ()
266 "List of buffers in Auto-Revert Mode.
268 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
269 buffers to this list.
271 The timer function `auto-revert-buffers' is responsible for purging
272 the list of old buffers.")
274 (defvar auto-revert-remaining-buffers ()
275 "Buffers not checked when user input stopped execution.")
277 (defvar auto-revert-tail-pos 0
278 "Position of last known end of file.")
280 (add-hook 'find-file-hook
281 (lambda ()
282 (set (make-local-variable 'auto-revert-tail-pos)
283 (nth 7 (file-attributes buffer-file-name)))))
285 ;; Functions:
287 ;;;###autoload
288 (define-minor-mode auto-revert-mode
289 "Toggle reverting buffer when file on disk changes.
291 With arg, turn Auto Revert mode on if and only if arg is positive.
292 This is a minor mode that affects only the current buffer.
293 Use `global-auto-revert-mode' to automatically revert all buffers.
294 Use `auto-revert-tail-mode' if you know that the file will only grow
295 without being changed in the part that is already in the buffer."
296 :group 'auto-revert :lighter auto-revert-mode-text
297 (if auto-revert-mode
298 (if (not (memq (current-buffer) auto-revert-buffer-list))
299 (push (current-buffer) auto-revert-buffer-list))
300 (setq auto-revert-buffer-list
301 (delq (current-buffer) auto-revert-buffer-list)))
302 (auto-revert-set-timer)
303 (when auto-revert-mode
304 (auto-revert-buffers)
305 (setq auto-revert-tail-mode nil)))
308 ;;;###autoload
309 (defun turn-on-auto-revert-mode ()
310 "Turn on Auto-Revert Mode.
312 This function is designed to be added to hooks, for example:
313 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
314 (auto-revert-mode 1))
317 ;;;###autoload
318 (define-minor-mode auto-revert-tail-mode
319 "Toggle reverting tail of buffer when file on disk grows.
320 With arg, turn Tail mode on if arg is positive, otherwise turn it off.
322 When Tail mode is enabled, the tail of the file is constantly
323 followed, as with the shell command `tail -f'. This means that
324 whenever the file grows on disk (presumably because some
325 background process is appending to it from time to time), this is
326 reflected in the current buffer.
328 You can edit the buffer and turn this mode off and on again as
329 you please. But make sure the background process has stopped
330 writing before you save the file!
332 Use `auto-revert-mode' for changes other than appends!"
333 :group 'find-file :lighter auto-revert-tail-mode-text
334 (when auto-revert-tail-mode
335 (unless buffer-file-name
336 (auto-revert-tail-mode 0)
337 (error "This buffer is not visiting a file"))
338 (if (and (buffer-modified-p)
339 (zerop auto-revert-tail-pos) ; library was loaded only after finding file
340 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
341 (auto-revert-tail-mode 0)
342 ;; a-r-tail-pos stores the size of the file at the time of the
343 ;; last revert. After this package loads, it adds a
344 ;; find-file-hook to set this variable every time a file is
345 ;; loaded. If the package is loaded only _after_ visiting the
346 ;; file to be reverted, then we have no idea what the value of
347 ;; a-r-tail-pos should have been when the file was visited. If
348 ;; the file has changed on disk in the meantime, all we can do
349 ;; is offer to revert the whole thing. If you choose not to
350 ;; revert, then you might miss some output then happened
351 ;; between visiting the file and activating a-r-t-mode.
352 (and (zerop auto-revert-tail-pos)
353 (not (verify-visited-file-modtime (current-buffer)))
354 (y-or-n-p "File changed on disk, content may be missing. \
355 Perform a full revert? ")
356 ;; Use this (not just revert-buffer) for point-preservation.
357 (auto-revert-handler))
358 ;; else we might reappend our own end when we save
359 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
360 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
361 (set (make-local-variable 'auto-revert-tail-pos)
362 (nth 7 (file-attributes buffer-file-name))))
363 ;; let auto-revert-mode set up the mechanism for us if it isn't already
364 (or auto-revert-mode
365 (let ((auto-revert-tail-mode t))
366 (auto-revert-mode 1)))
367 (setq auto-revert-mode nil))))
370 ;;;###autoload
371 (defun turn-on-auto-revert-tail-mode ()
372 "Turn on Auto-Revert Tail Mode.
374 This function is designed to be added to hooks, for example:
375 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
376 (auto-revert-tail-mode 1))
379 ;;;###autoload
380 (define-minor-mode global-auto-revert-mode
381 "Toggle Global Auto Revert mode.
382 With optional prefix argument ARG, enable Global Auto Revert Mode
383 if ARG > 0, else disable it.
385 This is a global minor mode that reverts any buffer associated
386 with a file when the file changes on disk. Use `auto-revert-mode'
387 to revert a particular buffer.
389 If `global-auto-revert-non-file-buffers' is non-nil, this mode
390 may also revert some non-file buffers, as described in the
391 documentation of that variable. It ignores buffers with modes
392 matching `global-auto-revert-ignore-modes', and buffers with a
393 non-nil vale of `global-auto-revert-ignore-buffer'.
395 This function calls the hook `global-auto-revert-mode-hook'.
396 It displays the text that `global-auto-revert-mode-text'
397 specifies in the mode line."
398 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
399 (auto-revert-set-timer)
400 (when global-auto-revert-mode
401 (auto-revert-buffers)))
404 (defun auto-revert-set-timer ()
405 "Restart or cancel the timer used by Auto-Revert Mode.
406 If such a timer is active, cancel it. Start a new timer if
407 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
408 in some buffer. Restarting the timer ensures that Auto-Revert Mode
409 will use an up-to-date value of `auto-revert-interval'"
410 (interactive)
411 (if (timerp auto-revert-timer)
412 (cancel-timer auto-revert-timer))
413 (setq auto-revert-timer
414 (if (or global-auto-revert-mode auto-revert-buffer-list)
415 (run-with-timer auto-revert-interval
416 auto-revert-interval
417 'auto-revert-buffers))))
419 (defun auto-revert-active-p ()
420 "Check if auto-revert is active (in current buffer or globally)."
421 (or auto-revert-mode
422 auto-revert-tail-mode
423 (and
424 global-auto-revert-mode
425 (not global-auto-revert-ignore-buffer)
426 (not (memq major-mode
427 global-auto-revert-ignore-modes)))))
429 (defun auto-revert-handler ()
430 "Revert current buffer, if appropriate.
431 This is an internal function used by Auto-Revert Mode."
432 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
433 (let* ((buffer (current-buffer)) size
434 (revert
435 (or (and buffer-file-name
436 (file-readable-p buffer-file-name)
437 (if auto-revert-tail-mode
438 ;; Tramp caches the file attributes. Setting
439 ;; `tramp-cache-inhibit' forces Tramp to
440 ;; reread the values.
441 (let ((tramp-cache-inhibit-cache t))
442 (/= auto-revert-tail-pos
443 (setq size
444 (nth 7 (file-attributes
445 buffer-file-name)))))
446 (and (not (file-remote-p buffer-file-name))
447 (not (verify-visited-file-modtime buffer)))))
448 (and (or auto-revert-mode
449 global-auto-revert-non-file-buffers)
450 revert-buffer-function
451 (boundp 'buffer-stale-function)
452 (functionp buffer-stale-function)
453 (funcall buffer-stale-function t))))
454 eob eoblist)
455 (when revert
456 (when (and auto-revert-verbose
457 (not (eq revert 'fast)))
458 (message "Reverting buffer `%s'." (buffer-name)))
459 ;; If point (or a window point) is at the end of the buffer,
460 ;; we want to keep it at the end after reverting. This allows
461 ;; to tail a file.
462 (when buffer-file-name
463 (setq eob (eobp))
464 (walk-windows
465 #'(lambda (window)
466 (and (eq (window-buffer window) buffer)
467 (= (window-point window) (point-max))
468 (push window eoblist)))
469 'no-mini t))
470 (if auto-revert-tail-mode
471 (auto-revert-tail-handler size)
472 ;; Bind buffer-read-only in case user has done C-x C-q,
473 ;; so as not to forget that. This gives undesirable results
474 ;; when the file's mode changes, but that is less common.
475 (let ((buffer-read-only buffer-read-only))
476 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
477 (when buffer-file-name
478 (when eob (goto-char (point-max)))
479 (dolist (window eoblist)
480 (set-window-point window (point-max)))))
481 ;; `preserve-modes' avoids changing the (minor) modes. But we
482 ;; do want to reset the mode for VC, so we do it manually.
483 (when (or revert auto-revert-check-vc-info)
484 (vc-find-file-hook)))))
486 (defun auto-revert-tail-handler (size)
487 (let ((modified (buffer-modified-p))
488 (inhibit-read-only t) ; Ignore.
489 (file buffer-file-name)
490 (buffer-file-name nil)) ; Ignore that file has changed.
491 (when (/= auto-revert-tail-pos size)
492 (run-hooks 'before-revert-hook)
493 (undo-boundary)
494 (save-restriction
495 (widen)
496 (save-excursion
497 (goto-char (point-max))
498 (insert-file-contents file nil
499 (and (< auto-revert-tail-pos size)
500 auto-revert-tail-pos)
501 size)))
502 (run-hooks 'after-revert-hook)
503 (undo-boundary)
504 (setq auto-revert-tail-pos size)
505 (restore-buffer-modified-p modified)))
506 (set-visited-file-modtime))
508 (defun auto-revert-buffers ()
509 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
511 Should `global-auto-revert-mode' be active all file buffers are checked.
513 Should `auto-revert-mode' be active in some buffers, those buffers
514 are checked.
516 Non-file buffers that have a custom `revert-buffer-function' and
517 a `buffer-stale-function' are reverted either when Auto-Revert
518 Mode is active in that buffer, or when the variable
519 `global-auto-revert-non-file-buffers' is non-nil and Global
520 Auto-Revert Mode is active.
522 This function stops whenever there is user input. The buffers not
523 checked are stored in the variable `auto-revert-remaining-buffers'.
525 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
526 are checked first the next time this function is called.
528 This function is also responsible for removing buffers no longer in
529 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
530 the timer when no buffers need to be checked."
531 (save-match-data
532 (let ((bufs (if global-auto-revert-mode
533 (buffer-list)
534 auto-revert-buffer-list))
535 (remaining ())
536 (new ()))
537 ;; Partition `bufs' into two halves depending on whether or not
538 ;; the buffers are in `auto-revert-remaining-buffers'. The two
539 ;; halves are then re-joined with the "remaining" buffers at the
540 ;; head of the list.
541 (dolist (buf auto-revert-remaining-buffers)
542 (if (memq buf bufs)
543 (push buf remaining)))
544 (dolist (buf bufs)
545 (if (not (memq buf remaining))
546 (push buf new)))
547 (setq bufs (nreverse (nconc new remaining)))
548 (while (and bufs
549 (not (and auto-revert-stop-on-user-input
550 (input-pending-p))))
551 (let ((buf (car bufs)))
552 (if (buffer-live-p buf)
553 (with-current-buffer buf
554 ;; Test if someone has turned off Auto-Revert Mode in a
555 ;; non-standard way, for example by changing major mode.
556 (if (and (not auto-revert-mode)
557 (not auto-revert-tail-mode)
558 (memq buf auto-revert-buffer-list))
559 (setq auto-revert-buffer-list
560 (delq buf auto-revert-buffer-list)))
561 (when (auto-revert-active-p) (auto-revert-handler)))
562 ;; Remove dead buffer from `auto-revert-buffer-list'.
563 (setq auto-revert-buffer-list
564 (delq buf auto-revert-buffer-list))))
565 (setq bufs (cdr bufs)))
566 (setq auto-revert-remaining-buffers bufs)
567 ;; Check if we should cancel the timer.
568 (when (and (not global-auto-revert-mode)
569 (null auto-revert-buffer-list))
570 (cancel-timer auto-revert-timer)
571 (setq auto-revert-timer nil)))))
574 ;; The end:
575 (provide 'autorevert)
577 (run-hooks 'auto-revert-load-hook)
579 ;;; autorevert.el ends here