summaryrefslogtreecommitdiff
path: root/lisp/autorevert.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/autorevert.el')
-rw-r--r--lisp/autorevert.el115
1 files changed, 54 insertions, 61 deletions
diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index d1b8f94a8e2..16a742a458f 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -321,7 +321,7 @@ the list of old buffers.")
(defun auto-revert-find-file-function ()
(setq-local auto-revert-tail-pos
- (nth 7 (file-attributes buffer-file-name))))
+ (file-attribute-size (file-attributes buffer-file-name))))
(add-hook 'find-file-hook
#'auto-revert-find-file-function)
@@ -351,9 +351,6 @@ This has been reported by a file notification event.")
;;;###autoload
(define-minor-mode auto-revert-mode
"Toggle reverting buffer when the file changes (Auto-Revert Mode).
-With a prefix argument ARG, enable Auto-Revert Mode if ARG is
-positive, and disable it otherwise. If called from Lisp, enable
-the mode if ARG is omitted or nil.
Auto-Revert Mode is a minor mode that affects only the current
buffer. When enabled, it reverts the buffer when the file on
@@ -373,7 +370,7 @@ without being changed in the part that is already in the buffer."
'kill-buffer-hook
#'auto-revert-remove-current-buffer
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-set-timer)
(when auto-revert-mode
@@ -393,9 +390,6 @@ This function is designed to be added to hooks, for example:
;;;###autoload
(define-minor-mode auto-revert-tail-mode
"Toggle reverting tail of buffer when the file grows.
-With a prefix argument ARG, enable Auto-Revert Tail Mode if ARG
-is positive, and disable it otherwise. If called from Lisp,
-enable the mode if ARG is omitted or nil.
When Auto-Revert Tail Mode is enabled, the tail of the file is
constantly followed, as with the shell command `tail -f'. This
@@ -440,7 +434,8 @@ Perform a full revert? ")
(add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
(or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
(setq-local auto-revert-tail-pos
- (nth 7 (file-attributes buffer-file-name))))
+ (file-attribute-size
+ (file-attributes buffer-file-name))))
;; let auto-revert-mode set up the mechanism for us if it isn't already
(or auto-revert-mode
(let ((auto-revert-tail-mode t))
@@ -460,9 +455,6 @@ This function is designed to be added to hooks, for example:
;;;###autoload
(define-minor-mode global-auto-revert-mode
"Toggle Global Auto-Revert Mode.
-With a prefix argument ARG, enable Global Auto-Revert Mode if ARG
-is positive, and disable it otherwise. If called from Lisp,
-enable the mode if ARG is omitted or nil.
Global Auto-Revert Mode is a global minor mode that reverts any
buffer associated with a file when the file changes on disk. Use
@@ -486,7 +478,7 @@ specifies in the mode line."
(auto-revert-buffers)
(dolist (buf (buffer-list))
(with-current-buffer buf
- (when auto-revert-use-notify
+ (when auto-revert-notify-watch-descriptor
(auto-revert-notify-rm-watch))))))
(defun auto-revert-set-timer ()
@@ -523,39 +515,44 @@ will use an up-to-date value of `auto-revert-interval'"
(defun auto-revert-notify-add-watch ()
"Enable file notification for current buffer's associated file."
- ;; We can assume that `buffer-file-name' and
- ;; `auto-revert-use-notify' are non-nil.
- (if (or (string-match auto-revert-notify-exclude-dir-regexp
- (expand-file-name default-directory))
- (file-symlink-p (or buffer-file-name default-directory)))
-
- ;; Fallback to file checks.
- (setq-local auto-revert-use-notify nil)
-
- (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)
+ ;; We can assume that `auto-revert-notify-watch-descriptor' is nil.
+ (unless (or auto-revert-notify-watch-descriptor
+ (string-match auto-revert-notify-exclude-dir-regexp
+ (expand-file-name default-directory))
+ (file-symlink-p (or buffer-file-name default-directory)))
+ ;; Check, whether this has been activated already.
+ (let ((file (if buffer-file-name
+ (expand-file-name buffer-file-name default-directory)
+ (expand-file-name default-directory))))
+ (maphash
+ (lambda (key _value)
+ (when (and
+ (file-notify-valid-p key)
+ (equal (file-notify--watch-absolute-filename
+ (gethash key file-notify-descriptors))
+ (directory-file-name file))
+ (equal (file-notify--watch-callback
+ (gethash key file-notify-descriptors))
+ 'auto-revert-notify-handler))
+ (setq auto-revert-notify-watch-descriptor key)))
+ auto-revert-notify-watch-descriptor-hash-list)
+ ;; Create a new watch if needed.
+ (unless auto-revert-notify-watch-descriptor
+ (setq auto-revert-notify-watch-descriptor
+ (ignore-errors
(file-notify-add-watch
- (expand-file-name default-directory)
- '(change)
- 'auto-revert-notify-handler))))
- (if auto-revert-notify-watch-descriptor
- (progn
- (puthash
- auto-revert-notify-watch-descriptor
- (cons (current-buffer)
- (gethash auto-revert-notify-watch-descriptor
- auto-revert-notify-watch-descriptor-hash-list))
- auto-revert-notify-watch-descriptor-hash-list)
- (add-hook 'kill-buffer-hook
- #'auto-revert-notify-rm-watch nil t))
- ;; Fallback to file checks.
- (setq-local auto-revert-use-notify nil)))))
+ file
+ (if buffer-file-name '(change attribute-change) '(change))
+ 'auto-revert-notify-handler))))
+ (when auto-revert-notify-watch-descriptor
+ (setq auto-revert-notify-modified-p t)
+ (puthash
+ auto-revert-notify-watch-descriptor
+ (cons (current-buffer)
+ (gethash auto-revert-notify-watch-descriptor
+ auto-revert-notify-watch-descriptor-hash-list))
+ 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
;; immediately when a notification occurs. Since file updates can happen very
@@ -611,8 +608,7 @@ no more reverts are possible until the next call of
(file-name-nondirectory buffer-file-name)))
;; A buffer w/o a file, like dired.
(null buffer-file-name)))
- (auto-revert-notify-rm-watch)
- (setq-local auto-revert-use-notify nil))))
+ (auto-revert-notify-rm-watch))))
;; Loop over all buffers, in order to find the intended one.
(cl-dolist (buffer buffers)
@@ -642,20 +638,15 @@ no more reverts are possible until the next call of
auto-revert-buffers-counter)
(auto-revert-handler)
(setq auto-revert-buffers-counter-lockedout
- auto-revert-buffers-counter))
-
- ;; No need to check other buffers.
- (cl-return)))))))))
+ auto-revert-buffers-counter))))))))))
(defun auto-revert-active-p ()
"Check if auto-revert is active (in current buffer or globally)."
(or auto-revert-mode
auto-revert-tail-mode
- (and
- global-auto-revert-mode
- (not global-auto-revert-ignore-buffer)
- (not (memq major-mode
- global-auto-revert-ignore-modes)))))
+ (and global-auto-revert-mode
+ (not global-auto-revert-ignore-buffer)
+ (not (memq major-mode global-auto-revert-ignore-modes)))))
(defun auto-revert-handler ()
"Revert current buffer, if appropriate.
@@ -669,14 +660,14 @@ This is an internal function used by Auto-Revert Mode."
(if buffer-file-name
(and (or auto-revert-remote-files
(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)
(if auto-revert-tail-mode
(and (file-readable-p buffer-file-name)
(/= auto-revert-tail-pos
(setq size
- (nth 7 (file-attributes
- buffer-file-name)))))
+ (file-attribute-size
+ (file-attributes buffer-file-name)))))
(funcall (or buffer-stale-function
#'buffer-stale--default-function)
t)))
@@ -719,7 +710,8 @@ This is an internal function used by Auto-Revert Mode."
;; `preserve-modes' avoids changing the (minor) modes. But we do
;; want to reset the mode for VC, so we do it manually.
(when (or revert auto-revert-check-vc-info)
- (vc-refresh-state))))
+ (let ((revert-buffer-in-progress-p t))
+ (vc-refresh-state)))))
(defun auto-revert-tail-handler (size)
(let ((modified (buffer-modified-p))
@@ -813,7 +805,8 @@ the timer when no buffers need to be checked."
;; Check if we should cancel the timer.
(when (and (not global-auto-revert-mode)
(null auto-revert-buffer-list))
- (cancel-timer auto-revert-timer)
+ (if (timerp auto-revert-timer)
+ (cancel-timer auto-revert-timer))
(setq auto-revert-timer nil)))))