Subject: bug#74437: 30.0.92; completion-preview-idle-delay is delayed by flyspell

From: Eshel Yaron <[email-masked]>
Date: Tue, 19 Nov 2024 19:40:07 +0100

Hi,

Eli Zaretskii <[email-masked]> writes:

>> Cc: [email-masked]
>> Date: Tue, 19 Nov 2024 17:13:16 +0100
>> From:  Eshel Yaron via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> The root cause is a peculiarity of flyspell-mode: it calls sit-for and
>> blocks Emacs for 3 whole seconds (by default, see flyspell-delay) after
>> certain commands, including after (org-)self-insert-command.  This also
>> blocks the idle timer that Completion Preview mode uses, unfortunately.
>> 
>> A quick search shows that this behavior of flyspell affects other
>> features as well.  For example, IIUC, auto completion in Corfu switched
>> to using run-at-time instead of run-with-idle-timer due to this issue.
>> 
>> I think flyspell should be modified to use a timer instead of sit-for,
>> so as to avoid blocking idle timers.  I can come up with such a patch,
>> but it's not quite trivial, so I wonder what others think about this
>> issue and how it should be addressed.
>
> This is likely to cause differences in behavior that some users will
> be unhappy about (how do you support delaying only after some
> commands? and how do you interact with async subprocess from an idle
> timer?).  So if you want to install such a patch, it must be an opt-in
> feature, so we could let users try it for some time before we decide
> whether to retire the old behavior based on sit-for.

All right, something like this?

diff --git a/lisp/textmodes/flyspell.el b/lisp/textmodes/flyspell.el
index 7bf9cb1ae9d..7ba5bb871ea 100644
--- a/lisp/textmodes/flyspell.el
+++ b/lisp/textmodes/flyspell.el
@@ -810,6 +810,13 @@ flyspell-check-changed-word-p
              (let ((pos (point)))
                (or (>= pos start) (<= pos stop) (= pos (1+ stop))))))))
 
+(defcustom flyspell-blocking-delay t
+  "Whether to block Emacs while Flyspell waits after a delayed command."
+  :type 'boolean
+  :version "30.1")
+
+(defvar flyspell--timer nil)
+
 ;;*---------------------------------------------------------------------*/
 ;;*    flyspell-check-word-p ...                                        */
 ;;*---------------------------------------------------------------------*/
@@ -844,7 +851,15 @@ flyspell-check-word-p
 	;; The current command is not delayed, that
 	;; is that we must check the word now.
 	(and (not unread-command-events)
-	     (sit-for flyspell-delay)))
+             (if flyspell-blocking-delay
+                 (sit-for flyspell-delay)
+               (setq flyspell--timer
+                     (run-with-idle-timer
+                      flyspell-delay nil
+                      (lambda (buffer)
+                        (when (eq (current-buffer) buffer) (flyspell-word)))
+                      (current-buffer)))
+               nil)))
        (t t)))
      (t t))))
 
@@ -955,6 +970,7 @@ flyspell-debug-signal-changed-checked
 (defun flyspell-post-command-hook ()
   "The `post-command-hook' used by flyspell to check a word on-the-fly."
   (interactive)
+  (when (timerp flyspell--timer) (cl-callf cancel-timer flyspell--timer))
   (when flyspell-mode
     (with-local-quit
       (let ((command this-command)
@@ -1179,7 +1195,7 @@ flyspell-word
 		  (set-process-query-on-exit-flag ispell-process nil)
                   ;; Wait until ispell has processed word.
                   (while (progn
-                           (accept-process-output ispell-process)
+                           (accept-process-output ispell-process 1)
                            (not (string= "" (car ispell-filter)))))
                   ;; (ispell-send-string "!\n")
                   ;; back to terse mode.