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