]> git.eshelyaron.com Git - emacs.git/commitdiff
(eglot--track-changes-signal): Improve last fix (bug#70541)
authorStefan Monnier <monnier@iro.umontreal.ca>
Sat, 4 May 2024 21:14:46 +0000 (17:14 -0400)
committerEshel Yaron <me@eshelyaron.com>
Mon, 6 May 2024 16:39:15 +0000 (18:39 +0200)
* lisp/progmodes/eglot.el (eglot--add-one-shot-hook): New function.
(eglot--track-changes-signal): Use it.

(cherry picked from commit 043bb36312039f60a464b918daa1dd214cd369f1)

lisp/progmodes/eglot.el

index 8db27a130cd38258b4517952309d0651a01c6b20..89e0aa63db40a001431b7631726c78e6bec68a22 100644 (file)
@@ -2680,6 +2680,15 @@ Records BEG, END and PRE-CHANGE-LENGTH locally."
                     ,(buffer-substring-no-properties beg end))
                   eglot--recent-changes))))))
 
+(defun eglot--add-one-shot-hook (hook function &optional append local)
+  "Like `add-hook' but calls FUNCTION only once."
+  (let* ((fname (make-symbol (format "eglot--%s-once" function)))
+         (fun (lambda (&rest args)
+                (remove-hook hook fname local)
+                (apply function args))))
+    (fset fname fun)
+    (add-hook hook fname append local)))
+
 (defun eglot--track-changes-signal (id &optional distance)
   (cond
    (distance
@@ -2698,15 +2707,13 @@ Records BEG, END and PRE-CHANGE-LENGTH locally."
              (when eglot--managed-mode
                (if (and (fboundp 'track-changes-inconsistent-state-p)
                         (track-changes-inconsistent-state-p))
-                   ;; Not a good time (e.g. in the middle of Quail
-                   ;; thingy, bug#70541), let's reschedule.
-                   ;; Ideally, we'd `run-with-idle-timer' to call
-                   ;; ourselves again but it's kind of a pain to do that
-                   ;; right (because we first have to wait for the
-                   ;; current idle period to end), so we just do
-                   ;; nothing and wait for the next buffer change to
-                   ;; reschedule us.
-                   nil
+                   ;; Not a good time (e.g. in the middle of Quail thingy,
+                   ;; bug#70541): reschedule for the next idle period.
+                (eglot--add-one-shot-hook
+                 'post-command-hook
+                 (lambda ()
+                   (eglot--when-live-buffer buf
+                     (eglot--track-changes-signal id))))
                  (run-hooks 'eglot--document-changed-hook)
                  (setq eglot--change-idle-timer nil)))))
          (current-buffer))))