Mercurial > emacs
annotate lisp/autorevert.el @ 24526:a70f2306f60b
(tcl-using-emacs-19-23): Recognize Emacs 20.
author | Tom Tromey <tromey@redhat.com> |
---|---|
date | Mon, 29 Mar 1999 07:35:55 +0000 |
parents | 2d0bece94ee7 |
children | 5d684a6517db |
rev | line source |
---|---|
22388 | 1 ;;; autorevert --- revert buffers when files on disk change |
18597 | 2 |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
3 ;; Copyright (C) 1997, 1998 Free Software Foundation, Inc. |
18597 | 4 |
5 ;; Author: Anders Lindgren <andersl@csd.uu.se> | |
22250
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
21289
diff
changeset
|
6 ;; Keywords: convenience |
18597 | 7 ;; Created: 1 Jun 1997 |
8 ;; Date: 3 Jul 1997 | |
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 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
32 ;; program the user normally has to execute the command `revert-buffer' |
18597 | 33 ;; to load the new content of the file into Emacs. |
34 ;; | |
35 ;; This package contains two minor modes: Global Auto-Revert Mode and | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
36 ;; Auto-Revert Mode. Both modes automatically revert buffers |
18597 | 37 ;; whenever the corresponding files have been changed on disk. |
38 ;; | |
39 ;; Auto-Revert Mode can be activated for individual buffers. | |
40 ;; Global Auto-Revert Mode applies to all file buffers. | |
41 ;; | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
42 ;; Both modes operate by checking the time stamp of all files at |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
43 ;; intervals of `auto-revert-interval'. The default is every five |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
44 ;; seconds. The check is aborted whenever the user actually uses |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
45 ;; Emacs. You should never even notice that this package is active |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
46 ;; (except that your buffers will be reverted, of course). |
18597 | 47 |
48 ;; Usage: | |
49 ;; | |
50 ;; Go to the appropriate buffer and press: | |
51 ;; M-x auto-revert-mode RET | |
52 ;; | |
53 ;; To activate Global Auto-Revert Mode, press: | |
54 ;; M-x global-auto-revert-mode RET | |
55 ;; | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
56 ;; To activate Global Auto-Revert Mode every time Emacs is started |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
57 ;; customise the option `global-auto-revert-mode' or the following |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
58 ;; line could be added to your ~/.emacs: |
18597 | 59 ;; (global-auto-revert-mode 1) |
60 ;; | |
61 ;; The function `turn-on-auto-revert-mode' could be added to any major | |
62 ;; mode hook to activate Auto-Revert Mode for all buffers in that | |
63 ;; mode. For example, the following line will activate Auto-Revert | |
64 ;; Mode in all C mode buffers: | |
65 ;; | |
66 ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode) | |
67 | |
68 ;;; Code: | |
69 | |
70 ;; Dependencies: | |
71 | |
72 (require 'timer) | |
73 (eval-when-compile (require 'cl)) | |
74 | |
75 | |
76 ;; Custom Group: | |
77 ;; | |
78 ;; The two modes will be placed next to Auto Save Mode under the | |
79 ;; Files group under Emacs. | |
80 | |
81 (defgroup auto-revert nil | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
82 "Revert individual buffers when files on disk change. |
18597 | 83 |
84 Auto-Revert Mode can be activated for individual buffer. | |
85 Global Auto-Revert Mode applies to all buffers." | |
22250
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
21289
diff
changeset
|
86 :group 'files |
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
21289
diff
changeset
|
87 :group 'convenience) |
18597 | 88 |
89 | |
90 ;; Variables: | |
91 | |
92 (defvar auto-revert-mode nil | |
93 "*Non-nil when Auto-Revert Mode is active. | |
94 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
95 Never set this variable directly, use the command `auto-revert-mode' |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
96 instead.") |
18597 | 97 |
98 (defcustom global-auto-revert-mode nil | |
99 "When on, buffers are automatically reverted when files on disk change. | |
100 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
101 Set this variable using \\[customize] only. Otherwise, use the |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
102 command `global-auto-revert-mode'." |
18597 | 103 :group 'auto-revert |
104 :initialize 'custom-initialize-default | |
105 :set '(lambda (symbol value) | |
106 (global-auto-revert-mode (or value 0))) | |
107 :type 'boolean | |
108 :require 'autorevert) | |
109 | |
110 (defcustom auto-revert-interval 5 | |
111 "Time, in seconds, between Auto-Revert Mode file checks." | |
112 :group 'auto-revert | |
113 :type 'integer) | |
114 | |
115 (defcustom auto-revert-stop-on-user-input t | |
116 "When non-nil Auto-Revert Mode stops checking files on user input." | |
117 :group 'auto-revert | |
118 :type 'boolean) | |
119 | |
120 (defcustom auto-revert-verbose t | |
121 "When nil, Auto-Revert Mode will not generate any messages. | |
122 | |
123 Currently, messages are generated when the mode is activated or | |
124 deactivated, and whenever a file is reverted." | |
125 :group 'auto-revert | |
126 :type 'boolean) | |
127 | |
128 (defcustom auto-revert-mode-text " ARev" | |
129 "String to display in the mode line when Auto-Revert Mode is active. | |
130 | |
131 \(When the string is not empty, make sure that it has a leading space.)" | |
132 :tag "Auto Revert Mode Text" ; To separate it from `global-...' | |
133 :group 'auto-revert | |
134 :type 'string) | |
135 | |
136 (defcustom auto-revert-mode-hook nil | |
137 "Functions to run when Auto-Revert Mode is activated." | |
138 :tag "Auto Revert Mode Hook" ; To separate it from `global-...' | |
139 :group 'auto-revert | |
140 :type 'hook) | |
141 | |
142 (defcustom global-auto-revert-mode-text "" | |
143 "String to display when Global Auto-Revert Mode is active. | |
144 | |
145 The default is nothing since when this mode is active this text doesn't | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
146 vary over time, or between buffers. Hence mode line text |
18597 | 147 would only waste precious space." |
148 :group 'auto-revert | |
149 :type 'string) | |
150 | |
151 (defcustom global-auto-revert-mode-hook nil | |
152 "Hook called when Global Auto-Revert Mode is activated." | |
153 :group 'auto-revert | |
154 :type 'hook) | |
155 | |
156 (defcustom global-auto-revert-non-file-buffers nil | |
157 "*When nil only file buffers are reverted by Global Auto-Revert Mode. | |
158 | |
159 When non-nil, both file buffers and buffers with a custom | |
160 `revert-buffer-function' are reverted by Global Auto-Revert Mode." | |
161 :group 'auto-revert | |
162 :type 'boolean) | |
163 | |
164 (defcustom global-auto-revert-non-file-buffers nil | |
165 "When nil only file buffers are reverted by Global Auto-Revert Mode. | |
166 | |
167 When non-nil, both file buffers and buffers with a custom | |
168 `revert-buffer-function' are reverted by Global Auto-Revert Mode. | |
169 | |
170 Use this option with care since it could lead to excessive reverts." | |
171 :group 'auto-revert | |
172 :type 'boolean) | |
173 | |
174 (defcustom global-auto-revert-ignore-modes '() | |
175 "List of major modes Global Auto-Revert Mode should not check." | |
176 :group 'auto-revert | |
177 :type '(repeat sexp)) | |
178 | |
179 (defcustom auto-revert-load-hook nil | |
180 "Functions to run when Auto-Revert Mode is first loaded." | |
181 :tag "Load Hook" | |
182 :group 'auto-revert | |
183 :type 'hook) | |
184 | |
185 (defvar global-auto-revert-ignore-buffer nil | |
20648
b644667dcd19
(global-auto-revert-ignore-buffer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
18597
diff
changeset
|
186 "*When non-nil, Global Auto-Revert Mode will not revert this buffer. |
18597 | 187 |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
188 This variable becomes buffer local when set in any fashion.") |
18597 | 189 (make-variable-buffer-local 'global-auto-revert-ignore-buffer) |
190 | |
191 | |
192 ;; Internal variables: | |
193 | |
194 (defvar auto-revert-buffer-list '() | |
195 "List of buffers in Auto-Revert Mode. | |
196 | |
197 Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds | |
198 buffers to this list. | |
199 | |
200 The timer function `auto-revert-buffers' is responsible for purging | |
201 the list of old buffers.") | |
202 | |
203 (defvar auto-revert-timer nil | |
204 "Timer used by Auto-Revert Mode.") | |
205 | |
206 (defvar auto-revert-remaining-buffers '() | |
207 "Buffers not checked when user input stopped execution.") | |
208 | |
209 | |
210 ;; Functions: | |
211 | |
212 ;;;###autoload | |
213 (defun auto-revert-mode (&optional arg) | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
214 "Toggle reverting buffer when file on disk changes. |
18597 | 215 |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
216 With arg, turn Auto Revert mode on if and only if arg is positive. |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
217 This is a minor mode that affects only the current buffer. |
18597 | 218 Use `global-auto-revert-mode' to automatically revert all buffers." |
219 (interactive "P") | |
220 (make-local-variable 'auto-revert-mode) | |
221 (setq auto-revert-mode | |
222 (if (null arg) | |
223 (not auto-revert-mode) | |
224 (> (prefix-numeric-value arg) 0))) | |
225 (if (and auto-revert-verbose | |
226 (interactive-p)) | |
227 (message "Auto-Revert Mode is now %s." | |
228 (if auto-revert-mode "on" "off"))) | |
229 (if auto-revert-mode | |
230 (if (not (memq (current-buffer) auto-revert-buffer-list)) | |
231 (push (current-buffer) auto-revert-buffer-list)) | |
232 (setq auto-revert-buffer-list | |
233 (delq (current-buffer) auto-revert-buffer-list))) | |
234 (auto-revert-set-timer) | |
235 (when auto-revert-mode | |
236 (auto-revert-buffers) | |
237 (run-hooks 'auto-revert-mode-hook))) | |
238 | |
239 | |
240 ;;;###autoload | |
241 (defun turn-on-auto-revert-mode () | |
242 "Turn on Auto-Revert Mode. | |
243 | |
244 This function is designed to be added to hooks, for example: | |
245 (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)" | |
246 (auto-revert-mode 1)) | |
247 | |
248 | |
249 ;;;###autoload | |
250 (defun global-auto-revert-mode (&optional arg) | |
251 "Revert any buffer when file on disk change. | |
252 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
253 With arg, turn Auto Revert mode on globally if and only if arg is positive. |
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
254 This is a minor mode that affects all buffers. |
18597 | 255 Use `auto-revert-mode' to revert a particular buffer." |
256 (interactive "P") | |
257 (setq global-auto-revert-mode | |
258 (if (null arg) | |
259 (not global-auto-revert-mode) | |
260 (> (prefix-numeric-value arg) 0))) | |
261 (if (and auto-revert-verbose | |
262 (interactive-p)) | |
20648
b644667dcd19
(global-auto-revert-ignore-buffer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
18597
diff
changeset
|
263 (message "Global Auto-Revert Mode is now %s." |
18597 | 264 (if global-auto-revert-mode "on" "off"))) |
265 (auto-revert-set-timer) | |
266 (when global-auto-revert-mode | |
267 (auto-revert-buffers) | |
268 (run-hooks 'global-auto-revert-mode-hook))) | |
269 | |
270 | |
271 (defun auto-revert-set-timer () | |
272 "Restart or cancel the timer." | |
273 (if (timerp auto-revert-timer) | |
274 (cancel-timer auto-revert-timer)) | |
275 (if (or global-auto-revert-mode auto-revert-buffer-list) | |
276 (setq auto-revert-timer (run-with-timer auto-revert-interval | |
277 auto-revert-interval | |
278 'auto-revert-buffers)) | |
279 (setq auto-revert-timer nil))) | |
280 | |
281 | |
282 (defun auto-revert-buffers () | |
283 "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode. | |
284 | |
285 Should `global-auto-revert-mode' be active all file buffers are checked. | |
286 | |
287 Should `auto-revert-mode' be active in some buffers, those buffers | |
288 are checked. | |
289 | |
290 Non-file buffers that have a custom `revert-buffer-function' are | |
291 reverted either when Auto-Revert Mode is active in that buffer, or | |
292 when the variable `global-auto-revert-non-file-buffers' is non-nil | |
293 and Global Auto-Revert Mode is active. | |
294 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
295 This function stops whenever there is user input. The buffers not |
18597 | 296 checked are stored in the variable `auto-revert-remaining-buffers'. |
297 | |
298 To avoid starvation, the buffers in `auto-revert-remaining-buffers' | |
299 are checked first the next time this function is called. | |
300 | |
21288
1b06a18f33fd
Various doc fixes, mainly grammar.
Dave Love <fx@gnu.org>
parents:
20648
diff
changeset
|
301 This function is also responsible for removing buffers no longer in |
18597 | 302 Auto-Revert mode from `auto-revert-buffer-list', and for canceling |
303 the timer when no buffers need to be checked." | |
304 (let ((bufs (if global-auto-revert-mode | |
305 (buffer-list) | |
306 auto-revert-buffer-list)) | |
307 (remaining '()) | |
308 (new '())) | |
309 ;; Partition `bufs' into two halves depending on whether or not | |
310 ;; the buffers are in `auto-revert-remaining-buffers'. The two | |
311 ;; halves are then re-joined with the "remaining" buffers at the | |
312 ;; head of the list. | |
313 (dolist (buf auto-revert-remaining-buffers) | |
314 (if (memq buf bufs) | |
315 (push buf remaining))) | |
316 (dolist (buf bufs) | |
317 (if (not (memq buf remaining)) | |
318 (push buf new))) | |
319 (setq bufs (nreverse (nconc new remaining))) | |
320 (while (and bufs | |
321 (not (and auto-revert-stop-on-user-input | |
322 (input-pending-p)))) | |
323 (let ((buf (car bufs))) | |
324 (if (buffer-name buf) ; Buffer still alive? | |
325 (save-excursion | |
326 (set-buffer buf) | |
327 ;; Test if someone has turned off Auto-Revert Mode in a | |
328 ;; non-standard way, for example by changing major mode. | |
329 (if (and (not auto-revert-mode) | |
330 (memq buf auto-revert-buffer-list)) | |
331 (setq auto-revert-buffer-list | |
332 (delq buf auto-revert-buffer-list))) | |
333 (when (and | |
334 (or auto-revert-mode | |
335 (and | |
336 global-auto-revert-mode | |
337 (not global-auto-revert-ignore-buffer) | |
338 (not (memq major-mode | |
339 global-auto-revert-ignore-modes)))) | |
340 (not (buffer-modified-p)) | |
341 (if (buffer-file-name) | |
342 (and (file-readable-p (buffer-file-name)) | |
343 (not (verify-visited-file-modtime buf))) | |
344 (and revert-buffer-function | |
345 (or (and global-auto-revert-mode | |
346 global-auto-revert-non-file-buffers) | |
347 auto-revert-mode)))) | |
348 (if auto-revert-verbose | |
349 (message "Reverting buffer `%s'." buf)) | |
350 (revert-buffer t t))) | |
351 ;; Remove dead buffer from `auto-revert-buffer-list'. | |
352 (setq auto-revert-buffer-list | |
353 (delq buf auto-revert-buffer-list)))) | |
354 (setq bufs (cdr bufs))) | |
355 (setq auto-revert-remaining-buffers bufs) | |
356 ;; Check if we should cancel the timer. | |
357 (when (and (not global-auto-revert-mode) | |
358 (null auto-revert-buffer-list)) | |
359 (cancel-timer auto-revert-timer) | |
360 (setq auto-revert-timer nil)))) | |
361 | |
362 | |
363 ;; The end: | |
364 | |
365 (unless (assq 'auto-revert-mode minor-mode-alist) | |
366 (push '(auto-revert-mode auto-revert-mode-text) | |
367 minor-mode-alist)) | |
368 (unless (assq 'global-auto-revert-mode minor-mode-alist) | |
369 (push '(global-auto-revert-mode global-auto-revert-mode-text) | |
370 minor-mode-alist)) | |
371 | |
372 (provide 'autorevert) | |
373 | |
374 (run-hooks 'auto-revert-load-hook) | |
375 | |
376 ;; This makes it possible to set Global Auto-Revert Mode from | |
377 ;; Customize. | |
378 (if global-auto-revert-mode | |
379 (global-auto-revert-mode 1)) | |
380 | |
381 ;; autorevert.el ends here. |