Search code examples
user-interfaceemacsspell-checkingflyspell

User Interaction Improvements to Flyspell Correct Word


I think the behavior of flyspell-correct-word should be extended to a command that corrects all occurrences of the misspelled word of interest. This of course is not relevant for syntactic errors in the underlying spelling correction. I don't know if aspell/ispell supports such kinds of corrections. It would also like to combine the two into a command that queries the user whether he wants to correct next occurrence in a query-replace manner (y,n,q,Y,N,!). Has anybody implemented any of these ideas?


Solution

  • Try adding the following code to your .emacs, this seems to do what you're requesting (though it does not prompt you for replacements (that seemed a bit cumbersome)):

    (setq flyspell-insert-function 'flyspell-insert-and-replace-all)
    (defvar flyspell-last-replacements nil)
    (defun flyspell-insert-and-replace-all (word)
      (unless (eq flyspell-auto-correct-pos pos) ; same check as done in flyspell-auto-correct-word
        (setq flyspell-last-replacements nil))
      (save-excursion
        (dolist (word-markers flyspell-last-replacements)
          (delete-region (car word-markers) (cdr word-markers))
          (goto-char (car word-markers))
          (insert word)))
      (insert word)
      (save-excursion
        (let ((do-replacement (not flyspell-last-replacements)))
          (while (re-search-forward (concat "\\<" flyspell-auto-correct-word "\\>") nil t)
            (replace-match word)
            ;; and, when doing first search/replace, record all the positions
            (when do-replacement
              (let ((end-marker (point-marker))
                    (begin-marker (make-marker)))
                (set-marker begin-marker (- (point) (length word)))
                (set-marker-insertion-type end-marker t)
                (set-marker-insertion-type begin-marker nil)
                (add-to-list 'flyspell-last-replacements (cons begin-marker end-marker))))))))