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.
This commit is contained in:
parent
5b64f18134
commit
530bb2dc68
2 changed files with 39 additions and 43 deletions
|
@ -373,7 +373,7 @@ without being changed in the part that is already in the buffer."
|
||||||
'kill-buffer-hook
|
'kill-buffer-hook
|
||||||
#'auto-revert-remove-current-buffer
|
#'auto-revert-remove-current-buffer
|
||||||
nil t))
|
nil t))
|
||||||
(when auto-revert-use-notify (auto-revert-notify-rm-watch))
|
(when auto-revert-notify-watch-descriptor (auto-revert-notify-rm-watch))
|
||||||
(auto-revert-remove-current-buffer))
|
(auto-revert-remove-current-buffer))
|
||||||
(auto-revert-set-timer)
|
(auto-revert-set-timer)
|
||||||
(when auto-revert-mode
|
(when auto-revert-mode
|
||||||
|
@ -486,7 +486,7 @@ specifies in the mode line."
|
||||||
(auto-revert-buffers)
|
(auto-revert-buffers)
|
||||||
(dolist (buf (buffer-list))
|
(dolist (buf (buffer-list))
|
||||||
(with-current-buffer buf
|
(with-current-buffer buf
|
||||||
(when auto-revert-use-notify
|
(when auto-revert-notify-watch-descriptor
|
||||||
(auto-revert-notify-rm-watch))))))
|
(auto-revert-notify-rm-watch))))))
|
||||||
|
|
||||||
(defun auto-revert-set-timer ()
|
(defun auto-revert-set-timer ()
|
||||||
|
@ -524,38 +524,31 @@ will use an up-to-date value of `auto-revert-interval'"
|
||||||
(defun auto-revert-notify-add-watch ()
|
(defun auto-revert-notify-add-watch ()
|
||||||
"Enable file notification for current buffer's associated file."
|
"Enable file notification for current buffer's associated file."
|
||||||
;; We can assume that `buffer-file-name' and
|
;; We can assume that `buffer-file-name' and
|
||||||
;; `auto-revert-use-notify' are non-nil.
|
;; `auto-revert-notify-watch-descriptor' are non-nil.
|
||||||
(if (or (string-match auto-revert-notify-exclude-dir-regexp
|
(unless (or auto-revert-notify-watch-descriptor
|
||||||
(expand-file-name default-directory))
|
(string-match auto-revert-notify-exclude-dir-regexp
|
||||||
(file-symlink-p (or buffer-file-name default-directory)))
|
(expand-file-name default-directory))
|
||||||
|
(file-symlink-p (or buffer-file-name default-directory)))
|
||||||
;; Fallback to file checks.
|
(setq auto-revert-notify-watch-descriptor
|
||||||
(setq-local auto-revert-use-notify nil)
|
(ignore-errors
|
||||||
|
(if buffer-file-name
|
||||||
(when (not auto-revert-notify-watch-descriptor)
|
|
||||||
(setq auto-revert-notify-watch-descriptor
|
|
||||||
(ignore-errors
|
|
||||||
(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
|
(file-notify-add-watch
|
||||||
(expand-file-name default-directory)
|
(expand-file-name buffer-file-name default-directory)
|
||||||
'(change)
|
'(change attribute-change)
|
||||||
'auto-revert-notify-handler))))
|
'auto-revert-notify-handler)
|
||||||
(if auto-revert-notify-watch-descriptor
|
(file-notify-add-watch
|
||||||
(progn
|
(expand-file-name default-directory)
|
||||||
(puthash
|
'(change)
|
||||||
auto-revert-notify-watch-descriptor
|
'auto-revert-notify-handler))))
|
||||||
(cons (current-buffer)
|
(when auto-revert-notify-watch-descriptor
|
||||||
(gethash auto-revert-notify-watch-descriptor
|
(setq auto-revert-notify-modified-p t)
|
||||||
auto-revert-notify-watch-descriptor-hash-list))
|
(puthash
|
||||||
auto-revert-notify-watch-descriptor-hash-list)
|
auto-revert-notify-watch-descriptor
|
||||||
(add-hook 'kill-buffer-hook
|
(cons (current-buffer)
|
||||||
#'auto-revert-notify-rm-watch nil t))
|
(gethash auto-revert-notify-watch-descriptor
|
||||||
;; Fallback to file checks.
|
auto-revert-notify-watch-descriptor-hash-list))
|
||||||
(setq-local auto-revert-use-notify nil)))))
|
auto-revert-notify-watch-descriptor-hash-list)
|
||||||
|
(add-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch nil t))))
|
||||||
|
|
||||||
;; If we have file notifications, we want to update the auto-revert buffers
|
;; If we have file notifications, we want to update the auto-revert buffers
|
||||||
;; immediately when a notification occurs. Since file updates can happen very
|
;; 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
|
||||||
(file-name-nondirectory buffer-file-name)))
|
(file-name-nondirectory buffer-file-name)))
|
||||||
;; A buffer w/o a file, like dired.
|
;; A buffer w/o a file, like dired.
|
||||||
(null buffer-file-name)))
|
(null buffer-file-name)))
|
||||||
(auto-revert-notify-rm-watch)
|
(auto-revert-notify-rm-watch))))
|
||||||
(setq-local auto-revert-use-notify nil))))
|
|
||||||
|
|
||||||
;; Loop over all buffers, in order to find the intended one.
|
;; Loop over all buffers, in order to find the intended one.
|
||||||
(cl-dolist (buffer buffers)
|
(cl-dolist (buffer buffers)
|
||||||
|
@ -651,11 +643,9 @@ no more reverts are possible until the next call of
|
||||||
"Check if auto-revert is active (in current buffer or globally)."
|
"Check if auto-revert is active (in current buffer or globally)."
|
||||||
(or auto-revert-mode
|
(or auto-revert-mode
|
||||||
auto-revert-tail-mode
|
auto-revert-tail-mode
|
||||||
(and
|
(and global-auto-revert-mode
|
||||||
global-auto-revert-mode
|
(not global-auto-revert-ignore-buffer)
|
||||||
(not global-auto-revert-ignore-buffer)
|
(not (memq major-mode global-auto-revert-ignore-modes)))))
|
||||||
(not (memq major-mode
|
|
||||||
global-auto-revert-ignore-modes)))))
|
|
||||||
|
|
||||||
(defun auto-revert-handler ()
|
(defun auto-revert-handler ()
|
||||||
"Revert current buffer, if appropriate.
|
"Revert current buffer, if appropriate.
|
||||||
|
@ -669,7 +659,7 @@ This is an internal function used by Auto-Revert Mode."
|
||||||
(if buffer-file-name
|
(if buffer-file-name
|
||||||
(and (or auto-revert-remote-files
|
(and (or auto-revert-remote-files
|
||||||
(not (file-remote-p buffer-file-name)))
|
(not (file-remote-p buffer-file-name)))
|
||||||
(or (not auto-revert-use-notify)
|
(or (not auto-revert-notify-watch-descriptor)
|
||||||
auto-revert-notify-modified-p)
|
auto-revert-notify-modified-p)
|
||||||
(if auto-revert-tail-mode
|
(if auto-revert-tail-mode
|
||||||
(and (file-readable-p buffer-file-name)
|
(and (file-readable-p buffer-file-name)
|
||||||
|
|
|
@ -161,7 +161,7 @@ This expects `auto-revert--messages' to be bound by
|
||||||
:tags '(:expensive-test)
|
:tags '(:expensive-test)
|
||||||
|
|
||||||
(let ((tmpfile (make-temp-file "auto-revert-test"))
|
(let ((tmpfile (make-temp-file "auto-revert-test"))
|
||||||
buf)
|
buf desc)
|
||||||
(unwind-protect
|
(unwind-protect
|
||||||
(progn
|
(progn
|
||||||
(write-region "any text" nil tmpfile nil 'no-message)
|
(write-region "any text" nil tmpfile nil 'no-message)
|
||||||
|
@ -174,6 +174,7 @@ This expects `auto-revert--messages' to be bound by
|
||||||
(sleep-for 1)
|
(sleep-for 1)
|
||||||
(auto-revert-mode 1)
|
(auto-revert-mode 1)
|
||||||
(should auto-revert-mode)
|
(should auto-revert-mode)
|
||||||
|
(setq desc auto-revert-notify-watch-descriptor)
|
||||||
|
|
||||||
;; Remove file while reverting. We simulate this by
|
;; Remove file while reverting. We simulate this by
|
||||||
;; modifying `before-revert-hook'.
|
;; modifying `before-revert-hook'.
|
||||||
|
@ -192,7 +193,7 @@ This expects `auto-revert--messages' to be bound by
|
||||||
(should (string-match "any text" (buffer-string)))
|
(should (string-match "any text" (buffer-string)))
|
||||||
;; With w32notify, the 'stopped' events are not sent.
|
;; With w32notify, the 'stopped' events are not sent.
|
||||||
(or (eq file-notify--library 'w32notify)
|
(or (eq file-notify--library 'w32notify)
|
||||||
(should-not auto-revert-use-notify))
|
(should-not auto-revert-notify-watch-descriptor))
|
||||||
|
|
||||||
;; Once the file has been recreated, the buffer shall be
|
;; Once the file has been recreated, the buffer shall be
|
||||||
;; reverted.
|
;; reverted.
|
||||||
|
@ -203,6 +204,11 @@ This expects `auto-revert--messages' to be bound by
|
||||||
(auto-revert--wait-for-revert buf))
|
(auto-revert--wait-for-revert buf))
|
||||||
;; Check, that the buffer has been reverted.
|
;; Check, that the buffer has been reverted.
|
||||||
(should (string-match "another text" (buffer-string)))
|
(should (string-match "another text" (buffer-string)))
|
||||||
|
;; When file notification is used, it must be reenabled
|
||||||
|
;; after recreation of the file. We cannot expect that
|
||||||
|
;; the descriptor is the same, so we just check the
|
||||||
|
;; existence.
|
||||||
|
(should (eq (null desc) (null auto-revert-notify-watch-descriptor)))
|
||||||
|
|
||||||
;; An empty file shall still be reverted.
|
;; An empty file shall still be reverted.
|
||||||
(ert-with-message-capture auto-revert--messages
|
(ert-with-message-capture auto-revert--messages
|
||||||
|
|
Loading…
Add table
Reference in a new issue