~vcs-imports/emacs/trunk

38421 by Pavel Janík
1
;;; autorevert.el --- revert buffers when files on disk change
18598 by Richard M. Stallman
Initial revision
2
115824 by Paul Eggert
Update copyright year to 2014 by running admin/update-copyright.
3
;; Copyright (C) 1997-1999, 2001-2014 Free Software Foundation, Inc.
18598 by Richard M. Stallman
Initial revision
4
26674 by Gerd Moellmann
(auto-revert-buffers): Auto-revert mode was turned
5
;; Author: Anders Lindgren <andersl@andersl.com>
22251 by Dan Nicolaescu
*** empty log message ***
6
;; Keywords: convenience
26674 by Gerd Moellmann
(auto-revert-buffers): Auto-revert mode was turned
7
;; Created: 1997-06-01
8
;; Date: 1999-11-30
18598 by Richard M. Stallman
Initial revision
9
10
;; This file is part of GNU Emacs.
11
87346 by Glenn Morris
Switch to recommended form of GPLv3 permissions notice.
12
;; GNU Emacs is free software: you can redistribute it and/or modify
18598 by Richard M. Stallman
Initial revision
13
;; it under the terms of the GNU General Public License as published by
87346 by Glenn Morris
Switch to recommended form of GPLv3 permissions notice.
14
;; the Free Software Foundation, either version 3 of the License, or
15
;; (at your option) any later version.
18598 by Richard M. Stallman
Initial revision
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
87346 by Glenn Morris
Switch to recommended form of GPLv3 permissions notice.
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
18598 by Richard M. Stallman
Initial revision
24
25
;;; Commentary:
26
27
;; Introduction:
28
;;
29
;; Whenever a file that Emacs is editing has been changed by another
21289 by Dave Love
Various doc fixes, mainly grammar.
30
;; program the user normally has to execute the command `revert-buffer'
18598 by Richard M. Stallman
Initial revision
31
;; to load the new content of the file into Emacs.
32
;;
33
;; This package contains two minor modes: Global Auto-Revert Mode and
21289 by Dave Love
Various doc fixes, mainly grammar.
34
;; Auto-Revert Mode.  Both modes automatically revert buffers
49780.1.32 by Henrik Enberg
sync with trunk
35
;; whenever the corresponding files have been changed on disk and the
36
;; buffer contains no unsaved changes.
18598 by Richard M. Stallman
Initial revision
37
;;
49780.1.32 by Henrik Enberg
sync with trunk
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.)
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
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.
18598 by Richard M. Stallman
Initial revision
47
;;
21289 by Dave Love
Various doc fixes, mainly grammar.
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).
49780.1.32 by Henrik Enberg
sync with trunk
53
;;
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
54
;; If Emacs is compiled with file notification support, notifications
55
;; are used instead of checking the time stamp of the files.  You can
111477 by Michael Albinus
* autorevert.el (auto-revert-notify-enabled): Move up.
56
;; disable this by setting the user option `auto-revert-use-notify' to
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
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'.
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
60
;;
49780.1.32 by Henrik Enberg
sync with trunk
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.
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.)
18598 by Richard M. Stallman
Initial revision
78
79
;; Usage:
80
;;
49780.1.32 by Henrik Enberg
sync with trunk
81
;; Go to the appropriate buffer and press either of:
18598 by Richard M. Stallman
Initial revision
82
;;   M-x auto-revert-mode RET
49780.1.32 by Henrik Enberg
sync with trunk
83
;;   M-x auto-revert-tail-mode RET
18598 by Richard M. Stallman
Initial revision
84
;;
85
;; To activate Global Auto-Revert Mode, press:
86
;;   M-x global-auto-revert-mode RET
87
;;
21289 by Dave Love
Various doc fixes, mainly grammar.
88
;; To activate Global Auto-Revert Mode every time Emacs is started
106439 by Paul Eggert
Spelling fixes.
89
;; customize the option `global-auto-revert-mode' or the following
21289 by Dave Love
Various doc fixes, mainly grammar.
90
;; line could be added to your ~/.emacs:
18598 by Richard M. Stallman
Initial revision
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
111495 by Stefan Monnier
* lisp/autorevert.el: Use cl-lib instead of cl.
104
(eval-when-compile (require 'cl-lib))
18598 by Richard M. Stallman
Initial revision
105
(require 'timer)
113280 by Michael Albinus
* filenotify.el: New package.
106
(require 'filenotify)
53947 by Eli Zaretskii
(auto-revert-dired-file-list): added missing variable `file' to `let'.
107
18598 by Richard M. Stallman
Initial revision
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
21289 by Dave Love
Various doc fixes, mainly grammar.
114
  "Revert individual buffers when files on disk change.
107994 by Chong Yidong
Tweaks to Customize interface. Set custom-reset-button-menu to t.
115
Auto-Revert mode enables auto-revert in individual buffers.
116
Global Auto-Revert mode does so in all buffers."
22251 by Dan Nicolaescu
*** empty log message ***
117
  :group 'files
118
  :group 'convenience)
18598 by Richard M. Stallman
Initial revision
119
120
121
;; Variables:
122
49780.1.32 by Henrik Enberg
sync with trunk
123
;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
124
;;; What's this?: ;;;###autoload
18598 by Richard M. Stallman
Initial revision
125
(defvar auto-revert-mode nil
103357 by Glenn Morris
autorevert.el trivia.
126
  "Non-nil when Auto-Revert Mode is active.
44441 by Stefan Monnier
(auto-revert-mode, global-auto-revert-mode):
127
Never set this variable directly, use the command `auto-revert-mode' instead.")
128
(put 'auto-revert-mode 'permanent-local t)
18598 by Richard M. Stallman
Initial revision
129
49780.1.32 by Henrik Enberg
sync with trunk
130
(defvar auto-revert-tail-mode nil
103357 by Glenn Morris
autorevert.el trivia.
131
  "Non-nil when Auto-Revert Tail Mode is active.
49780.1.32 by Henrik Enberg
sync with trunk
132
Never set this variable directly, use the command
133
`auto-revert-tail-mode' instead.")
134
(put 'auto-revert-tail-mode 'permanent-local t)
135
136
(defvar auto-revert-timer nil
137
  "Timer used by Auto-Revert Mode.")
138
18598 by Richard M. Stallman
Initial revision
139
(defcustom auto-revert-interval 5
53342 by Eli Zaretskii
(auto-revert-interval): Doc fix.
140
  "Time, in seconds, between Auto-Revert Mode file checks.
49780.1.32 by Henrik Enberg
sync with trunk
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."
18598 by Richard M. Stallman
Initial revision
150
  :group 'auto-revert
49780.1.32 by Henrik Enberg
sync with trunk
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))))
18598 by Richard M. Stallman
Initial revision
157
158
(defcustom auto-revert-stop-on-user-input t
49780.1.32 by Henrik Enberg
sync with trunk
159
  "When non-nil, user input temporarily interrupts Auto-Revert Mode.
160
With this setting, Auto-Revert Mode checks for user input after
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.
165
Thus, with this setting, Emacs might be non-responsive at times."
18598 by Richard M. Stallman
Initial revision
166
  :group 'auto-revert
167
  :type 'boolean)
168
169
(defcustom auto-revert-verbose t
49780.1.32 by Henrik Enberg
sync with trunk
170
  "When nil, Auto-Revert Mode does not generate any messages.
171
When non-nil, a message is generated whenever a file is reverted."
18598 by Richard M. Stallman
Initial revision
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
49780.1.32 by Henrik Enberg
sync with trunk
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
188
  :type 'string
189
  :version "22.1")
190
18598 by Richard M. Stallman
Initial revision
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
21289 by Dave Love
Various doc fixes, mainly grammar.
201
vary over time, or between buffers.  Hence mode line text
18598 by Richard M. Stallman
Initial revision
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
49780.1.32 by Henrik Enberg
sync with trunk
212
  "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
18598 by Richard M. Stallman
Initial revision
213
214
When non-nil, both file buffers and buffers with a custom
49780.1.32 by Henrik Enberg
sync with trunk
215
`revert-buffer-function' and a `buffer-stale-function' are
216
reverted by Global Auto-Revert mode.  These include the Buffer
96276 by Glenn Morris
(global-auto-revert-non-file-buffers, global-auto-revert-mode): Doc fixes.
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.
56119 by Luc Teirlinck
(global-auto-revert-non-file-buffers): Update docstring.
224
49780.1.32 by Henrik Enberg
sync with trunk
225
Use this option with care since it could lead to excessive auto-reverts.
70434 by Eli Zaretskii
(global-auto-revert-non-file-buffers): Point Info links to the main manual,
226
For more information, see Info node `(emacs)Autorevert'."
18598 by Richard M. Stallman
Initial revision
227
  :group 'auto-revert
49780.1.32 by Henrik Enberg
sync with trunk
228
  :type 'boolean
70434 by Eli Zaretskii
(global-auto-revert-non-file-buffers): Point Info links to the main manual,
229
  :link '(info-link "(emacs)Autorevert"))
18598 by Richard M. Stallman
Initial revision
230
49780.1.32 by Henrik Enberg
sync with trunk
231
(defcustom global-auto-revert-ignore-modes ()
18598 by Richard M. Stallman
Initial revision
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
49780.1.32 by Henrik Enberg
sync with trunk
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
258
buffers.  CPU usage depends on the version control system."
259
  :group 'auto-revert
260
  :type 'boolean
261
  :version "22.1")
262
18598 by Richard M. Stallman
Initial revision
263
(defvar global-auto-revert-ignore-buffer nil
103357 by Glenn Morris
autorevert.el trivia.
264
  "When non-nil, Global Auto-Revert Mode will not revert this buffer.
21289 by Dave Love
Various doc fixes, mainly grammar.
265
This variable becomes buffer local when set in any fashion.")
18598 by Richard M. Stallman
Initial revision
266
(make-variable-buffer-local 'global-auto-revert-ignore-buffer)
267
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
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
113527 by Michael Albinus
* filenotify.el (file-notify-supported-p):
274
(defcustom auto-revert-use-notify t
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
275
  "If non-nil Auto Revert Mode uses file notification functions.
113447 by Michael Albinus
* filenotify.el (file-notify--library): Renamed from
276
You should set this variable through Custom."
111477 by Michael Albinus
* autorevert.el (auto-revert-notify-enabled): Move up.
277
  :group 'auto-revert
278
  :type 'boolean
279
  :set (lambda (variable value)
113447 by Michael Albinus
* filenotify.el (file-notify--library): Renamed from
280
	 (set-default variable value)
111542 by michael.albinus at gmx
* autorevert.el (auto-revert-use-notify): In the :set function, do
281
	 (unless (symbol-value variable)
113447 by Michael Albinus
* filenotify.el (file-notify--library): Renamed from
282
	   (dolist (buf (buffer-list))
283
	     (with-current-buffer buf
284
	       (when (symbol-value 'auto-revert-notify-watch-descriptor)
285
		 (auto-revert-notify-rm-watch))))))
111620 by michael.albinus at gmx
* autorevert.el (auto-revert-use-notify): Use
286
  :initialize 'custom-initialize-default
111477 by Michael Albinus
* autorevert.el (auto-revert-notify-enabled): Move up.
287
  :version "24.4")
288
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
289
(defcustom auto-revert-notify-exclude-dir-regexp
290
  (concat
291
   ;; No mounted file systems.
292
   "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
293
   ;; No remote files.
294
   (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
295
  "Regular expression of directories to be excluded from file notifications."
296
  :group 'auto-revert
297
  :type 'regexp
298
  :version "24.4")
299
18598 by Richard M. Stallman
Initial revision
300
;; Internal variables:
301
49780.1.32 by Henrik Enberg
sync with trunk
302
(defvar auto-revert-buffer-list ()
18598 by Richard M. Stallman
Initial revision
303
  "List of buffers in Auto-Revert Mode.
304
305
Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
306
buffers to this list.
307
308
The timer function `auto-revert-buffers' is responsible for purging
309
the list of old buffers.")
310
49780.1.32 by Henrik Enberg
sync with trunk
311
(defvar auto-revert-remaining-buffers ()
18598 by Richard M. Stallman
Initial revision
312
  "Buffers not checked when user input stopped execution.")
313
49780.1.32 by Henrik Enberg
sync with trunk
314
(defvar auto-revert-tail-pos 0
315
  "Position of last known end of file.")
316
317
(add-hook 'find-file-hook
78945 by Glenn Morris
(auto-revert-tail-mode): auto-revert-tail-pos is zero, not nil, when
318
 	  (lambda ()
319
 	    (set (make-local-variable 'auto-revert-tail-pos)
320
 		 (nth 7 (file-attributes buffer-file-name)))))
18598 by Richard M. Stallman
Initial revision
321
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
322
(defvar auto-revert-notify-watch-descriptor-hash-list
323
  (make-hash-table :test 'equal)
324
  "A hash table collecting all file watch descriptors.
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
325
Hash key is a watch descriptor, hash value is a list of buffers
326
which are related to files being watched and carrying the same
327
default directory.")
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
328
329
(defvar auto-revert-notify-watch-descriptor nil
330
  "The file watch descriptor active for the current buffer.")
111545 by Michael Albinus
* autorevert.el (auto-revert-notify-watch-descriptor): Make it
331
(make-variable-buffer-local 'auto-revert-notify-watch-descriptor)
111500 by Michael Albinus
* autorevert.el (auto-revert-notify-watch-descriptor): Give it
332
(put 'auto-revert-notify-watch-descriptor 'permanent-local t)
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
333
334
(defvar auto-revert-notify-modified-p nil
335
  "Non-nil when file has been modified on the file system.
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
336
This has been reported by a file notification event.")
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
337
(make-variable-buffer-local 'auto-revert-notify-modified-p)
338
18598 by Richard M. Stallman
Initial revision
339
;; Functions:
340
341
;;;###autoload
44441 by Stefan Monnier
(auto-revert-mode, global-auto-revert-mode):
342
(define-minor-mode auto-revert-mode
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
343
  "Toggle reverting buffer when the file changes (Auto Revert mode).
344
With a prefix argument ARG, enable Auto Revert mode if ARG is
345
positive, and disable it otherwise.  If called from Lisp, enable
346
the mode if ARG is omitted or nil.
347
348
Auto Revert mode is a minor mode that affects only the current
349
buffer.  When enabled, it reverts the buffer when the file on
350
disk changes.
351
49780.1.32 by Henrik Enberg
sync with trunk
352
Use `global-auto-revert-mode' to automatically revert all buffers.
353
Use `auto-revert-tail-mode' if you know that the file will only grow
354
without being changed in the part that is already in the buffer."
355
  :group 'auto-revert :lighter auto-revert-mode-text
18598 by Richard M. Stallman
Initial revision
356
  (if auto-revert-mode
357
      (if (not (memq (current-buffer) auto-revert-buffer-list))
358
	  (push (current-buffer) auto-revert-buffer-list))
111477 by Michael Albinus
* autorevert.el (auto-revert-notify-enabled): Move up.
359
    (when auto-revert-use-notify (auto-revert-notify-rm-watch))
18598 by Richard M. Stallman
Initial revision
360
    (setq auto-revert-buffer-list
361
	  (delq (current-buffer) auto-revert-buffer-list)))
362
  (auto-revert-set-timer)
363
  (when auto-revert-mode
112226 by michael.albinus at gmx
* autorevert.el (auto-revert-mode, auto-revert-tail-mode)
364
    (let (auto-revert-use-notify)
365
      (auto-revert-buffers)
366
      (setq auto-revert-tail-mode nil))))
18598 by Richard M. Stallman
Initial revision
367
368
369
;;;###autoload
370
(defun turn-on-auto-revert-mode ()
371
  "Turn on Auto-Revert Mode.
372
373
This function is designed to be added to hooks, for example:
374
  (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
375
  (auto-revert-mode 1))
376
377
378
;;;###autoload
49780.1.32 by Henrik Enberg
sync with trunk
379
(define-minor-mode auto-revert-tail-mode
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
380
  "Toggle reverting tail of buffer when the file grows.
381
With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
382
is positive, and disable it otherwise.  If called from Lisp,
383
enable the mode if ARG is omitted or nil.
49780.1.32 by Henrik Enberg
sync with trunk
384
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
385
When Auto Revert Tail mode is enabled, the tail of the file is
386
constantly followed, as with the shell command `tail -f'.  This
387
means that whenever the file grows on disk (presumably because
388
some background process is appending to it from time to time),
389
this is reflected in the current buffer.
49780.1.32 by Henrik Enberg
sync with trunk
390
391
You can edit the buffer and turn this mode off and on again as
392
you please.  But make sure the background process has stopped
393
writing before you save the file!
394
395
Use `auto-revert-mode' for changes other than appends!"
396
  :group 'find-file :lighter auto-revert-tail-mode-text
397
  (when auto-revert-tail-mode
398
    (unless buffer-file-name
399
      (auto-revert-tail-mode 0)
400
      (error "This buffer is not visiting a file"))
401
    (if (and (buffer-modified-p)
78945 by Glenn Morris
(auto-revert-tail-mode): auto-revert-tail-pos is zero, not nil, when
402
	     (zerop auto-revert-tail-pos) ; library was loaded only after finding file
49780.1.32 by Henrik Enberg
sync with trunk
403
	     (not (y-or-n-p "Buffer is modified, so tail offset may be wrong.  Proceed? ")))
404
	(auto-revert-tail-mode 0)
78945 by Glenn Morris
(auto-revert-tail-mode): auto-revert-tail-pos is zero, not nil, when
405
      ;; a-r-tail-pos stores the size of the file at the time of the
406
      ;; last revert. After this package loads, it adds a
407
      ;; find-file-hook to set this variable every time a file is
408
      ;; loaded.  If the package is loaded only _after_ visiting the
409
      ;; file to be reverted, then we have no idea what the value of
410
      ;; a-r-tail-pos should have been when the file was visited.  If
411
      ;; the file has changed on disk in the meantime, all we can do
412
      ;; is offer to revert the whole thing. If you choose not to
413
      ;; revert, then you might miss some output then happened
414
      ;; between visiting the file and activating a-r-t-mode.
415
      (and (zerop auto-revert-tail-pos)
416
           (not (verify-visited-file-modtime (current-buffer)))
417
           (y-or-n-p "File changed on disk, content may be missing.  \
418
Perform a full revert? ")
419
           ;; Use this (not just revert-buffer) for point-preservation.
112226 by michael.albinus at gmx
* autorevert.el (auto-revert-mode, auto-revert-tail-mode)
420
	   (let (auto-revert-use-notify)
421
	     (auto-revert-handler)))
49780.1.32 by Henrik Enberg
sync with trunk
422
      ;; else we might reappend our own end when we save
423
      (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
424
      (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
425
	  (set (make-local-variable 'auto-revert-tail-pos)
76660 by Jason Rumney
(find-file-hook, auto-revert-tail-mode):
426
	       (nth 7 (file-attributes buffer-file-name))))
49780.1.32 by Henrik Enberg
sync with trunk
427
      ;; let auto-revert-mode set up the mechanism for us if it isn't already
428
      (or auto-revert-mode
429
	  (let ((auto-revert-tail-mode t))
430
	    (auto-revert-mode 1)))
431
      (setq auto-revert-mode nil))))
432
433
434
;;;###autoload
435
(defun turn-on-auto-revert-tail-mode ()
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
436
  "Turn on Auto-Revert Tail mode.
49780.1.32 by Henrik Enberg
sync with trunk
437
438
This function is designed to be added to hooks, for example:
439
  (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
440
  (auto-revert-tail-mode 1))
441
442
443
;;;###autoload
44441 by Stefan Monnier
(auto-revert-mode, global-auto-revert-mode):
444
(define-minor-mode global-auto-revert-mode
96276 by Glenn Morris
(global-auto-revert-non-file-buffers, global-auto-revert-mode): Doc fixes.
445
  "Toggle Global Auto Revert mode.
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
446
With a prefix argument ARG, enable Global Auto Revert mode if ARG
447
is positive, and disable it otherwise.  If called from Lisp,
448
enable the mode if ARG is omitted or nil.
96276 by Glenn Morris
(global-auto-revert-non-file-buffers, global-auto-revert-mode): Doc fixes.
449
106134 by Chong Yidong
Fix minor mode docstrings for the new meaning of a nil ARG.
450
Global Auto Revert mode is a global minor mode that reverts any
451
buffer associated with a file when the file changes on disk.  Use
452
`auto-revert-mode' to revert a particular buffer.
96276 by Glenn Morris
(global-auto-revert-non-file-buffers, global-auto-revert-mode): Doc fixes.
453
454
If `global-auto-revert-non-file-buffers' is non-nil, this mode
455
may also revert some non-file buffers, as described in the
456
documentation of that variable.  It ignores buffers with modes
457
matching `global-auto-revert-ignore-modes', and buffers with a
458
non-nil vale of `global-auto-revert-ignore-buffer'.
459
460
This function calls the hook `global-auto-revert-mode-hook'.
461
It displays the text that `global-auto-revert-mode-text'
462
specifies in the mode line."
44441 by Stefan Monnier
(auto-revert-mode, global-auto-revert-mode):
463
  :global t :group 'auto-revert :lighter global-auto-revert-mode-text
18598 by Richard M. Stallman
Initial revision
464
  (auto-revert-set-timer)
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
465
  (if global-auto-revert-mode
112226 by michael.albinus at gmx
* autorevert.el (auto-revert-mode, auto-revert-tail-mode)
466
      (let (auto-revert-use-notify)
467
	(auto-revert-buffers))
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
468
    (dolist (buf (buffer-list))
469
      (with-current-buffer buf
470
	(when auto-revert-use-notify
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
471
	  (auto-revert-notify-rm-watch))))))
18598 by Richard M. Stallman
Initial revision
472
473
(defun auto-revert-set-timer ()
49780.1.32 by Henrik Enberg
sync with trunk
474
  "Restart or cancel the timer used by Auto-Revert Mode.
475
If such a timer is active, cancel it.  Start a new timer if
476
Global Auto-Revert Mode is active or if Auto-Revert Mode is active
477
in some buffer.  Restarting the timer ensures that Auto-Revert Mode
478
will use an up-to-date value of `auto-revert-interval'"
479
  (interactive)
18598 by Richard M. Stallman
Initial revision
480
  (if (timerp auto-revert-timer)
481
      (cancel-timer auto-revert-timer))
44441 by Stefan Monnier
(auto-revert-mode, global-auto-revert-mode):
482
  (setq auto-revert-timer
483
	(if (or global-auto-revert-mode auto-revert-buffer-list)
484
	    (run-with-timer auto-revert-interval
485
			    auto-revert-interval
49780.1.32 by Henrik Enberg
sync with trunk
486
			    'auto-revert-buffers))))
487
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
488
(defun auto-revert-notify-rm-watch ()
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
489
  "Disable file notification for current buffer's associated file."
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
490
  (when auto-revert-notify-watch-descriptor
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
491
    (maphash
492
     (lambda (key value)
493
       (when (equal key auto-revert-notify-watch-descriptor)
494
	 (setq value (delete (current-buffer) value))
495
	 (if value
496
	     (puthash key value auto-revert-notify-watch-descriptor-hash-list)
497
	   (remhash key auto-revert-notify-watch-descriptor-hash-list)
498
	   (ignore-errors
113280 by Michael Albinus
* filenotify.el: New package.
499
	     (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
500
     auto-revert-notify-watch-descriptor-hash-list)
111542 by michael.albinus at gmx
* autorevert.el (auto-revert-use-notify): In the :set function, do
501
    (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch))
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
502
  (setq auto-revert-notify-watch-descriptor nil
503
	auto-revert-notify-modified-p nil))
504
505
(defun auto-revert-notify-add-watch ()
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
506
  "Enable file notification for current buffer's associated file."
115439 by Michael Albinus
* autorevert.el (auto-revert-notify-add-watch): Do not handle symlinked files.
507
  ;; We can assume that `buffer-file-name' and
508
  ;; `auto-revert-use-notify' are non-nil.
509
  (when (or (string-match auto-revert-notify-exclude-dir-regexp
510
			  (expand-file-name default-directory))
115441 by Eli Zaretskii
Fix a thinko in last commit in autorevert.el.
511
	    (file-symlink-p buffer-file-name))
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
512
    ;; Fallback to file checks.
513
    (set (make-local-variable 'auto-revert-use-notify) nil))
514
115439 by Michael Albinus
* autorevert.el (auto-revert-notify-add-watch): Do not handle symlinked files.
515
  (when (not auto-revert-notify-watch-descriptor)
113280 by Michael Albinus
* filenotify.el: New package.
516
    (setq auto-revert-notify-watch-descriptor
517
	  (ignore-errors
518
	    (file-notify-add-watch
519
	     (expand-file-name buffer-file-name default-directory)
520
	     '(change attribute-change) 'auto-revert-notify-handler)))
521
    (if auto-revert-notify-watch-descriptor
522
	(progn
523
	  (puthash
524
	   auto-revert-notify-watch-descriptor
525
	   (cons (current-buffer)
526
		 (gethash auto-revert-notify-watch-descriptor
527
			  auto-revert-notify-watch-descriptor-hash-list))
528
	   auto-revert-notify-watch-descriptor-hash-list)
529
	  (add-hook (make-local-variable 'kill-buffer-hook)
530
		    'auto-revert-notify-rm-watch))
531
      ;; Fallback to file checks.
532
      (set (make-local-variable 'auto-revert-use-notify) nil))))
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
533
534
(defun auto-revert-notify-handler (event)
112864 by Stefan Monnier
* lisp/autorevert.el (auto-revert-notify-handler): Use memq.
535
  "Handle an EVENT returned from file notification."
114130 by Stefan Monnier
* lisp/autorevert.el (auto-revert-notify-handler): Explicitly ignore
536
  (with-demoted-errors
113280 by Michael Albinus
* filenotify.el: New package.
537
    (let* ((descriptor (car event))
538
	   (action (nth 1 event))
539
	   (file (nth 2 event))
540
	   (file1 (nth 3 event)) ;; Target of `renamed'.
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
541
	   (buffers (gethash descriptor
542
			     auto-revert-notify-watch-descriptor-hash-list)))
113280 by Michael Albinus
* filenotify.el: New package.
543
      ;; Check, that event is meant for us.
544
      (cl-assert descriptor)
545
      ;; We do not handle `deleted', because nothing has to be refreshed.
114130 by Stefan Monnier
* lisp/autorevert.el (auto-revert-notify-handler): Explicitly ignore
546
      (unless (eq action 'deleted)
547
        (cl-assert (memq action '(attribute-changed changed created renamed))
548
                   t)
549
        ;; Since we watch a directory, a file name must be returned.
550
        (cl-assert (stringp file))
551
        (when (eq action 'renamed) (cl-assert (stringp file1)))
552
        ;; Loop over all buffers, in order to find the intended one.
114224 by Stefan Monnier
* lisp/autorevert.el (auto-revert-notify-handler): Use `cl-dolist' since we
553
        (cl-dolist (buffer buffers)
114130 by Stefan Monnier
* lisp/autorevert.el (auto-revert-notify-handler): Explicitly ignore
554
          (when (buffer-live-p buffer)
555
            (with-current-buffer buffer
556
              (when (and (stringp buffer-file-name)
557
                         (or
558
                          (and (memq action '(attribute-changed changed
559
                                              created))
560
                               (string-equal
561
                                (file-name-nondirectory file)
562
                                (file-name-nondirectory buffer-file-name)))
563
                          (and (eq action 'renamed)
564
                               (string-equal
565
                                (file-name-nondirectory file1)
566
                                (file-name-nondirectory buffer-file-name)))))
567
                ;; Mark buffer modified.
568
                (setq auto-revert-notify-modified-p t)
569
                ;; No need to check other buffers.
570
                (cl-return)))))))))
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
571
53943 by Eli Zaretskii
Added support to detect changed dired buffers.
572
(defun auto-revert-active-p ()
573
  "Check if auto-revert is active (in current buffer or globally)."
574
  (or auto-revert-mode
49780.1.32 by Henrik Enberg
sync with trunk
575
      auto-revert-tail-mode
53943 by Eli Zaretskii
Added support to detect changed dired buffers.
576
      (and
577
       global-auto-revert-mode
578
       (not global-auto-revert-ignore-buffer)
579
       (not (memq major-mode
580
		  global-auto-revert-ignore-modes)))))
581
582
(defun auto-revert-handler ()
49780.1.32 by Henrik Enberg
sync with trunk
583
  "Revert current buffer, if appropriate.
584
This is an internal function used by Auto-Revert Mode."
585
  (when (or auto-revert-tail-mode (not (buffer-modified-p)))
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
586
    (let* ((buffer (current-buffer)) size
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
587
	   ;; Tramp caches the file attributes.  Setting
588
	   ;; `remote-file-name-inhibit-cache' forces Tramp to reread
589
	   ;; the values.
590
	   (remote-file-name-inhibit-cache t)
49780.1.32 by Henrik Enberg
sync with trunk
591
	   (revert
592
	    (or (and buffer-file-name
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
593
		     (or auto-revert-remote-files
594
			 (not (file-remote-p buffer-file-name)))
111609 by Michael Albinus
* autorevert.el (auto-revert-handler): Notifications which result
595
		     (or (not auto-revert-use-notify)
596
			 auto-revert-notify-modified-p)
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
597
		     (if auto-revert-tail-mode
111609 by Michael Albinus
* autorevert.el (auto-revert-handler): Notifications which result
598
			 (and (file-readable-p buffer-file-name)
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
599
			      (/= auto-revert-tail-pos
600
				  (setq size
601
					(nth 7 (file-attributes
602
						buffer-file-name)))))
113642 by Stefan Monnier
* lisp/files.el: Use lexical-binding.
603
		       (funcall (or buffer-stale-function
604
                                    #'buffer-stale--default-function)
605
                                t)))
49780.1.32 by Henrik Enberg
sync with trunk
606
		(and (or auto-revert-mode
607
			 global-auto-revert-non-file-buffers)
113642 by Stefan Monnier
* lisp/files.el: Use lexical-binding.
608
		     (funcall (or buffer-stale-function
609
                                  #'buffer-stale--default-function)
610
                              t))))
49780.1.32 by Henrik Enberg
sync with trunk
611
	   eob eoblist)
111609 by Michael Albinus
* autorevert.el (auto-revert-handler): Notifications which result
612
      (setq auto-revert-notify-modified-p nil)
49780.1.32 by Henrik Enberg
sync with trunk
613
      (when revert
614
	(when (and auto-revert-verbose
615
		   (not (eq revert 'fast)))
616
	  (message "Reverting buffer `%s'." (buffer-name)))
617
	;; If point (or a window point) is at the end of the buffer,
618
	;; we want to keep it at the end after reverting.  This allows
619
	;; to tail a file.
620
	(when buffer-file-name
621
	  (setq eob (eobp))
622
	  (walk-windows
103904 by Juanma Barranquero
lisp/autorevert.el: Use `remote-file-name-inhibit-cache'.
623
	   (lambda (window)
624
	     (and (eq (window-buffer window) buffer)
625
		  (= (window-point window) (point-max))
626
		  (push window eoblist)))
49780.1.32 by Henrik Enberg
sync with trunk
627
	   'no-mini t))
628
	(if auto-revert-tail-mode
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
629
	    (auto-revert-tail-handler size)
49780.1.32 by Henrik Enberg
sync with trunk
630
	  ;; Bind buffer-read-only in case user has done C-x C-q,
631
	  ;; so as not to forget that.  This gives undesirable results
632
	  ;; when the file's mode changes, but that is less common.
633
	  (let ((buffer-read-only buffer-read-only))
634
	    (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
635
	(when buffer-file-name
636
	  (when eob (goto-char (point-max)))
637
	  (dolist (window eoblist)
638
	    (set-window-point window (point-max)))))
639
      ;; `preserve-modes' avoids changing the (minor) modes.  But we
640
      ;; do want to reset the mode for VC, so we do it manually.
641
      (when (or revert auto-revert-check-vc-info)
642
	(vc-find-file-hook)))))
53943 by Eli Zaretskii
Added support to detect changed dired buffers.
643
96794 by Michael Albinus
* autorevert.el (auto-revert-handler): Allow
644
(defun auto-revert-tail-handler (size)
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
645
  (let ((modified (buffer-modified-p))
78409 by Stefan Monnier
(auto-revert-tail-handler): Use inhibit-read-only.
646
	(inhibit-read-only t)		; Ignore.
49780.1.32 by Henrik Enberg
sync with trunk
647
	(file buffer-file-name)
78409 by Stefan Monnier
(auto-revert-tail-handler): Use inhibit-read-only.
648
	(buffer-file-name nil))		; Ignore that file has changed.
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
649
    (when (/= auto-revert-tail-pos size)
78409 by Stefan Monnier
(auto-revert-tail-handler): Use inhibit-read-only.
650
      (run-hooks 'before-revert-hook)
49780.1.32 by Henrik Enberg
sync with trunk
651
      (undo-boundary)
652
      (save-restriction
653
	(widen)
654
	(save-excursion
655
	  (goto-char (point-max))
80910 by David Kastrup
(auto-revert-handler): In `auto-revert-tail-mode',
656
	  (insert-file-contents file nil
657
				(and (< auto-revert-tail-pos size)
658
				     auto-revert-tail-pos)
659
				size)))
78409 by Stefan Monnier
(auto-revert-tail-handler): Use inhibit-read-only.
660
      (run-hooks 'after-revert-hook)
49780.1.32 by Henrik Enberg
sync with trunk
661
      (undo-boundary)
662
      (setq auto-revert-tail-pos size)
78409 by Stefan Monnier
(auto-revert-tail-handler): Use inhibit-read-only.
663
      (restore-buffer-modified-p modified)))
49780.1.32 by Henrik Enberg
sync with trunk
664
  (set-visited-file-modtime))
56378 by Daniel Pfeiffer
(auto-revert-tail-mode, auto-revert-tail-mode-text, auto-revert-tail-pos): New vars.
665
18598 by Richard M. Stallman
Initial revision
666
(defun auto-revert-buffers ()
667
  "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
668
669
Should `global-auto-revert-mode' be active all file buffers are checked.
670
671
Should `auto-revert-mode' be active in some buffers, those buffers
672
are checked.
673
49780.1.32 by Henrik Enberg
sync with trunk
674
Non-file buffers that have a custom `revert-buffer-function' and
116811.1.93 by Glenn Morris
Doc updates for buffer-stale-function now applying to file buffers too
675
`buffer-stale-function' are reverted either when Auto-Revert
49780.1.32 by Henrik Enberg
sync with trunk
676
Mode is active in that buffer, or when the variable
677
`global-auto-revert-non-file-buffers' is non-nil and Global
678
Auto-Revert Mode is active.
18598 by Richard M. Stallman
Initial revision
679
21289 by Dave Love
Various doc fixes, mainly grammar.
680
This function stops whenever there is user input.  The buffers not
18598 by Richard M. Stallman
Initial revision
681
checked are stored in the variable `auto-revert-remaining-buffers'.
682
683
To avoid starvation, the buffers in `auto-revert-remaining-buffers'
684
are checked first the next time this function is called.
685
21289 by Dave Love
Various doc fixes, mainly grammar.
686
This function is also responsible for removing buffers no longer in
18598 by Richard M. Stallman
Initial revision
687
Auto-Revert mode from `auto-revert-buffer-list', and for canceling
688
the timer when no buffers need to be checked."
49780.1.32 by Henrik Enberg
sync with trunk
689
  (save-match-data
690
    (let ((bufs (if global-auto-revert-mode
691
		    (buffer-list)
692
		  auto-revert-buffer-list))
693
	  (remaining ())
694
	  (new ()))
695
      ;; Partition `bufs' into two halves depending on whether or not
696
      ;; the buffers are in `auto-revert-remaining-buffers'.  The two
697
      ;; halves are then re-joined with the "remaining" buffers at the
698
      ;; head of the list.
699
      (dolist (buf auto-revert-remaining-buffers)
700
	(if (memq buf bufs)
701
	    (push buf remaining)))
702
      (dolist (buf bufs)
703
	(if (not (memq buf remaining))
704
	    (push buf new)))
705
      (setq bufs (nreverse (nconc new remaining)))
706
      (while (and bufs
707
		  (not (and auto-revert-stop-on-user-input
708
			    (input-pending-p))))
709
	(let ((buf (car bufs)))
85884 by Stefan Monnier
(auto-revert-buffers): Use buffer-live-p.
710
          (if (buffer-live-p buf)
49780.1.32 by Henrik Enberg
sync with trunk
711
	      (with-current-buffer buf
712
		;; Test if someone has turned off Auto-Revert Mode in a
713
		;; non-standard way, for example by changing major mode.
714
		(if (and (not auto-revert-mode)
715
			 (not auto-revert-tail-mode)
716
			 (memq buf auto-revert-buffer-list))
717
		    (setq auto-revert-buffer-list
718
			  (delq buf auto-revert-buffer-list)))
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
719
		(when (auto-revert-active-p)
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
720
		  ;; Enable file notification.
111477 by Michael Albinus
* autorevert.el (auto-revert-notify-enabled): Move up.
721
		  (when (and auto-revert-use-notify buffer-file-name
111603 by michael.albinus at gmx
* autorevert.el (auto-revert-remote-files)
722
			     (not auto-revert-notify-watch-descriptor))
723
		    (auto-revert-notify-add-watch))
111472 by michael.albinus at gmx
* autorevert.el: Add file watch support.
724
		  (auto-revert-handler)))
49780.1.32 by Henrik Enberg
sync with trunk
725
	    ;; Remove dead buffer from `auto-revert-buffer-list'.
726
	    (setq auto-revert-buffer-list
727
		  (delq buf auto-revert-buffer-list))))
728
	(setq bufs (cdr bufs)))
729
      (setq auto-revert-remaining-buffers bufs)
730
      ;; Check if we should cancel the timer.
731
      (when (and (not global-auto-revert-mode)
732
		 (null auto-revert-buffer-list))
733
	(cancel-timer auto-revert-timer)
734
	(setq auto-revert-timer nil)))))
18598 by Richard M. Stallman
Initial revision
735
736
737
;; The end:
738
(provide 'autorevert)
739
740
(run-hooks 'auto-revert-load-hook)
741
38421 by Pavel Janík
742
;;; autorevert.el ends here