Subject: 30.0.50; Flyspell only changes

From: Juri Linkov <[email-masked]>
Date: Tue, 28 Feb 2023 19:53:06 +0200

Tags: patch

This feature is the opposite of the feature implemented by Augusto in
bug#61814.  The problem is that the default behavior of flyspell-mode
is too erratic.  It checks and highlights random words where the cursor
happens to travel while navigating the buffer.  The feature from Augusto
allows to check all words in the buffer that makes sense when the intention
of the user is to see all misspelled words to correct them.

OTOH, often there is no need to check all misspellings in the existing file,
but only newly entered text.  For such use cases extra highlighting
while moving thru the file causes too much distraction.  So here is
a new option that allow to check only text edited by the user.

diff --git a/lisp/textmodes/flyspell.el b/lisp/textmodes/flyspell.el
index 84c207b8a48..ef11c0ca326 100644
--- a/lisp/textmodes/flyspell.el
+++ b/lisp/textmodes/flyspell.el
@@ -289,6 +289,11 @@ flyspell-auto-correct-binding
   "The key binding for flyspell auto correction."
   :type 'key-sequence)
 
+(defcustom flyspell-check-changes nil
+  "Check only edited text."
+  :type 'boolean
+  :version "30.1")
+
 ;;*---------------------------------------------------------------------*/
 ;;*    Mode specific options                                            */
 ;;*    -------------------------------------------------------------    */
@@ -627,7 +632,9 @@ flyspell-mode-on
   ;; we put the `flyspell-deplacement' property on some commands
   (flyspell-deplacement-commands)
   ;; we bound flyspell action to post-command hook
-  (add-hook 'post-command-hook (function flyspell-post-command-hook) t t)
+  (if flyspell-check-changes
+      (add-hook 'post-command-hook (function flyspell-check-changes) t t)
+    (add-hook 'post-command-hook (function flyspell-post-command-hook) t t))
   ;; we bound flyspell action to pre-command hook
   (add-hook 'pre-command-hook (function flyspell-pre-command-hook) t t)
   ;; we bound flyspell action to after-change hook
@@ -732,6 +739,7 @@ flyspell-pre-command-hook
 (defun flyspell-mode-off ()
   "Turn Flyspell mode off."
   ;; We remove the hooks.
+  (remove-hook 'post-command-hook (function flyspell-check-changes) t)
   (remove-hook 'post-command-hook (function flyspell-post-command-hook) t)
   (remove-hook 'pre-command-hook (function flyspell-pre-command-hook) t)
   (remove-hook 'after-change-functions 'flyspell-after-change-function t)
@@ -1016,6 +1024,20 @@ flyspell-post-command-hook
             (setq flyspell-changes (cdr flyspell-changes))))
         (setq flyspell-previous-command command)))))
 
+(defun flyspell-check-changes ()
+  "The `post-command-hook' used by flyspell to check only edits."
+  (when flyspell-mode
+    (with-local-quit
+      (when (consp flyspell-changes)
+        (let ((start (car (car flyspell-changes)))
+              (stop  (cdr (car flyspell-changes)))
+              (word (save-excursion (flyspell-get-word))))
+          (unless (and word (<= (nth 1 word) start) (>= (nth 2 word) stop))
+            (save-excursion
+              (goto-char start)
+              (flyspell-word))
+            (setq flyspell-changes nil)))))))
+
 ;;*---------------------------------------------------------------------*/
 ;;*    flyspell-notify-misspell ...                                     */
 ;;*---------------------------------------------------------------------*/