(help-at-pt-timer): Move defvar up to avoid compiler warning.
[bpt/emacs.git] / lisp / autorevert.el
CommitLineData
e8af40ee 1;;; autorevert.el --- revert buffers when files on disk change
4a35aff3 2
38f442c6 3;; Copyright (C) 1997, 1998, 1999, 2001, 2004 Free Software Foundation, Inc.
4a35aff3 4
a468671a 5;; Author: Anders Lindgren <andersl@andersl.com>
f5f727f8 6;; Keywords: convenience
a468671a
GM
7;; Created: 1997-06-01
8;; Date: 1999-11-30
4a35aff3
RS
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 2, or (at your option)
15;; 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; 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.
26
27;;; Commentary:
28
29;; Introduction:
30;;
31;; Whenever a file that Emacs is editing has been changed by another
48764ae2 32;; program the user normally has to execute the command `revert-buffer'
4a35aff3
RS
33;; to load the new content of the file into Emacs.
34;;
35;; This package contains two minor modes: Global Auto-Revert Mode and
48764ae2 36;; Auto-Revert Mode. Both modes automatically revert buffers
2d677766
LT
37;; whenever the corresponding files have been changed on disk and the
38;; buffer contains no unsaved changes.
4a35aff3 39;;
ae229809
LT
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.
4a35aff3 46;;
48764ae2
DL
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).
1f41bcba
LT
52;;
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.
2d677766
LT
63;;
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.)
4a35aff3
RS
70
71;; Usage:
72;;
dddc748b 73;; Go to the appropriate buffer and press either of:
4a35aff3 74;; M-x auto-revert-mode RET
dddc748b 75;; M-x auto-revert-tail-mode RET
4a35aff3
RS
76;;
77;; To activate Global Auto-Revert Mode, press:
78;; M-x global-auto-revert-mode RET
79;;
48764ae2
DL
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:
4a35aff3
RS
83;; (global-auto-revert-mode 1)
84;;
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:
89;;
90;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
91
92;;; Code:
93
94;; Dependencies:
95
96(require 'timer)
0f98bc23 97
71c8db4c 98(eval-when-compile (require 'cl))
4a35aff3
RS
99
100
101;; Custom Group:
102;;
103;; The two modes will be placed next to Auto Save Mode under the
104;; Files group under Emacs.
105
106(defgroup auto-revert nil
48764ae2 107 "Revert individual buffers when files on disk change.
4a35aff3
RS
108
109Auto-Revert Mode can be activated for individual buffer.
110Global Auto-Revert Mode applies to all buffers."
f5f727f8
DN
111 :group 'files
112 :group 'convenience)
4a35aff3
RS
113
114
115;; Variables:
116
dddc748b
DP
117;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
118;;; What's this?: ;;;###autoload
4a35aff3
RS
119(defvar auto-revert-mode nil
120 "*Non-nil when Auto-Revert Mode is active.
0e4f9468
SM
121Never set this variable directly, use the command `auto-revert-mode' instead.")
122(put 'auto-revert-mode 'permanent-local t)
4a35aff3 123
dddc748b
DP
124(defvar auto-revert-tail-mode nil
125 "*Non-nil when Auto-Revert Tail Mode is active.
126Never set this variable directly, use the command `auto-revert-mode' instead.")
127(put 'auto-revert-tail-mode 'permanent-local t)
128
a2ac68f1
LT
129(defvar auto-revert-timer nil
130 "Timer used by Auto-Revert Mode.")
131
4a35aff3 132(defcustom auto-revert-interval 5
ad660075 133 "Time, in seconds, between Auto-Revert Mode file checks.
a2ac68f1
LT
134The value may be an integer or floating point number.
135
136If a timer is already active, there are two ways to make sure
137that the new value will take effect immediately. You can set
138this variable through Custom or you can call the command
139`auto-revert-set-timer' after setting the variable. Otherwise,
140the new value will take effect the first time Auto Revert Mode
141calls `auto-revert-set-timer' for internal reasons or in your
142next editing session."
4a35aff3 143 :group 'auto-revert
a2ac68f1
LT
144 :type 'number
145 :set (lambda (variable value)
146 (set-default variable value)
147 (and (boundp 'auto-revert-timer)
148 auto-revert-timer
149 (auto-revert-set-timer))))
4a35aff3
RS
150
151(defcustom auto-revert-stop-on-user-input t
152 "When non-nil Auto-Revert Mode stops checking files on user input."
153 :group 'auto-revert
154 :type 'boolean)
155
156(defcustom auto-revert-verbose t
157 "When nil, Auto-Revert Mode will not generate any messages.
0e5dcfd7 158When non-nil, a message is generated whenever a file is reverted."
4a35aff3
RS
159 :group 'auto-revert
160 :type 'boolean)
161
162(defcustom auto-revert-mode-text " ARev"
163 "String to display in the mode line when Auto-Revert Mode is active.
164
165\(When the string is not empty, make sure that it has a leading space.)"
166 :tag "Auto Revert Mode Text" ; To separate it from `global-...'
167 :group 'auto-revert
168 :type 'string)
169
dddc748b
DP
170(defcustom auto-revert-tail-mode-text " Tail"
171 "String to display in the mode line when Auto-Revert Tail Mode is active.
172
173\(When the string is not empty, make sure that it has a leading space.)"
174 :group 'auto-revert
175 :type 'string)
176
4a35aff3
RS
177(defcustom auto-revert-mode-hook nil
178 "Functions to run when Auto-Revert Mode is activated."
179 :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
180 :group 'auto-revert
181 :type 'hook)
182
183(defcustom global-auto-revert-mode-text ""
184 "String to display when Global Auto-Revert Mode is active.
185
186The default is nothing since when this mode is active this text doesn't
48764ae2 187vary over time, or between buffers. Hence mode line text
4a35aff3
RS
188would only waste precious space."
189 :group 'auto-revert
190 :type 'string)
191
192(defcustom global-auto-revert-mode-hook nil
193 "Hook called when Global Auto-Revert Mode is activated."
194 :group 'auto-revert
195 :type 'hook)
196
197(defcustom global-auto-revert-non-file-buffers nil
d9e4328d 198 "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
4a35aff3
RS
199
200When non-nil, both file buffers and buffers with a custom
0e5dcfd7 201`revert-buffer-function' and a `buffer-stale-function' are
9ae0d84f
LT
202reverted by Global Auto-Revert mode. These include the Buffer
203List buffer, and Dired buffers showing complete local
204directories. Dired buffers do not auto-revert as a result of
205changes in subdirectories or in the contents, size, modes, etc.,
206of files. You may still sometimes want to revert them manually.
0e4f9468 207
d9e4328d 208Use this option with care since it could lead to excessive auto-reverts.
9ae0d84f 209For more information, see Info node `(emacs-xtra)Autorevert'."
4a35aff3 210 :group 'auto-revert
843c51ae
LT
211 :type 'boolean
212 :link '(info-link "(emacs-xtra)Autorevert"))
4a35aff3 213
dddc748b 214(defcustom global-auto-revert-ignore-modes ()
4a35aff3
RS
215 "List of major modes Global Auto-Revert Mode should not check."
216 :group 'auto-revert
217 :type '(repeat sexp))
218
219(defcustom auto-revert-load-hook nil
220 "Functions to run when Auto-Revert Mode is first loaded."
221 :tag "Load Hook"
222 :group 'auto-revert
223 :type 'hook)
224
71c8db4c
LT
225(defcustom auto-revert-check-vc-info nil
226 "If non-nil Auto Revert Mode reliably updates version control info.
227Auto Revert Mode updates version control info whenever the buffer
228needs reverting, regardless of the value of this variable.
229However, the version control state can change without changes to
230the work file. If the change is made from the current Emacs
231session, all info is updated. But if, for instance, a new
232version is checked in from outside the current Emacs session, the
233version control number in the mode line, as well as other version
234control related information, may not be properly updated. If you
235are worried about this, set this variable to a non-nil value.
236
237This currently works by automatically updating the version
238control info every `auto-revert-interval' seconds. Nevertheless,
239it should not cause excessive CPU usage on a reasonably fast
240machine, if it does not apply to too many version controlled
241buffers. CPU usage depends on the version control system"
242 :group 'auto-revert
243 :type 'boolean
244 :version "21.4")
245
4a35aff3 246(defvar global-auto-revert-ignore-buffer nil
f1e3ff80 247 "*When non-nil, Global Auto-Revert Mode will not revert this buffer.
4a35aff3 248
48764ae2 249This variable becomes buffer local when set in any fashion.")
4a35aff3
RS
250(make-variable-buffer-local 'global-auto-revert-ignore-buffer)
251
4a35aff3
RS
252;; Internal variables:
253
dddc748b 254(defvar auto-revert-buffer-list ()
4a35aff3
RS
255 "List of buffers in Auto-Revert Mode.
256
257Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
258buffers to this list.
259
260The timer function `auto-revert-buffers' is responsible for purging
261the list of old buffers.")
262
dddc748b 263(defvar auto-revert-remaining-buffers ()
4a35aff3
RS
264 "Buffers not checked when user input stopped execution.")
265
dddc748b
DP
266(defvar auto-revert-tail-pos 0
267 "Position of last known end of file.")
268
269(add-hook 'find-file-hook
270 (lambda ()
271 (set (make-local-variable 'auto-revert-tail-pos)
272 (save-restriction (widen) (1- (point-max))))))
4a35aff3
RS
273
274;; Functions:
275
276;;;###autoload
0e4f9468 277(define-minor-mode auto-revert-mode
48764ae2 278 "Toggle reverting buffer when file on disk changes.
4a35aff3 279
48764ae2
DL
280With arg, turn Auto Revert mode on if and only if arg is positive.
281This is a minor mode that affects only the current buffer.
dddc748b
DP
282Use `global-auto-revert-mode' to automatically revert all buffers.
283Use `auto-revert-tail-mode' if you know that the file will only grow
284without being changed in the part that is already in the buffer."
0e4f9468 285 nil auto-revert-mode-text nil
4a35aff3
RS
286 (if auto-revert-mode
287 (if (not (memq (current-buffer) auto-revert-buffer-list))
288 (push (current-buffer) auto-revert-buffer-list))
289 (setq auto-revert-buffer-list
290 (delq (current-buffer) auto-revert-buffer-list)))
291 (auto-revert-set-timer)
292 (when auto-revert-mode
dddc748b
DP
293 (auto-revert-buffers)
294 (setq auto-revert-tail-mode nil)))
4a35aff3
RS
295
296
297;;;###autoload
298(defun turn-on-auto-revert-mode ()
299 "Turn on Auto-Revert Mode.
300
301This function is designed to be added to hooks, for example:
302 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
303 (auto-revert-mode 1))
304
305
dddc748b
DP
306;;;###autoload
307(define-minor-mode auto-revert-tail-mode
308 "Toggle reverting tail of buffer when file on disk grows.
309With arg, turn Tail mode on iff arg is positive.
310
311When Tail mode is enabled, the tail of the file is constantly
312followed, as with the shell command `tail -f'. This means that
313whenever the file grows on disk (presumably because some
314background process is appending to it from time to time), this is
315reflected in the current buffer.
316
317You can edit the buffer and turn this mode off and on again as
318you please. But make sure the background process has stopped
319writing before you save the file!
320
321Use `auto-revert-mode' for changes other than appends!"
322 :group 'find-file :lighter auto-revert-tail-mode-text
323 (when auto-revert-tail-mode
324 (unless buffer-file-name
325 (auto-revert-tail-mode 0)
326 (error "This buffer is not visiting a file"))
327 (if (and (buffer-modified-p)
328 (not auto-revert-tail-pos) ; library was loaded only after finding file
329 (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
330 (auto-revert-tail-mode 0)
331 ;; else we might reappend our own end when we save
332 (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
333 (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
334 (set (make-variable-buffer-local 'auto-revert-tail-pos)
335 (save-restriction (widen) (1- (point-max)))))
336 ;; let auto-revert-mode set up the mechanism for us if it isn't already
337 (or auto-revert-mode
338 (let ((auto-revert-tail-mode t))
339 (auto-revert-mode 1)))
340 (setq auto-revert-mode nil))))
341
342
343;;;###autoload
344(defun turn-on-auto-revert-tail-mode ()
345 "Turn on Auto-Revert Tail Mode.
346
347This function is designed to be added to hooks, for example:
348 (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
349 (auto-revert-tail-mode 1))
350
351
4a35aff3 352;;;###autoload
0e4f9468 353(define-minor-mode global-auto-revert-mode
0e5dcfd7 354 "Revert any buffer when file on disk changes.
4a35aff3 355
48764ae2
DL
356With arg, turn Auto Revert mode on globally if and only if arg is positive.
357This is a minor mode that affects all buffers.
4a35aff3 358Use `auto-revert-mode' to revert a particular buffer."
0e4f9468 359 :global t :group 'auto-revert :lighter global-auto-revert-mode-text
4a35aff3
RS
360 (auto-revert-set-timer)
361 (when global-auto-revert-mode
0e4f9468 362 (auto-revert-buffers)))
4a35aff3
RS
363
364
365(defun auto-revert-set-timer ()
0e5dcfd7 366 "Restart or cancel the timer used by Auto-Revert Mode.
d97c8375 367If such a timer is active, cancel it. Start a new timer if
0e5dcfd7
LT
368Global Auto-Revert Mode is active or if Auto-Revert Mode is active
369in some buffer. Restarting the timer ensures that Auto-Revert Mode
370will use an up-to-date value of `auto-revert-interval'"
a2ac68f1 371 (interactive)
4a35aff3
RS
372 (if (timerp auto-revert-timer)
373 (cancel-timer auto-revert-timer))
0e4f9468
SM
374 (setq auto-revert-timer
375 (if (or global-auto-revert-mode auto-revert-buffer-list)
376 (run-with-timer auto-revert-interval
377 auto-revert-interval
dddc748b 378 'auto-revert-buffers))))
4a35aff3 379
ed35db71
EZ
380(defun auto-revert-active-p ()
381 "Check if auto-revert is active (in current buffer or globally)."
382 (or auto-revert-mode
dddc748b 383 auto-revert-tail-mode
ed35db71
EZ
384 (and
385 global-auto-revert-mode
386 (not global-auto-revert-ignore-buffer)
387 (not (memq major-mode
388 global-auto-revert-ignore-modes)))))
389
ed35db71 390(defun auto-revert-handler ()
0e5dcfd7
LT
391 "Revert current buffer, if appropriate.
392This is an internal function used by Auto-Revert Mode."
dddc748b
DP
393 (when (or auto-revert-tail-mode (not (buffer-modified-p)))
394 (let* ((buffer (current-buffer))
395 (revert
396 (or (and buffer-file-name
397 (not (file-remote-p buffer-file-name))
398 (file-readable-p buffer-file-name)
399 (not (verify-visited-file-modtime buffer)))
2d677766 400 (and (or auto-revert-mode
dddc748b
DP
401 global-auto-revert-non-file-buffers)
402 revert-buffer-function
403 (boundp 'buffer-stale-function)
404 (functionp buffer-stale-function)
405 (funcall buffer-stale-function t))))
406 eob eoblist)
d4411cef 407 (when revert
71c8db4c
LT
408 (when (and auto-revert-verbose
409 (not (eq revert 'fast)))
633e0363 410 (message "Reverting buffer `%s'." (buffer-name)))
1f41bcba
LT
411 ;; If point (or a window point) is at the end of the buffer,
412 ;; we want to keep it at the end after reverting. This allows
413 ;; to tail a file.
414 (when buffer-file-name
415 (setq eob (eobp))
416 (walk-windows
417 #'(lambda (window)
418 (and (eq (window-buffer window) buffer)
419 (= (window-point window) (point-max))
420 (push window eoblist)))
421 'no-mini t))
dddc748b
DP
422 (if auto-revert-tail-mode
423 (auto-revert-tail-handler)
7ebc19f9
RS
424 ;; Bind buffer-read-only in case user has done C-x C-q,
425 ;; so as not to forget that. This gives undesirable results
426 ;; when the file's mode changes, but that is less common.
90e118ab
LT
427 (let ((buffer-read-only buffer-read-only))
428 (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
1f41bcba
LT
429 (when buffer-file-name
430 (when eob (goto-char (point-max)))
431 (dolist (window eoblist)
432 (set-window-point window (point-max)))))
71c8db4c
LT
433 ;; `preserve-modes' avoids changing the (minor) modes. But we
434 ;; do want to reset the mode for VC, so we do it manually.
435 (when (or revert auto-revert-check-vc-info)
436 (vc-find-file-hook)))))
ed35db71 437
dddc748b
DP
438(defun auto-revert-tail-handler ()
439 (let ((size (nth 7 (file-attributes buffer-file-name)))
440 (modified (buffer-modified-p))
441 buffer-read-only ; ignore
442 (file buffer-file-name)
443 buffer-file-name) ; ignore that file has changed
444 (when (> size auto-revert-tail-pos)
445 (save-restriction
446 (widen)
447 (save-excursion
448 (goto-char (point-max))
449 (insert-file-contents file nil auto-revert-tail-pos size)))
450 (setq auto-revert-tail-pos size)
451 (set-buffer-modified-p modified)))
452 (set-visited-file-modtime))
453
4a35aff3
RS
454(defun auto-revert-buffers ()
455 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
456
457Should `global-auto-revert-mode' be active all file buffers are checked.
458
459Should `auto-revert-mode' be active in some buffers, those buffers
460are checked.
461
0e5dcfd7
LT
462Non-file buffers that have a custom `revert-buffer-function' and
463a `buffer-stale-function' are reverted either when Auto-Revert
464Mode is active in that buffer, or when the variable
465`global-auto-revert-non-file-buffers' is non-nil and Global
466Auto-Revert Mode is active.
4a35aff3 467
48764ae2 468This function stops whenever there is user input. The buffers not
4a35aff3
RS
469checked are stored in the variable `auto-revert-remaining-buffers'.
470
471To avoid starvation, the buffers in `auto-revert-remaining-buffers'
472are checked first the next time this function is called.
473
48764ae2 474This function is also responsible for removing buffers no longer in
4a35aff3
RS
475Auto-Revert mode from `auto-revert-buffer-list', and for canceling
476the timer when no buffers need to be checked."
477 (let ((bufs (if global-auto-revert-mode
478 (buffer-list)
479 auto-revert-buffer-list))
dddc748b
DP
480 (remaining ())
481 (new ()))
4a35aff3
RS
482 ;; Partition `bufs' into two halves depending on whether or not
483 ;; the buffers are in `auto-revert-remaining-buffers'. The two
484 ;; halves are then re-joined with the "remaining" buffers at the
485 ;; head of the list.
486 (dolist (buf auto-revert-remaining-buffers)
487 (if (memq buf bufs)
488 (push buf remaining)))
489 (dolist (buf bufs)
490 (if (not (memq buf remaining))
491 (push buf new)))
492 (setq bufs (nreverse (nconc new remaining)))
493 (while (and bufs
494 (not (and auto-revert-stop-on-user-input
495 (input-pending-p))))
496 (let ((buf (car bufs)))
497 (if (buffer-name buf) ; Buffer still alive?
0e4f9468 498 (with-current-buffer buf
4a35aff3
RS
499 ;; Test if someone has turned off Auto-Revert Mode in a
500 ;; non-standard way, for example by changing major mode.
501 (if (and (not auto-revert-mode)
dddc748b 502 (not auto-revert-tail-mode)
4a35aff3
RS
503 (memq buf auto-revert-buffer-list))
504 (setq auto-revert-buffer-list
505 (delq buf auto-revert-buffer-list)))
c86afc19 506 (when (auto-revert-active-p) (auto-revert-handler)))
4a35aff3
RS
507 ;; Remove dead buffer from `auto-revert-buffer-list'.
508 (setq auto-revert-buffer-list
509 (delq buf auto-revert-buffer-list))))
510 (setq bufs (cdr bufs)))
511 (setq auto-revert-remaining-buffers bufs)
512 ;; Check if we should cancel the timer.
513 (when (and (not global-auto-revert-mode)
514 (null auto-revert-buffer-list))
515 (cancel-timer auto-revert-timer)
516 (setq auto-revert-timer nil))))
517
518
519;; The end:
4a35aff3
RS
520(provide 'autorevert)
521
522(run-hooks 'auto-revert-load-hook)
523
ab5796a9 524;;; arch-tag: f6bcb07b-4841-477e-9e44-b18678e58876
e8af40ee 525;;; autorevert.el ends here