(ps-default-fg, ps-default-bg): Fix typos in docstrings.
[emacs.git] / lisp / autorevert.el
blob1769a992f7b3d974dd2343b80f63c64551e1753d
1 ;;; autorevert.el --- revert buffers when files on disk change
3 ;; Copyright (C) 1997, 1998, 1999, 2001, 2004, 2005 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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Introduction:
31 ;; Whenever a file that Emacs is editing has been changed by another
32 ;; program the user normally has to execute the command `revert-buffer'
33 ;; to load the new content of the file into Emacs.
35 ;; This package contains two minor modes: Global Auto-Revert Mode and
36 ;; Auto-Revert Mode. Both modes automatically revert buffers
37 ;; whenever the corresponding files have been changed on disk and the
38 ;; buffer contains no unsaved changes.
40 ;; Auto-Revert Mode can be activated for individual buffers. Global
41 ;; Auto-Revert Mode applies to all file buffers. (If the user option
42 ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies
43 ;; to some non-file buffers. This option is disabled by default.)
44 ;; Since checking a remote file is too slow, these modes do not check
45 ;; or revert remote files.
47 ;; Both modes operate by checking the time stamp of all files at
48 ;; intervals of `auto-revert-interval'. The default is every five
49 ;; seconds. The check is aborted whenever the user actually uses
50 ;; Emacs. You should never even notice that this package is active
51 ;; (except that your buffers will be reverted, of course).
53 ;; After reverting a file buffer, Auto Revert Mode normally puts point
54 ;; at the same position that a regular manual revert would. However,
55 ;; there is one exception to this rule. If point is at the end of the
56 ;; buffer before reverting, it stays at the end. Similarly if point
57 ;; is displayed at the end of a file buffer in any window, it will stay
58 ;; at the end of the buffer in that window, even if the window is not
59 ;; selected. This way, you can use Auto Revert Mode to `tail' a file.
60 ;; Just put point at the end of the buffer and it will stay there.
61 ;; These rules apply to file buffers. For non-file buffers, the
62 ;; behavior may be mode dependent.
64 ;; While you can use Auto Revert Mode to tail a file, this package
65 ;; contains a third minor mode, Auto Revert Tail Mode, which does so
66 ;; more efficiently, as long as you are sure that the file will only
67 ;; change by growing at the end. It only appends the new output,
68 ;; instead of reverting the entire buffer. It does so even if the
69 ;; buffer contains unsaved changes. (Because they will not be lost.)
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, and Dired buffers showing complete local
212 directories. Dired buffers do not auto-revert as a result of
213 changes in subdirectories or in the contents, size, modes, etc.,
214 of files. You may still sometimes want to revert them manually.
216 Use this option with care since it could lead to excessive auto-reverts.
217 For more information, see Info node `(emacs-xtra)Autorevert'."
218 :group 'auto-revert
219 :type 'boolean
220 :link '(info-link "(emacs-xtra)Autorevert"))
222 (defcustom global-auto-revert-ignore-modes ()
223 "List of major modes Global Auto-Revert Mode should not check."
224 :group 'auto-revert
225 :type '(repeat sexp))
227 (defcustom auto-revert-load-hook nil
228 "Functions to run when Auto-Revert Mode is first loaded."
229 :tag "Load Hook"
230 :group 'auto-revert
231 :type 'hook)
233 (defcustom auto-revert-check-vc-info nil
234 "If non-nil Auto Revert Mode reliably updates version control info.
235 Auto Revert Mode updates version control info whenever the buffer
236 needs reverting, regardless of the value of this variable.
237 However, the version control state can change without changes to
238 the work file. If the change is made from the current Emacs
239 session, all info is updated. But if, for instance, a new
240 version is checked in from outside the current Emacs session, the
241 version control number in the mode line, as well as other version
242 control related information, may not be properly updated. If you
243 are worried about this, set this variable to a non-nil value.
245 This currently works by automatically updating the version
246 control info every `auto-revert-interval' seconds. Nevertheless,
247 it should not cause excessive CPU usage on a reasonably fast
248 machine, if it does not apply to too many version controlled
249 buffers. CPU usage depends on the version control system."
250 :group 'auto-revert
251 :type 'boolean
252 :version "22.1")
254 (defvar global-auto-revert-ignore-buffer nil
255 "*When non-nil, Global Auto-Revert Mode will not revert this buffer.
257 This variable becomes buffer local when set in any fashion.")
258 (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
260 ;; Internal variables:
262 (defvar auto-revert-buffer-list ()
263 "List of buffers in Auto-Revert Mode.
265 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
266 buffers to this list.
268 The timer function `auto-revert-buffers' is responsible for purging
269 the list of old buffers.")
271 (defvar auto-revert-remaining-buffers ()
272 "Buffers not checked when user input stopped execution.")
274 (defvar auto-revert-tail-pos 0
275 "Position of last known end of file.")
277 (add-hook 'find-file-hook
278 (lambda ()
279 (set (make-local-variable 'auto-revert-tail-pos)
280 (save-restriction (widen) (1- (point-max))))))
282 ;; Functions:
284 ;;;###autoload
285 (define-minor-mode auto-revert-mode
286 "Toggle reverting buffer when file on disk changes.
288 With arg, turn Auto Revert mode on if and only if arg is positive.
289 This is a minor mode that affects only the current buffer.
290 Use `global-auto-revert-mode' to automatically revert all buffers.
291 Use `auto-revert-tail-mode' if you know that the file will only grow
292 without being changed in the part that is already in the buffer."
293 :group 'auto-revert :lighter auto-revert-mode-text
294 (if auto-revert-mode
295 (if (not (memq (current-buffer) auto-revert-buffer-list))
296 (push (current-buffer) auto-revert-buffer-list))
297 (setq auto-revert-buffer-list
298 (delq (current-buffer) auto-revert-buffer-list)))
299 (auto-revert-set-timer)
300 (when auto-revert-mode
301 (auto-revert-buffers)
302 (setq auto-revert-tail-mode nil)))
305 ;;;###autoload
306 (defun turn-on-auto-revert-mode ()
307 "Turn on Auto-Revert Mode.
309 This function is designed to be added to hooks, for example:
310 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
311 (auto-revert-mode 1))
314 ;;;###autoload
315 (define-minor-mode auto-revert-tail-mode
316 "Toggle reverting tail of buffer when file on disk grows.
317 With arg, turn Tail mode on iff arg is positive.
319 When Tail mode is enabled, the tail of the file is constantly
320 followed, as with the shell command `tail -f'. This means that
321 whenever the file grows on disk (presumably because some
322 background process is appending to it from time to time), this is
323 reflected in the current buffer.
325 You can edit the buffer and turn this mode off and on again as
326 you please. But make sure the background process has stopped
327 writing before you save the file!
329 Use `auto-revert-mode' for changes other than appends!"
330 :group 'find-file :lighter auto-revert-tail-mode-text
331 (when auto-revert-tail-mode
332 (unless buffer-file-name
333 (auto-revert-tail-mode 0)
334 (error "This buffer is not visiting a file"))
335 (if (and (buffer-modified-p)
336 (not auto-revert-tail-pos) ; library was loaded only after finding file
337 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
338 (auto-revert-tail-mode 0)
339 ;; else we might reappend our own end when we save
340 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
341 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
342 (set (make-variable-buffer-local 'auto-revert-tail-pos)
343 (save-restriction (widen) (1- (point-max)))))
344 ;; let auto-revert-mode set up the mechanism for us if it isn't already
345 (or auto-revert-mode
346 (let ((auto-revert-tail-mode t))
347 (auto-revert-mode 1)))
348 (setq auto-revert-mode nil))))
351 ;;;###autoload
352 (defun turn-on-auto-revert-tail-mode ()
353 "Turn on Auto-Revert Tail Mode.
355 This function is designed to be added to hooks, for example:
356 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
357 (auto-revert-tail-mode 1))
360 ;;;###autoload
361 (define-minor-mode global-auto-revert-mode
362 "Revert any buffer when file on disk changes.
364 With arg, turn Auto Revert mode on globally if and only if arg is positive.
365 This is a minor mode that affects all buffers.
366 Use `auto-revert-mode' to revert a particular buffer."
367 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
368 (auto-revert-set-timer)
369 (when global-auto-revert-mode
370 (auto-revert-buffers)))
373 (defun auto-revert-set-timer ()
374 "Restart or cancel the timer used by Auto-Revert Mode.
375 If such a timer is active, cancel it. Start a new timer if
376 Global Auto-Revert Mode is active or if Auto-Revert Mode is active
377 in some buffer. Restarting the timer ensures that Auto-Revert Mode
378 will use an up-to-date value of `auto-revert-interval'"
379 (interactive)
380 (if (timerp auto-revert-timer)
381 (cancel-timer auto-revert-timer))
382 (setq auto-revert-timer
383 (if (or global-auto-revert-mode auto-revert-buffer-list)
384 (run-with-timer auto-revert-interval
385 auto-revert-interval
386 'auto-revert-buffers))))
388 (defun auto-revert-active-p ()
389 "Check if auto-revert is active (in current buffer or globally)."
390 (or auto-revert-mode
391 auto-revert-tail-mode
392 (and
393 global-auto-revert-mode
394 (not global-auto-revert-ignore-buffer)
395 (not (memq major-mode
396 global-auto-revert-ignore-modes)))))
398 (defun auto-revert-handler ()
399 "Revert current buffer, if appropriate.
400 This is an internal function used by Auto-Revert Mode."
401 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
402 (let* ((buffer (current-buffer))
403 (revert
404 (or (and buffer-file-name
405 (not (file-remote-p buffer-file-name))
406 (file-readable-p buffer-file-name)
407 (not (verify-visited-file-modtime buffer)))
408 (and (or auto-revert-mode
409 global-auto-revert-non-file-buffers)
410 revert-buffer-function
411 (boundp 'buffer-stale-function)
412 (functionp buffer-stale-function)
413 (funcall buffer-stale-function t))))
414 eob eoblist)
415 (when revert
416 (when (and auto-revert-verbose
417 (not (eq revert 'fast)))
418 (message "Reverting buffer `%s'." (buffer-name)))
419 ;; If point (or a window point) is at the end of the buffer,
420 ;; we want to keep it at the end after reverting. This allows
421 ;; to tail a file.
422 (when buffer-file-name
423 (setq eob (eobp))
424 (walk-windows
425 #'(lambda (window)
426 (and (eq (window-buffer window) buffer)
427 (= (window-point window) (point-max))
428 (push window eoblist)))
429 'no-mini t))
430 (if auto-revert-tail-mode
431 (auto-revert-tail-handler)
432 ;; Bind buffer-read-only in case user has done C-x C-q,
433 ;; so as not to forget that. This gives undesirable results
434 ;; when the file's mode changes, but that is less common.
435 (let ((buffer-read-only buffer-read-only))
436 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
437 (when buffer-file-name
438 (when eob (goto-char (point-max)))
439 (dolist (window eoblist)
440 (set-window-point window (point-max)))))
441 ;; `preserve-modes' avoids changing the (minor) modes. But we
442 ;; do want to reset the mode for VC, so we do it manually.
443 (when (or revert auto-revert-check-vc-info)
444 (vc-find-file-hook)))))
446 (defun auto-revert-tail-handler ()
447 (let ((size (nth 7 (file-attributes buffer-file-name)))
448 (modified (buffer-modified-p))
449 buffer-read-only ; ignore
450 (file buffer-file-name)
451 buffer-file-name) ; ignore that file has changed
452 (when (> size auto-revert-tail-pos)
453 (undo-boundary)
454 (save-restriction
455 (widen)
456 (save-excursion
457 (goto-char (point-max))
458 (insert-file-contents file nil auto-revert-tail-pos size)))
459 (undo-boundary)
460 (setq auto-revert-tail-pos size)
461 (set-buffer-modified-p modified)))
462 (set-visited-file-modtime))
464 (defun auto-revert-buffers ()
465 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
467 Should `global-auto-revert-mode' be active all file buffers are checked.
469 Should `auto-revert-mode' be active in some buffers, those buffers
470 are checked.
472 Non-file buffers that have a custom `revert-buffer-function' and
473 a `buffer-stale-function' are reverted either when Auto-Revert
474 Mode is active in that buffer, or when the variable
475 `global-auto-revert-non-file-buffers' is non-nil and Global
476 Auto-Revert Mode is active.
478 This function stops whenever there is user input. The buffers not
479 checked are stored in the variable `auto-revert-remaining-buffers'.
481 To avoid starvation, the buffers in `auto-revert-remaining-buffers'
482 are checked first the next time this function is called.
484 This function is also responsible for removing buffers no longer in
485 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
486 the timer when no buffers need to be checked."
487 (save-match-data
488 (let ((bufs (if global-auto-revert-mode
489 (buffer-list)
490 auto-revert-buffer-list))
491 (remaining ())
492 (new ()))
493 ;; Partition `bufs' into two halves depending on whether or not
494 ;; the buffers are in `auto-revert-remaining-buffers'. The two
495 ;; halves are then re-joined with the "remaining" buffers at the
496 ;; head of the list.
497 (dolist (buf auto-revert-remaining-buffers)
498 (if (memq buf bufs)
499 (push buf remaining)))
500 (dolist (buf bufs)
501 (if (not (memq buf remaining))
502 (push buf new)))
503 (setq bufs (nreverse (nconc new remaining)))
504 (while (and bufs
505 (not (and auto-revert-stop-on-user-input
506 (input-pending-p))))
507 (let ((buf (car bufs)))
508 (if (buffer-name buf) ; Buffer still alive?
509 (with-current-buffer buf
510 ;; Test if someone has turned off Auto-Revert Mode in a
511 ;; non-standard way, for example by changing major mode.
512 (if (and (not auto-revert-mode)
513 (not auto-revert-tail-mode)
514 (memq buf auto-revert-buffer-list))
515 (setq auto-revert-buffer-list
516 (delq buf auto-revert-buffer-list)))
517 (when (auto-revert-active-p) (auto-revert-handler)))
518 ;; Remove dead buffer from `auto-revert-buffer-list'.
519 (setq auto-revert-buffer-list
520 (delq buf auto-revert-buffer-list))))
521 (setq bufs (cdr bufs)))
522 (setq auto-revert-remaining-buffers bufs)
523 ;; Check if we should cancel the timer.
524 (when (and (not global-auto-revert-mode)
525 (null auto-revert-buffer-list))
526 (cancel-timer auto-revert-timer)
527 (setq auto-revert-timer nil)))))
530 ;; The end:
531 (provide 'autorevert)
533 (run-hooks 'auto-revert-load-hook)
535 ;;; arch-tag: f6bcb07b-4841-477e-9e44-b18678e58876
536 ;;; autorevert.el ends here