From: Stefan Monnier Date: Sat, 4 May 2024 21:14:46 +0000 (-0400) Subject: (eglot--track-changes-signal): Improve last fix (bug#70541) X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4e1bb55027a04a330a9b3c186d6b6ad0fdc4ebb6;p=emacs.git (eglot--track-changes-signal): Improve last fix (bug#70541) * lisp/progmodes/eglot.el (eglot--add-one-shot-hook): New function. (eglot--track-changes-signal): Use it. (cherry picked from commit 043bb36312039f60a464b918daa1dd214cd369f1) --- diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 8db27a130cd..89e0aa63db4 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -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))))