]> code.delx.au - gnu-emacs/blobdiff - lisp/autorevert.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / autorevert.el
index a2d70c37762b48a3d25de3a2f865f0856adf1bf3..357916c6b4d7dcacb7431a618f1c3e713b96c6ec 100644 (file)
@@ -1,6 +1,6 @@
 ;;; autorevert.el --- revert buffers when files on disk change
 
-;; Copyright (C) 1997-1999, 2001-2013 Free Software Foundation, Inc.
+;; Copyright (C) 1997-1999, 2001-2015 Free Software Foundation, Inc.
 
 ;; Author: Anders Lindgren <andersl@andersl.com>
 ;; Keywords: convenience
 
 (eval-when-compile (require 'cl-lib))
 (require 'timer)
+(require 'filenotify)
 
 ;; Custom Group:
 ;;
@@ -270,25 +271,18 @@ This variable becomes buffer local when set in any fashion.")
   :type 'boolean
   :version "24.4")
 
-(defconst auto-revert-notify-enabled
-  (or (featurep 'inotify) (featurep 'w32notify))
-  "Non-nil when Emacs has been compiled with file notification support.")
-
-(defcustom auto-revert-use-notify auto-revert-notify-enabled
+(defcustom auto-revert-use-notify t
   "If non-nil Auto Revert Mode uses file notification functions.
-This requires Emacs being compiled with file notification
-support (see `auto-revert-notify-enabled').  You should set this
-variable through Custom."
+You should set this variable through Custom."
   :group 'auto-revert
   :type 'boolean
   :set (lambda (variable value)
-        (set-default variable (and auto-revert-notify-enabled value))
+        (set-default variable value)
         (unless (symbol-value variable)
-          (when auto-revert-notify-enabled
-            (dolist (buf (buffer-list))
-              (with-current-buffer buf
-                (when (symbol-value 'auto-revert-notify-watch-descriptor)
-                  (auto-revert-notify-rm-watch)))))))
+          (dolist (buf (buffer-list))
+            (with-current-buffer buf
+              (when (symbol-value 'auto-revert-notify-watch-descriptor)
+                (auto-revert-notify-rm-watch))))))
   :initialize 'custom-initialize-default
   :version "24.4")
 
@@ -422,7 +416,7 @@ Use `auto-revert-mode' for changes other than appends!"
            (y-or-n-p "File changed on disk, content may be missing.  \
 Perform a full revert? ")
            ;; Use this (not just revert-buffer) for point-preservation.
-           (auto-revert-handler))
+           (auto-revert-buffers))
       ;; else we might reappend our own end when we save
       (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
       (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
@@ -499,9 +493,7 @@ will use an up-to-date value of `auto-revert-interval'"
             (puthash key value auto-revert-notify-watch-descriptor-hash-list)
           (remhash key auto-revert-notify-watch-descriptor-hash-list)
           (ignore-errors
-            (funcall (if (fboundp 'inotify-rm-watch)
-                         'inotify-rm-watch 'w32notify-rm-watch)
-                     auto-revert-notify-watch-descriptor)))))
+            (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
      auto-revert-notify-watch-descriptor-hash-list)
     (remove-hook 'kill-buffer-hook 'auto-revert-notify-rm-watch))
   (setq auto-revert-notify-watch-descriptor nil
@@ -509,23 +501,27 @@ will use an up-to-date value of `auto-revert-interval'"
 
 (defun auto-revert-notify-add-watch ()
   "Enable file notification for current buffer's associated file."
-  (when (string-match auto-revert-notify-exclude-dir-regexp
-                     (expand-file-name default-directory))
-    ;; Fallback to file checks.
-    (set (make-local-variable 'auto-revert-use-notify) nil))
-
-  (when (and buffer-file-name auto-revert-use-notify
-            (not auto-revert-notify-watch-descriptor))
-    (let ((func (if (fboundp 'inotify-add-watch)
-                   'inotify-add-watch 'w32notify-add-watch))
-         (aspect (if (fboundp 'inotify-add-watch)
-                     '(create modify moved-to) '(size last-write-time)))
-         (file (if (fboundp 'inotify-add-watch)
-                   (directory-file-name (expand-file-name default-directory))
-                 (buffer-file-name))))
+  ;; We can assume that `buffer-file-name' and
+  ;; `auto-revert-use-notify' are non-nil.
+  (if (or (string-match auto-revert-notify-exclude-dir-regexp
+                       (expand-file-name default-directory))
+         (file-symlink-p (or buffer-file-name default-directory)))
+
+      ;; Fallback to file checks.
+      (set (make-local-variable 'auto-revert-use-notify) nil)
+
+    (when (not auto-revert-notify-watch-descriptor)
       (setq auto-revert-notify-watch-descriptor
            (ignore-errors
-             (funcall func file aspect 'auto-revert-notify-handler)))
+             (if buffer-file-name
+                 (file-notify-add-watch
+                  (expand-file-name buffer-file-name default-directory)
+                  '(change attribute-change)
+                  'auto-revert-notify-handler)
+               (file-notify-add-watch
+                (expand-file-name default-directory)
+                '(change)
+                'auto-revert-notify-handler))))
       (if auto-revert-notify-watch-descriptor
          (progn
            (puthash
@@ -539,57 +535,75 @@ will use an up-to-date value of `auto-revert-interval'"
        ;; Fallback to file checks.
        (set (make-local-variable 'auto-revert-use-notify) nil)))))
 
-(defun auto-revert-notify-event-p (event)
-  "Check that event is a file notification event."
-  (cond ((featurep 'inotify)
-        (and (listp event) (= (length event) 4)))
-       ((featurep 'w32notify)
-        (and (listp event) (= (length event) 3) (stringp (nth 2 event))))))
-
-(defun auto-revert-notify-event-descriptor (event)
-  "Return watch descriptor of file notification event, or nil."
-  (and (auto-revert-notify-event-p event) (car event)))
-
-(defun auto-revert-notify-event-action (event)
-  "Return action of file notification event, or nil."
-  (and (auto-revert-notify-event-p event) (nth 1 event)))
-
-(defun auto-revert-notify-event-file-name (event)
-  "Return file name of file notification event, or nil."
-  (and (auto-revert-notify-event-p event)
-       (cond ((featurep 'inotify) (nth 3 event))
-            ((featurep 'w32notify) (nth 2 event)))))
+;; If we have file notifications, we want to update the auto-revert buffers
+;; immediately when a notification occurs. Since file updates can happen very
+;; often, we want to skip some revert operations so that we don't spend all our
+;; time reverting the buffer.
+;;
+;; We do this by reverting immediately in response to the first in a flurry of
+;; notifications. We suppress subsequent notifications until the next time
+;; `auto-revert-buffers' is called (this happens on a timer with a period set by
+;; `auto-revert-interval').
+(defvar auto-revert-buffers-counter 1
+  "Incremented each time `auto-revert-buffers' is called")
+(defvar-local auto-revert-buffers-counter-lockedout 0
+  "Buffer-local value to indicate whether we should immediately
+update the buffer on a notification event or not. If
+
+  (= auto-revert-buffers-counter-lockedout
+     auto-revert-buffers-counter)
+
+then the updates are locked out, and we wait until the next call
+of `auto-revert-buffers' to revert the buffer. If no lockout is
+present, then we revert immediately and set the lockout, so that
+no more reverts are possible until the next call of
+`auto-revert-buffers'")
 
 (defun auto-revert-notify-handler (event)
-  "Handle an event returned from file notification."
-  (when (auto-revert-notify-event-p event)
-    (let* ((descriptor (auto-revert-notify-event-descriptor event))
-          (action (auto-revert-notify-event-action event))
-          (file (auto-revert-notify-event-file-name event))
+  "Handle an EVENT returned from file notification."
+  (with-demoted-errors
+    (let* ((descriptor (car event))
+          (action (nth 1 event))
+          (file (nth 2 event))
+          (file1 (nth 3 event)) ;; Target of `renamed'.
           (buffers (gethash descriptor
                             auto-revert-notify-watch-descriptor-hash-list)))
-      (ignore-errors
-       ;; Check, that event is meant for us.
-       ;; TODO: Filter events which stop watching, like `move' or `removed'.
-       (cl-assert descriptor)
-       (when (featurep 'inotify)
-         (cl-assert (or (memq 'create action)
-                        (memq 'modify action)
-                        (memq 'moved-to action))))
-       (when (featurep 'w32notify) (cl-assert (eq 'modified action)))
-       ;; Since we watch a directory, a file name must be returned.
-       (cl-assert (stringp file))
-       (dolist (buffer buffers)
-         (when (buffer-live-p buffer)
-           (with-current-buffer buffer
-             (when (and (stringp buffer-file-name)
-                        (string-equal
-                         (file-name-nondirectory file)
-                         (file-name-nondirectory buffer-file-name)))
-               ;; Mark buffer modified.
-               (setq auto-revert-notify-modified-p t)
-               ;; No need to check other buffers.
-               (cl-return)))))))))
+      ;; Check, that event is meant for us.
+      (cl-assert descriptor)
+      ;; Since we watch a directory, a file name must be returned.
+      (cl-assert (stringp file))
+      (when (eq action 'renamed) (cl-assert (stringp file1)))
+      ;; Loop over all buffers, in order to find the intended one.
+      (cl-dolist (buffer buffers)
+       (when (buffer-live-p buffer)
+         (with-current-buffer buffer
+           (when (or
+                  ;; A buffer associated with a file.
+                  (and (stringp buffer-file-name)
+                       (or
+                        (and (memq action '(attribute-changed changed created))
+                             (string-equal
+                              (file-name-nondirectory file)
+                              (file-name-nondirectory buffer-file-name)))
+                        (and (eq action 'renamed)
+                             (string-equal
+                              (file-name-nondirectory file1)
+                              (file-name-nondirectory buffer-file-name)))))
+                  ;; A buffer w/o a file, like dired.
+                  (and (null buffer-file-name)
+                       (memq action '(created renamed deleted))))
+             ;; Mark buffer modified.
+             (setq auto-revert-notify-modified-p t)
+
+             ;; Revert the buffer now if we're not locked out.
+             (when (/= auto-revert-buffers-counter-lockedout
+                       auto-revert-buffers-counter)
+               (auto-revert-handler)
+               (setq auto-revert-buffers-counter-lockedout
+                     auto-revert-buffers-counter))
+
+             ;; No need to check other buffers.
+             (cl-return))))))))
 
 (defun auto-revert-active-p ()
   "Check if auto-revert is active (in current buffer or globally)."
@@ -611,8 +625,8 @@ This is an internal function used by Auto-Revert Mode."
           ;; the values.
           (remote-file-name-inhibit-cache t)
           (revert
-           (or (and buffer-file-name
-                    (or auto-revert-remote-files
+           (if buffer-file-name
+               (and (or auto-revert-remote-files
                         (not (file-remote-p buffer-file-name)))
                     (or (not auto-revert-use-notify)
                         auto-revert-notify-modified-p)
@@ -622,14 +636,14 @@ This is an internal function used by Auto-Revert Mode."
                                  (setq size
                                        (nth 7 (file-attributes
                                                buffer-file-name)))))
-                      (and (file-readable-p buffer-file-name)
-                           (not (verify-visited-file-modtime buffer)))))
-               (and (or auto-revert-mode
-                        global-auto-revert-non-file-buffers)
-                    revert-buffer-function
-                    (boundp 'buffer-stale-function)
-                    (functionp buffer-stale-function)
-                    (funcall buffer-stale-function t))))
+                      (funcall (or buffer-stale-function
+                                    #'buffer-stale--default-function)
+                                t)))
+             (and (or auto-revert-mode
+                      global-auto-revert-non-file-buffers)
+                  (funcall (or buffer-stale-function
+                               #'buffer-stale--default-function)
+                           t))))
           eob eoblist)
       (setq auto-revert-notify-modified-p nil)
       (when revert
@@ -694,7 +708,7 @@ Should `auto-revert-mode' be active in some buffers, those buffers
 are checked.
 
 Non-file buffers that have a custom `revert-buffer-function' and
-`buffer-stale-function' are reverted either when Auto-Revert
+`buffer-stale-function' are reverted either when Auto-Revert
 Mode is active in that buffer, or when the variable
 `global-auto-revert-non-file-buffers' is non-nil and Global
 Auto-Revert Mode is active.
@@ -708,12 +722,15 @@ are checked first the next time this function is called.
 This function is also responsible for removing buffers no longer in
 Auto-Revert mode from `auto-revert-buffer-list', and for canceling
 the timer when no buffers need to be checked."
+
+  (setq auto-revert-buffers-counter
+        (1+ auto-revert-buffers-counter))
+
   (save-match-data
     (let ((bufs (if global-auto-revert-mode
                    (buffer-list)
                  auto-revert-buffer-list))
-         (remaining ())
-         (new ()))
+         remaining new)
       ;; Partition `bufs' into two halves depending on whether or not
       ;; the buffers are in `auto-revert-remaining-buffers'.  The two
       ;; halves are then re-joined with the "remaining" buffers at the
@@ -740,7 +757,7 @@ the timer when no buffers need to be checked."
                          (delq buf auto-revert-buffer-list)))
                (when (auto-revert-active-p)
                  ;; Enable file notification.
-                 (when (and auto-revert-use-notify buffer-file-name
+                 (when (and auto-revert-use-notify
                             (not auto-revert-notify-watch-descriptor))
                    (auto-revert-notify-add-watch))
                  (auto-revert-handler)))