aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2015-10-27 16:02:26 +0100
committerMichael Albinus2015-10-27 16:02:26 +0100
commit838023d469054cc19d0a2b7cf48e39082d8220f0 (patch)
treec6e4bd985901ab909948332d6b67e3c47b471ee5
parent99ded6be7afe843d7abc0e255fe36b684435cfd7 (diff)
downloademacs-838023d469054cc19d0a2b7cf48e39082d8220f0.tar.gz
emacs-838023d469054cc19d0a2b7cf48e39082d8220f0.zip
Fall back to polling in autorevert when needed
* lisp/autorevert.el (auto-revert-notify-handler): When a `stopped' event arrives from file notification, fall back to polling. * test/automated/file-notify-tests.el (file-notify-test03-autorevert): Extend test for polling when file notification ceases to work.
-rw-r--r--lisp/autorevert.el79
-rw-r--r--test/automated/file-notify-tests.el29
2 files changed, 76 insertions, 32 deletions
diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 37ee8eedcfd..f0c12d2d97e 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -570,37 +570,54 @@ no more reverts are possible until the next call of
570 ;; Since we watch a directory, a file name must be returned. 570 ;; Since we watch a directory, a file name must be returned.
571 (cl-assert (stringp file)) 571 (cl-assert (stringp file))
572 (when (eq action 'renamed) (cl-assert (stringp file1))) 572 (when (eq action 'renamed) (cl-assert (stringp file1)))
573 ;; Loop over all buffers, in order to find the intended one. 573
574 (cl-dolist (buffer buffers) 574 (if (eq action 'stopped)
575 (when (buffer-live-p buffer) 575 ;; File notification has stopped. Continue with polling.
576 (with-current-buffer buffer 576 (cl-dolist (buffer buffers)
577 (when (or 577 (with-current-buffer buffer
578 ;; A buffer associated with a file. 578 (when (or
579 (and (stringp buffer-file-name) 579 ;; A buffer associated with a file.
580 (or 580 (and (stringp buffer-file-name)
581 (and (memq action '(attribute-changed changed created)) 581 (string-equal
582 (string-equal 582 (file-name-nondirectory file)
583 (file-name-nondirectory file) 583 (file-name-nondirectory buffer-file-name)))
584 (file-name-nondirectory buffer-file-name))) 584 ;; A buffer w/o a file, like dired.
585 (and (eq action 'renamed) 585 (null buffer-file-name))
586 (string-equal 586 (auto-revert-notify-rm-watch)
587 (file-name-nondirectory file1) 587 (setq-local auto-revert-use-notify nil))))
588 (file-name-nondirectory buffer-file-name))))) 588
589 ;; A buffer w/o a file, like dired. 589 ;; Loop over all buffers, in order to find the intended one.
590 (and (null buffer-file-name) 590 (cl-dolist (buffer buffers)
591 (memq action '(created renamed deleted)))) 591 (when (buffer-live-p buffer)
592 ;; Mark buffer modified. 592 (with-current-buffer buffer
593 (setq auto-revert-notify-modified-p t) 593 (when (or
594 594 ;; A buffer associated with a file.
595 ;; Revert the buffer now if we're not locked out. 595 (and (stringp buffer-file-name)
596 (when (/= auto-revert-buffers-counter-lockedout 596 (or
597 auto-revert-buffers-counter) 597 (and (memq
598 (auto-revert-handler) 598 action '(attribute-changed changed created))
599 (setq auto-revert-buffers-counter-lockedout 599 (string-equal
600 auto-revert-buffers-counter)) 600 (file-name-nondirectory file)
601 601 (file-name-nondirectory buffer-file-name)))
602 ;; No need to check other buffers. 602 (and (eq action 'renamed)
603 (cl-return)))))))) 603 (string-equal
604 (file-name-nondirectory file1)
605 (file-name-nondirectory buffer-file-name)))))
606 ;; A buffer w/o a file, like dired.
607 (and (null buffer-file-name)
608 (memq action '(created renamed deleted))))
609 ;; Mark buffer modified.
610 (setq auto-revert-notify-modified-p t)
611
612 ;; Revert the buffer now if we're not locked out.
613 (when (/= auto-revert-buffers-counter-lockedout
614 auto-revert-buffers-counter)
615 (auto-revert-handler)
616 (setq auto-revert-buffers-counter-lockedout
617 auto-revert-buffers-counter))
618
619 ;; No need to check other buffers.
620 (cl-return)))))))))
604 621
605(defun auto-revert-active-p () 622(defun auto-revert-active-p ()
606 "Check if auto-revert is active (in current buffer or globally)." 623 "Check if auto-revert is active (in current buffer or globally)."
diff --git a/test/automated/file-notify-tests.el b/test/automated/file-notify-tests.el
index 472c6927b87..f411c6b76b0 100644
--- a/test/automated/file-notify-tests.el
+++ b/test/automated/file-notify-tests.el
@@ -461,6 +461,8 @@ Don't wait longer than TIMEOUT seconds for the events to be delivered."
461 461
462 ;; Modify file. We wait for a second, in order to 462 ;; Modify file. We wait for a second, in order to
463 ;; have another timestamp. 463 ;; have another timestamp.
464 (with-current-buffer (get-buffer-create "*Messages*")
465 (narrow-to-region (point-max) (point-max)))
464 (sleep-for 1) 466 (sleep-for 1)
465 (write-region 467 (write-region
466 "another text" nil file-notify--test-tmpfile nil 'no-message) 468 "another text" nil file-notify--test-tmpfile nil 'no-message)
@@ -472,9 +474,34 @@ Don't wait longer than TIMEOUT seconds for the events to be delivered."
472 (string-match 474 (string-match
473 (format-message "Reverting buffer `%s'." (buffer-name buf)) 475 (format-message "Reverting buffer `%s'." (buffer-name buf))
474 (buffer-string)))) 476 (buffer-string))))
475 (should (string-match "another text" (buffer-string))))) 477 (should (string-match "another text" (buffer-string)))
478
479 ;; Stop file notification. Autorevert shall still work via polling.
480 (file-notify-rm-watch auto-revert-notify-watch-descriptor)
481 (file-notify--wait-for-events
482 timeout (null auto-revert-use-notify))
483 (should-not auto-revert-use-notify)
484 (should-not auto-revert-notify-watch-descriptor)
485
486 ;; Modify file. We wait for a second, in order to
487 ;; have another timestamp.
488 (with-current-buffer (get-buffer-create "*Messages*")
489 (narrow-to-region (point-max) (point-max)))
490 (sleep-for 2)
491 (write-region
492 "foo bla" nil file-notify--test-tmpfile nil 'no-message)
493
494 ;; Check, that the buffer has been reverted.
495 (with-current-buffer (get-buffer-create "*Messages*")
496 (file-notify--wait-for-events
497 timeout
498 (string-match
499 (format-message "Reverting buffer `%s'." (buffer-name buf))
500 (buffer-string))))
501 (should (string-match "foo bla" (buffer-string)))))
476 502
477 ;; Cleanup. 503 ;; Cleanup.
504 (with-current-buffer "*Messages*" (widen))
478 (ignore-errors (kill-buffer buf)) 505 (ignore-errors (kill-buffer buf))
479 (file-notify--test-cleanup)))) 506 (file-notify--test-cleanup))))
480 507