branch: externals/org-modern
commit 3cba802e1ce9bfb5674a13b97642fa66f0154837
Author: JD Smith <93749+jdtsm...@users.noreply.github.com>
Commit: JD Smith <93749+jdtsm...@users.noreply.github.com>

    give up after 5 init attempts, and reuse timer
---
 org-modern-indent.el | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/org-modern-indent.el b/org-modern-indent.el
index 445288f4fa..1389e6576f 100644
--- a/org-modern-indent.el
+++ b/org-modern-indent.el
@@ -157,16 +157,28 @@ of the returned vector.  If PREFIX is nil or empty, nil 
is returned."
                                         'display org-modern-indent-guide))
                  t))))))))
 
+(defvar-local org-modern-indent--init nil)
 (defun org-modern-indent--wait-and-refresh (buf)
   "Wait for org-indent to finish initializing BUF, then refresh."
   (if (or (not (bound-and-true-p org-indent-agentized-buffers))
          (not (memq buf org-indent-agentized-buffers)))
-      (progn
-       (when (buffer-live-p buf)       ; org-capture buffers vanish fast
-         (with-current-buffer buf
-           (font-lock-add-keywords nil org-modern-indent--font-lock-keywords t)
-           (font-lock-flush))))
-    (run-at-time 0.1 nil #'org-modern-indent--wait-and-refresh buf)))
+      (when (buffer-live-p buf)             ; org-capture buffers vanish fast
+       (with-current-buffer buf
+         (font-lock-add-keywords nil org-modern-indent--font-lock-keywords t)
+         (font-lock-flush)))
+    ;; still waiting
+    (with-current-buffer buf
+      (if org-modern-indent--init
+         (let ((cnt (cl-incf (cadr org-modern-indent--init))))
+           (if (> cnt 5)
+               (user-error
+                "org-modern-indent: Gave up waiting for %s to initialize" buf)
+             (timer-activate (timer-set-time (car org-modern-indent--init)
+                                             (time-add (current-time) 0.2)))))
+       (setq
+        org-modern-indent--init
+        (list (run-at-time 0.1 nil #'org-modern-indent--wait-and-refresh buf)
+              1))))))
 
 (defun org-modern-indent--refresh ()
   "Unfontify entire buffer and refresh line prefix."

Reply via email to