From: "Mattias Engdegård" <mattiase@acm.org>
To: 36159@debbugs.gnu.org
Cc: Michael Albinus <michael.albinus@gmx.de>
Subject: bug#36159: [PATCH] auto-revert mode doesn't work when changing buffer file name
Date: Mon, 10 Jun 2019 18:57:04 +0200 [thread overview]
Message-ID: <F01A3972-49FA-4152-B2F3-6BA46845D597@acm.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 221 bytes --]
Auto-revert mode stops working effectively when the user changes the buffer file name, by running `write-file' for example, because the notifier still tracks the old file.
The attached patch attempts to rectify this.
[-- Attachment #2: 0001-Keep-auto-revert-mode-working-when-changing-buffer-f.patch --]
[-- Type: application/octet-stream, Size: 5146 bytes --]
From fa1e9cb90db4a296c1e7c879c21b68285b7026a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Mon, 10 Jun 2019 17:52:50 +0200
Subject: [PATCH] Keep auto-revert-mode working when changing buffer file name
* 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.
---
lisp/autorevert.el | 19 ++++++++++---------
test/lisp/autorevert-tests.el | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 2de855b303..3944dcdc00 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -347,6 +347,8 @@ auto-revert-find-file-function
(add-hook 'find-file-hook
#'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
(make-hash-table :test 'equal)
@@ -508,8 +510,6 @@ global-auto-revert-mode
(auto-revert--global-add-current-buffer)))
;; Make sure future buffers are added as well.
(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
;; creation in general. Listening to major-mode changes is
;; suitable, since we then know whether it's a mode that is tracked.
@@ -520,8 +520,6 @@ global-auto-revert-mode
;; Turn global-auto-revert-mode OFF.
(remove-hook 'after-change-major-mode-hook
#'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)
(dolist (buf (buffer-list))
(with-current-buffer buf
@@ -551,14 +549,17 @@ auto-revert--global-adopt-current-buffer
(auto-revert--global-add-current-buffer)
(auto-revert-set-timer))
-(defun auto-revert--global-set-visited-file-name ()
- "Update Global Auto-Revert management of the current buffer.
+(defun auto-revert--set-visited-file-name ()
+ "Update Auto-Revert management of the current buffer.
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
+ ;; 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--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 ()
"List of buffers that need to be polled."
diff --git a/test/lisp/autorevert-tests.el b/test/lisp/autorevert-tests.el
index 3c9e04bf19..618923e1bf 100644
--- a/test/lisp/autorevert-tests.el
+++ b/test/lisp/autorevert-tests.el
@@ -542,6 +542,40 @@ auto-revert-test--wait-for-buffer-text
(auto-revert--deftest-remote auto-revert-test05-global-notify
"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)
+ (auto-revert-avoid-polling 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)
+ (should auto-revert-notify-watch-descriptor)
+
+ (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"))))
+ (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)
"Run all tests for \\[auto-revert]."
(interactive "p")
--
2.20.1 (Apple Git-117)
next reply other threads:[~2019-06-10 16:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-10 16:57 Mattias Engdegård [this message]
2019-06-11 13:15 ` bug#36159: [PATCH] auto-revert mode doesn't work when changing buffer file name Michael Albinus
2019-06-11 16:55 ` Mattias Engdegård
2019-06-11 18:02 ` Michael Albinus
2019-06-11 20:17 ` Mattias Engdegård
2019-06-15 9:16 ` Michael Albinus
2019-06-15 9:26 ` Mattias Engdegård
2019-06-15 9:56 ` Michael Albinus
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=F01A3972-49FA-4152-B2F3-6BA46845D597@acm.org \
--to=mattiase@acm.org \
--cc=36159@debbugs.gnu.org \
--cc=michael.albinus@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.