branch: elpa/magit
commit 85265d4d2e46d88ac1e95967a4f2bb229c48e898
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    magit-auto-revert-mode: Always delay activation until after-init-hook
    
    During startup `magit-autorevert' may unintentionally be loaded before
    the user deactivates `magit-auto-revert-mode', so it is not enough to
    delay activation until after `magit-autorevert' mode is fully loaded.
---
 lisp/magit-autorevert.el | 53 ++++++++++++++++++++++++++----------------------
 1 file changed, 29 insertions(+), 24 deletions(-)

diff --git a/lisp/magit-autorevert.el b/lisp/magit-autorevert.el
index 26f141d2683..3b0c465af78 100644
--- a/lisp/magit-autorevert.el
+++ b/lisp/magit-autorevert.el
@@ -107,30 +107,35 @@ seconds of user inactivity.  That is not desirable."
 ;;; Mode
 
 ;;;###autoload
-(progn ; magit-custom-initialize-after-init
-  (defun magit-custom-initialize-after-init (symbol value)
-    ;; Use `apply-partially' instead of the wonders of lexical bindings,
-    ;; because of bugs in the autoload handling of package managers, which
-    ;; cause these variables to be treated as dynamic.  See #5476 and #5485.
+(progn ; magit-auto-revert-mode--initialize
+  (defun magit-auto-revert-mode--initialize (symbol value)
     (internal--define-uninitialized-variable symbol)
-    (cond ((not after-init-time)
-           (letrec ((f (apply-partially
-                        (lambda (symbol value)
-                          (ignore-errors
-                            (remove-hook 'after-init-hook f))
-                          (custom-initialize-set symbol value))
-                        symbol value)))
-             (add-hook 'after-init-hook f)))
-          ((not load-file-name)
-           (custom-initialize-set symbol value))
-          ((letrec ((f (apply-partially
-                        (lambda (thisfile symbol value file)
-                          (when (equal file thisfile)
-                            (ignore-errors
-                              (remove-hook 'after-load-functions f))
-                            (custom-initialize-set symbol value)))
-                        load-file-name symbol value)))
-             (add-hook 'after-load-functions f))))))
+    (if (not load-file-name)
+        (custom-initialize-set symbol value)
+      ;; Bugs in package managers prevent the use of lexical
+      ;; bindings in autoloaded code.  See #5476 and #5485.
+      (defalias 'magit-auto-revert-mode--after-load
+        (apply-partially
+         (lambda (symbol value mode-file file)
+           (when (equal file mode-file)
+             (remove-hook 'after-load-functions
+                          'magit-auto-revert-mode--after-load)
+             (fmakunbound 'magit-auto-revert-mode--after-load)
+             (if after-init-time
+                 (custom-initialize-set symbol value)
+               ;; Delay activation in case the user disables the mode
+               ;; after loading this library but still during startup.
+               (defalias 'magit-auto-revert-mode--after-init
+                 (apply-partially
+                  (lambda (symbol value)
+                    (remove-hook 'after-init-hook
+                                 'magit-auto-revert-mode--after-init)
+                    (fmakunbound 'magit-auto-revert-mode--after-init)
+                    (custom-initialize-set symbol value))
+                  symbol value))
+               (add-hook 'after-init-hook 
'magit-auto-revert-mode--after-init))))
+         symbol value load-file-name))
+      (add-hook 'after-load-functions 'magit-auto-revert-mode--after-load))))
 
 (defun magit-turn-on-auto-revert-mode-if-desired (&optional file)
   (cond (file
@@ -159,7 +164,7 @@ seconds of user inactivity.  That is not desirable."
   :group 'magit-auto-revert
   :group 'magit-essentials
   :init-value (not (or global-auto-revert-mode noninteractive))
-  :initialize #'magit-custom-initialize-after-init)
+  :initialize #'magit-auto-revert-mode--initialize)
 
 (defun magit-auto-revert-mode--disable ()
   "When enabling `global-auto-revert-mode', disable `magit-auto-revert-mode'."

Reply via email to