aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2018-01-20 16:26:02 +0100
committerMichael Albinus2018-01-20 16:26:02 +0100
commit530bb2dc68bbd8be8a90759992095cc2edc4efcf (patch)
tree0e90f97d0893246cdcfd1c812db912b23ccafacd
parent5b64f1813476b681c10649e9aa0642af2c4ed845 (diff)
downloademacs-530bb2dc68bbd8be8a90759992095cc2edc4efcf.tar.gz
emacs-530bb2dc68bbd8be8a90759992095cc2edc4efcf.zip
Use file notification in autorevert also for recreated files
* lisp/autorevert.el (auto-revert-mode) (global-auto-revert-mode, auto-revert-notify-add-watch) (auto-revert-notify-handler, auto-revert-handler): Do not use buffer local `auto-revert-use-notify' anymore. * test/lisp/autorevert-tests.el (auto-revert-test02-auto-revert-deleted-file): Adapt test in order to check, that file notification is reenabled when possible.
-rw-r--r--lisp/autorevert.el72
-rw-r--r--test/lisp/autorevert-tests.el10
2 files changed, 39 insertions, 43 deletions
diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 7b8302695fa..da8942664b7 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -373,7 +373,7 @@ without being changed in the part that is already in the buffer."
373 'kill-buffer-hook 373 'kill-buffer-hook
374 #'auto-revert-remove-current-buffer 374 #'auto-revert-remove-current-buffer
375 nil t)) 375 nil t))
376 (when auto-revert-use-notify (auto-revert-notify-rm-watch)) 376 (when auto-revert-notify-watch-descriptor (auto-revert-notify-rm-watch))
377 (auto-revert-remove-current-buffer)) 377 (auto-revert-remove-current-buffer))
378 (auto-revert-set-timer) 378 (auto-revert-set-timer)
379 (when auto-revert-mode 379 (when auto-revert-mode
@@ -486,7 +486,7 @@ specifies in the mode line."
486 (auto-revert-buffers) 486 (auto-revert-buffers)
487 (dolist (buf (buffer-list)) 487 (dolist (buf (buffer-list))
488 (with-current-buffer buf 488 (with-current-buffer buf
489 (when auto-revert-use-notify 489 (when auto-revert-notify-watch-descriptor
490 (auto-revert-notify-rm-watch)))))) 490 (auto-revert-notify-rm-watch))))))
491 491
492(defun auto-revert-set-timer () 492(defun auto-revert-set-timer ()
@@ -524,38 +524,31 @@ will use an up-to-date value of `auto-revert-interval'"
524(defun auto-revert-notify-add-watch () 524(defun auto-revert-notify-add-watch ()
525 "Enable file notification for current buffer's associated file." 525 "Enable file notification for current buffer's associated file."
526 ;; We can assume that `buffer-file-name' and 526 ;; We can assume that `buffer-file-name' and
527 ;; `auto-revert-use-notify' are non-nil. 527 ;; `auto-revert-notify-watch-descriptor' are non-nil.
528 (if (or (string-match auto-revert-notify-exclude-dir-regexp 528 (unless (or auto-revert-notify-watch-descriptor
529 (expand-file-name default-directory)) 529 (string-match auto-revert-notify-exclude-dir-regexp
530 (file-symlink-p (or buffer-file-name default-directory))) 530 (expand-file-name default-directory))
531 531 (file-symlink-p (or buffer-file-name default-directory)))
532 ;; Fallback to file checks. 532 (setq auto-revert-notify-watch-descriptor
533 (setq-local auto-revert-use-notify nil) 533 (ignore-errors
534 534 (if buffer-file-name
535 (when (not auto-revert-notify-watch-descriptor)
536 (setq auto-revert-notify-watch-descriptor
537 (ignore-errors
538 (if buffer-file-name
539 (file-notify-add-watch
540 (expand-file-name buffer-file-name default-directory)
541 '(change attribute-change)
542 'auto-revert-notify-handler)
543 (file-notify-add-watch 535 (file-notify-add-watch
544 (expand-file-name default-directory) 536 (expand-file-name buffer-file-name default-directory)
545 '(change) 537 '(change attribute-change)
546 'auto-revert-notify-handler)))) 538 'auto-revert-notify-handler)
547 (if auto-revert-notify-watch-descriptor 539 (file-notify-add-watch
548 (progn 540 (expand-file-name default-directory)
549 (puthash 541 '(change)
550 auto-revert-notify-watch-descriptor 542 'auto-revert-notify-handler))))
551 (cons (current-buffer) 543 (when auto-revert-notify-watch-descriptor
552 (gethash auto-revert-notify-watch-descriptor 544 (setq auto-revert-notify-modified-p t)
553 auto-revert-notify-watch-descriptor-hash-list)) 545 (puthash
554 auto-revert-notify-watch-descriptor-hash-list) 546 auto-revert-notify-watch-descriptor
555 (add-hook 'kill-buffer-hook 547 (cons (current-buffer)
556 #'auto-revert-notify-rm-watch nil t)) 548 (gethash auto-revert-notify-watch-descriptor
557 ;; Fallback to file checks. 549 auto-revert-notify-watch-descriptor-hash-list))
558 (setq-local auto-revert-use-notify nil))))) 550 auto-revert-notify-watch-descriptor-hash-list)
551 (add-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch nil t))))
559 552
560;; If we have file notifications, we want to update the auto-revert buffers 553;; If we have file notifications, we want to update the auto-revert buffers
561;; immediately when a notification occurs. Since file updates can happen very 554;; immediately when a notification occurs. Since file updates can happen very
@@ -611,8 +604,7 @@ no more reverts are possible until the next call of
611 (file-name-nondirectory buffer-file-name))) 604 (file-name-nondirectory buffer-file-name)))
612 ;; A buffer w/o a file, like dired. 605 ;; A buffer w/o a file, like dired.
613 (null buffer-file-name))) 606 (null buffer-file-name)))
614 (auto-revert-notify-rm-watch) 607 (auto-revert-notify-rm-watch))))
615 (setq-local auto-revert-use-notify nil))))
616 608
617 ;; Loop over all buffers, in order to find the intended one. 609 ;; Loop over all buffers, in order to find the intended one.
618 (cl-dolist (buffer buffers) 610 (cl-dolist (buffer buffers)
@@ -651,11 +643,9 @@ no more reverts are possible until the next call of
651 "Check if auto-revert is active (in current buffer or globally)." 643 "Check if auto-revert is active (in current buffer or globally)."
652 (or auto-revert-mode 644 (or auto-revert-mode
653 auto-revert-tail-mode 645 auto-revert-tail-mode
654 (and 646 (and global-auto-revert-mode
655 global-auto-revert-mode 647 (not global-auto-revert-ignore-buffer)
656 (not global-auto-revert-ignore-buffer) 648 (not (memq major-mode global-auto-revert-ignore-modes)))))
657 (not (memq major-mode
658 global-auto-revert-ignore-modes)))))
659 649
660(defun auto-revert-handler () 650(defun auto-revert-handler ()
661 "Revert current buffer, if appropriate. 651 "Revert current buffer, if appropriate.
@@ -669,7 +659,7 @@ This is an internal function used by Auto-Revert Mode."
669 (if buffer-file-name 659 (if buffer-file-name
670 (and (or auto-revert-remote-files 660 (and (or auto-revert-remote-files
671 (not (file-remote-p buffer-file-name))) 661 (not (file-remote-p buffer-file-name)))
672 (or (not auto-revert-use-notify) 662 (or (not auto-revert-notify-watch-descriptor)
673 auto-revert-notify-modified-p) 663 auto-revert-notify-modified-p)
674 (if auto-revert-tail-mode 664 (if auto-revert-tail-mode
675 (and (file-readable-p buffer-file-name) 665 (and (file-readable-p buffer-file-name)
diff --git a/test/lisp/autorevert-tests.el b/test/lisp/autorevert-tests.el
index 8f375b63a69..05d24b51ee7 100644
--- a/test/lisp/autorevert-tests.el
+++ b/test/lisp/autorevert-tests.el
@@ -161,7 +161,7 @@ This expects `auto-revert--messages' to be bound by
161 :tags '(:expensive-test) 161 :tags '(:expensive-test)
162 162
163 (let ((tmpfile (make-temp-file "auto-revert-test")) 163 (let ((tmpfile (make-temp-file "auto-revert-test"))
164 buf) 164 buf desc)
165 (unwind-protect 165 (unwind-protect
166 (progn 166 (progn
167 (write-region "any text" nil tmpfile nil 'no-message) 167 (write-region "any text" nil tmpfile nil 'no-message)
@@ -174,6 +174,7 @@ This expects `auto-revert--messages' to be bound by
174 (sleep-for 1) 174 (sleep-for 1)
175 (auto-revert-mode 1) 175 (auto-revert-mode 1)
176 (should auto-revert-mode) 176 (should auto-revert-mode)
177 (setq desc auto-revert-notify-watch-descriptor)
177 178
178 ;; Remove file while reverting. We simulate this by 179 ;; Remove file while reverting. We simulate this by
179 ;; modifying `before-revert-hook'. 180 ;; modifying `before-revert-hook'.
@@ -192,7 +193,7 @@ This expects `auto-revert--messages' to be bound by
192 (should (string-match "any text" (buffer-string))) 193 (should (string-match "any text" (buffer-string)))
193 ;; With w32notify, the 'stopped' events are not sent. 194 ;; With w32notify, the 'stopped' events are not sent.
194 (or (eq file-notify--library 'w32notify) 195 (or (eq file-notify--library 'w32notify)
195 (should-not auto-revert-use-notify)) 196 (should-not auto-revert-notify-watch-descriptor))
196 197
197 ;; Once the file has been recreated, the buffer shall be 198 ;; Once the file has been recreated, the buffer shall be
198 ;; reverted. 199 ;; reverted.
@@ -203,6 +204,11 @@ This expects `auto-revert--messages' to be bound by
203 (auto-revert--wait-for-revert buf)) 204 (auto-revert--wait-for-revert buf))
204 ;; Check, that the buffer has been reverted. 205 ;; Check, that the buffer has been reverted.
205 (should (string-match "another text" (buffer-string))) 206 (should (string-match "another text" (buffer-string)))
207 ;; When file notification is used, it must be reenabled
208 ;; after recreation of the file. We cannot expect that
209 ;; the descriptor is the same, so we just check the
210 ;; existence.
211 (should (eq (null desc) (null auto-revert-notify-watch-descriptor)))
206 212
207 ;; An empty file shall still be reverted. 213 ;; An empty file shall still be reverted.
208 (ert-with-message-capture auto-revert--messages 214 (ert-with-message-capture auto-revert--messages