Commit e92dac34 authored by Mattias Engdegård's avatar Mattias Engdegård
Browse files

Keep auto-revert-mode working when changing buffer file name (bug#36159)

* lisp/autorevert.el (after-set-visited-file-name-hook):
Add unconditionally.
(global-auto-revert-mode): Don't use
`after-set-visited-file-name-hook' here.
(auto-revert-set-visited-file-name): Rename from
`auto-revert--global-set-visited-file-name' and generalise.
* test/lisp/autorevert-tests.el (auto-revert-test06-write-file): New.
parent 2b8f9ded
Pipeline #1997 failed with stage
in 3 seconds
...@@ -347,6 +347,8 @@ the list of old buffers.") ...@@ -347,6 +347,8 @@ the list of old buffers.")
(add-hook 'find-file-hook (add-hook 'find-file-hook
#'auto-revert-find-file-function) #'auto-revert-find-file-function)
(add-hook 'after-set-visited-file-name-hook
#'auto-revert-set-visited-file-name)
(defvar auto-revert--buffers-by-watch-descriptor (defvar auto-revert--buffers-by-watch-descriptor
(make-hash-table :test 'equal) (make-hash-table :test 'equal)
...@@ -508,8 +510,6 @@ specifies in the mode line." ...@@ -508,8 +510,6 @@ specifies in the mode line."
(auto-revert--global-add-current-buffer))) (auto-revert--global-add-current-buffer)))
;; Make sure future buffers are added as well. ;; Make sure future buffers are added as well.
(add-hook 'find-file-hook #'auto-revert--global-adopt-current-buffer) (add-hook 'find-file-hook #'auto-revert--global-adopt-current-buffer)
(add-hook 'after-set-visited-file-name-hook
#'auto-revert--global-set-visited-file-name)
;; To track non-file buffers, we need to listen in to buffer ;; To track non-file buffers, we need to listen in to buffer
;; creation in general. Listening to major-mode changes is ;; creation in general. Listening to major-mode changes is
;; suitable, since we then know whether it's a mode that is tracked. ;; suitable, since we then know whether it's a mode that is tracked.
...@@ -520,8 +520,6 @@ specifies in the mode line." ...@@ -520,8 +520,6 @@ specifies in the mode line."
;; Turn global-auto-revert-mode OFF. ;; Turn global-auto-revert-mode OFF.
(remove-hook 'after-change-major-mode-hook (remove-hook 'after-change-major-mode-hook
#'auto-revert--global-adopt-current-buffer) #'auto-revert--global-adopt-current-buffer)
(remove-hook 'after-set-visited-file-name-hook
#'auto-revert--global-set-visited-file-name)
(remove-hook 'find-file-hook #'auto-revert--global-adopt-current-buffer) (remove-hook 'find-file-hook #'auto-revert--global-adopt-current-buffer)
(dolist (buf (buffer-list)) (dolist (buf (buffer-list))
(with-current-buffer buf (with-current-buffer buf
...@@ -551,14 +549,17 @@ specifies in the mode line." ...@@ -551,14 +549,17 @@ specifies in the mode line."
(auto-revert--global-add-current-buffer) (auto-revert--global-add-current-buffer)
(auto-revert-set-timer)) (auto-revert-set-timer))
(defun auto-revert--global-set-visited-file-name () (defun auto-revert-set-visited-file-name ()
"Update Global Auto-Revert management of the current buffer. "Update Auto-Revert management of the current buffer.
Called after `set-visited-file-name'." Called after `set-visited-file-name'."
;; Remove any existing notifier first so that we don't track the
;; wrong file in case the file name was changed.
(when auto-revert-notify-watch-descriptor (when auto-revert-notify-watch-descriptor
;; Remove any existing notifier so that we don't track the wrong
;; file in case the file name was changed.
(auto-revert-notify-rm-watch)) (auto-revert-notify-rm-watch))
(auto-revert--global-adopt-current-buffer)) (cond (global-auto-revert-mode
(auto-revert--global-adopt-current-buffer))
((or auto-revert-mode auto-revert-tail-mode)
(auto-revert-set-timer))))
(defun auto-revert--polled-buffers () (defun auto-revert--polled-buffers ()
"List of buffers that need to be polled." "List of buffers that need to be polled."
......
...@@ -542,6 +542,40 @@ This expects `auto-revert--messages' to be bound by ...@@ -542,6 +542,40 @@ This expects `auto-revert--messages' to be bound by
(auto-revert--deftest-remote auto-revert-test05-global-notify (auto-revert--deftest-remote auto-revert-test05-global-notify
"Test `global-auto-revert-mode' without polling for remote buffers.") "Test `global-auto-revert-mode' without polling for remote buffers.")
(ert-deftest auto-revert-test06-write-file ()
"Verify that notification follows `write-file' correctly."
:tags '(:expensive-test)
(skip-unless (or file-notify--library
(file-remote-p temporary-file-directory)))
(let* ((auto-revert-use-notify t)
(file-1 (make-temp-file "auto-revert-test"))
(file-2 (concat file-1 "-2"))
(buf nil))
(unwind-protect
(progn
(setq buf (find-file-noselect file-1))
(with-current-buffer buf
(insert "A")
(save-buffer)
(auto-revert-mode 1)
(insert "B")
(write-file file-2)
(auto-revert-test--write-file "C" file-2)
(auto-revert-test--wait-for-buffer-text
buf "C" (+ auto-revert-interval 1))
(should (equal (buffer-string) "C"))))
;; Clean up.
(ignore-errors (kill-buffer buf))
(ignore-errors (delete-file file-1))
(ignore-errors (delete-file file-2)))))
(auto-revert--deftest-remote auto-revert-test06-write-file
"Test `write-file' in `auto-revert-mode' for remote buffers.")
(defun auto-revert-test-all (&optional interactive) (defun auto-revert-test-all (&optional interactive)
"Run all tests for \\[auto-revert]." "Run all tests for \\[auto-revert]."
(interactive "p") (interactive "p")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment