unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Albinus <michael.albinus@gmx.de>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Spencer Baugh <sbaugh@catern.com>, 44639-done@debbugs.gnu.org
Subject: bug#44639: [PATCH 2/2] autorevert: map each watch descriptor to a single buffer
Date: Thu, 28 Jan 2021 15:19:33 +0100	[thread overview]
Message-ID: <871re55ekq.fsf@gmx.de> (raw)
In-Reply-To: <87pn1p68oj.fsf@gnus.org> (Lars Ingebrigtsen's message of "Thu, 28 Jan 2021 04:29:16 +0100")

[-- Attachment #1: Type: text/plain, Size: 617 bytes --]

Lars Ingebrigtsen <larsi@gnus.org> writes:

Hi,

> Patch 1/2 was applied to Emacs 28 some time ago (and works fine), so
> this was just a followup on 2/2 -- whether this clean-up patch (which
> apparently doesn't work correctly, according to Michael) is going to get
> any further work done, or whether this issue should just be closed...

I've reworked the patch, using an association list instead of a hash
table. By this, I've fixed also the error which was always evident, but
uncovered only by Spencer's patch.

Pushed to master, I'm closing the bug. The patch itself is appended
below.

Best regards, Michael.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 8816 bytes --]

*** /tmp/ediffGTBgTB	2021-01-28 15:18:21.881661122 +0100
--- /home/albinus/src/emacs/lisp/autorevert.el	2021-01-28 15:06:38.034456811 +0100
***************
*** 355,364 ****
  (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)
!   "A hash table mapping notification descriptors to lists of buffers.
! The buffers use that descriptor for auto-revert notifications.
  The key is equal to `auto-revert-notify-watch-descriptor' in each
  buffer.")

--- 355,363 ----
  (add-hook 'after-set-visited-file-name-hook
            #'auto-revert-set-visited-file-name)

! (defvar auto-revert--buffer-by-watch-descriptor nil
!   "An association list mapping notification descriptors to buffers.
! The buffer uses that descriptor for auto-revert notifications.
  The key is equal to `auto-revert-notify-watch-descriptor' in each
  buffer.")

***************
*** 630,645 ****

  (defun auto-revert-notify-rm-watch ()
    "Disable file notification for current buffer's associated file."
!   (let ((desc auto-revert-notify-watch-descriptor)
!         (table auto-revert--buffers-by-watch-descriptor))
!     (when desc
!       (let ((buffers (delq (current-buffer) (gethash desc table))))
!         (if buffers
!             (puthash desc buffers table)
!           (remhash desc table)))
!       (ignore-errors
! 	(file-notify-rm-watch desc))
!       (remove-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch t)))
    (setq auto-revert-notify-watch-descriptor nil
  	auto-revert-notify-modified-p nil))

--- 629,640 ----

  (defun auto-revert-notify-rm-watch ()
    "Disable file notification for current buffer's associated file."
!   (when-let ((desc auto-revert-notify-watch-descriptor))
!     (setq auto-revert--buffer-by-watch-descriptor
!           (assoc-delete-all desc auto-revert--buffer-by-watch-descriptor))
!     (ignore-errors
!       (file-notify-rm-watch desc))
!     (remove-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch t))
    (setq auto-revert-notify-watch-descriptor nil
  	auto-revert-notify-modified-p nil))

***************
*** 660,672 ****
                 (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--buffers-by-watch-descriptor))
!          auto-revert--buffers-by-watch-descriptor)
          (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
--- 655,664 ----
                 (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
!               auto-revert--buffer-by-watch-descriptor
!               (cons (cons auto-revert-notify-watch-descriptor (current-buffer))
!                     auto-revert--buffer-by-watch-descriptor))
          (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
***************
*** 696,703 ****
  	   (action (nth 1 event))
  	   (file (nth 2 event))
  	   (file1 (nth 3 event)) ;; Target of `renamed'.
! 	   (buffers (gethash descriptor
! 			     auto-revert--buffers-by-watch-descriptor)))
        ;; Check, that event is meant for us.
        (cl-assert descriptor)
        ;; Since we watch a directory, a file name must be returned.
--- 688,695 ----
  	   (action (nth 1 event))
  	   (file (nth 2 event))
  	   (file1 (nth 3 event)) ;; Target of `renamed'.
! 	   (buffer (alist-get descriptor auto-revert--buffer-by-watch-descriptor
!                               nil nil #'equal)))
        ;; Check, that event is meant for us.
        (cl-assert descriptor)
        ;; Since we watch a directory, a file name must be returned.
***************
*** 706,714 ****
        (when auto-revert-debug
          (message "auto-revert-notify-handler %S" event))

!       (if (eq action 'stopped)
!           ;; File notification has stopped.  Continue with polling.
!           (cl-dolist (buffer buffers)
              (with-current-buffer buffer
                (when (or
                       ;; A buffer associated with a file.
--- 698,706 ----
        (when auto-revert-debug
          (message "auto-revert-notify-handler %S" event))

!       (when (buffer-live-p buffer)
!         (if (eq action 'stopped)
!             ;; File notification has stopped.  Continue with polling.
              (with-current-buffer buffer
                (when (or
                       ;; A buffer associated with a file.
***************
*** 721,758 ****
                  (auto-revert-notify-rm-watch)
                  ;; Restart the timer if it wasn't running.
                  (unless auto-revert-timer
!                   (auto-revert-set-timer)))))

!         ;; Loop over all buffers, in order to find the intended one.
!         (cl-dolist (buffer buffers)
!           (when (buffer-live-p buffer)
!             (with-current-buffer buffer
!               (when (or
!                      ;; A buffer associated with a file.
!                      (and (stringp buffer-file-name)
!                           (or
!                            (and (memq
!                                  action '(attribute-changed changed created))
!                                 (string-equal
!                                  (file-name-nondirectory file)
!                                  (file-name-nondirectory buffer-file-name)))
!                            (and (eq action 'renamed)
!                                 (string-equal
!                                  (file-name-nondirectory file1)
!                                  (file-name-nondirectory buffer-file-name)))))
!                      ;; A buffer w/o a file, like dired.
!                      (and (null buffer-file-name)
!                           (memq action '(created renamed deleted))))
!                 ;; Mark buffer modified.
!                 (setq auto-revert-notify-modified-p t)
!
!                 ;; Revert the buffer now if we're not locked out.
!                 (unless auto-revert--lockout-timer
!                   (auto-revert-handler)
!                   (setq auto-revert--lockout-timer
!                         (run-with-timer
!                          auto-revert--lockout-interval nil
!                          #'auto-revert--end-lockout buffer)))))))))))

  (defun auto-revert--end-lockout (buffer)
    "End the lockout period after a notification.
--- 713,747 ----
                  (auto-revert-notify-rm-watch)
                  ;; Restart the timer if it wasn't running.
                  (unless auto-revert-timer
!                   (auto-revert-set-timer))))

!           (with-current-buffer buffer
!             (when (or
!                    ;; A buffer associated with a file.
!                    (and (stringp buffer-file-name)
!                         (or
!                          (and (memq
!                                action '(attribute-changed changed created))
!                               (string-equal
!                                (file-name-nondirectory file)
!                                (file-name-nondirectory buffer-file-name)))
!                          (and (eq action 'renamed)
!                               (string-equal
!                                (file-name-nondirectory file1)
!                                (file-name-nondirectory buffer-file-name)))))
!                    ;; A buffer w/o a file, like dired.
!                    (and (null buffer-file-name)
!                         (memq action '(created renamed deleted))))
!               ;; Mark buffer modified.
!               (setq auto-revert-notify-modified-p t)
!
!               ;; Revert the buffer now if we're not locked out.
!               (unless auto-revert--lockout-timer
!                 (auto-revert-handler)
!                 (setq auto-revert--lockout-timer
!                       (run-with-timer
!                        auto-revert--lockout-interval nil
!                        #'auto-revert--end-lockout buffer))))))))))

  (defun auto-revert--end-lockout (buffer)
    "End the lockout period after a notification.

  reply	other threads:[~2021-01-28 14:19 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-14 16:54 bug#44638: [PATCH 1/2] autorevert: don't reuse existing watch descriptors Spencer Baugh
2020-11-14 16:54 ` bug#44639: [PATCH 2/2] autorevert: map each watch descriptor to a single buffer Spencer Baugh
2020-12-02 14:50   ` Michael Albinus
2021-01-27  3:59     ` Lars Ingebrigtsen
2021-01-27 16:34       ` Spencer Baugh
2021-01-28  3:29         ` Lars Ingebrigtsen
2021-01-28 14:19           ` Michael Albinus [this message]
2020-11-14 17:22 ` bug#44638: [PATCH 1/2] autorevert: don't reuse existing watch descriptors Eli Zaretskii
2020-11-14 21:19   ` Spencer Baugh
2020-11-30 18:01     ` Spencer Baugh
2020-11-30 18:21       ` Eli Zaretskii
2020-11-30 18:31         ` Michael Albinus
2020-12-01 20:16           ` Michael Albinus
2020-12-02  3:20             ` Eli Zaretskii
2020-12-02 15:17               ` Michael Albinus
2020-12-02 15:41                 ` Eli Zaretskii
2020-12-02 17:05                   ` Michael Albinus
2020-12-02 17:13                     ` Eli Zaretskii
2020-12-02 17:20                       ` Michael Albinus
2020-12-02 17:43                         ` Eli Zaretskii
2020-12-03 15:01                         ` Michael Albinus
2020-12-02 17:46                       ` Dmitry Gutov

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871re55ekq.fsf@gmx.de \
    --to=michael.albinus@gmx.de \
    --cc=44639-done@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=sbaugh@catern.com \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).