1

flyspell-auto-correct-previous-word is a great way to quickly correct mistakes in a buffer. I get a lot of false positives though, and I would like to quickly add them to my dictionary in an analogous way.

How can I make a command to do this? Confusingly, flyspell-auto-correct-previous-pos is always nil for me.

1

If I understand your question correctly this should solve it. The add-false-postive command jumps back to the previous error, adds it to the dict, cleans up and jumps back.

(defun goto-previous-spelling-error ()
  "Go to previous spelling error."
  (interactive)
  (push-mark (point) t nil)
  (let ((pos (point))
        (min (point-min)))
    (while (and (> pos min)
                (let ((ovs (overlays-at pos))
                      (r '()))
                  (while (and (not r) (consp ovs))
                    (if (flyspell-overlay-p (car ovs))
                        (setq r t)
                      (setq ovs (cdr ovs))))
                  (not r)))
      (backward-word 1)
      (setq pos (point)))
    (goto-char pos)))

(defun add-false-postive ()
  "Add previous false positive to dict"
  (interactive)
  (goto-previous-spelling-error)
  (let* ((flyspell-info (flyspell-get-word))
         (word (car flyspell-info))
         (bounds (cdr flyspell-info)))
    (flyspell-do-correct 'save nil word (point) (car bounds) (cadr bounds) (point))
    (flyspell-delete-region-overlays (car bounds) (cadr bounds))
    (exchange-point-and-mark)
    (deactivate-mark))
  • This is excellent. I would recommend adding (deactivate-mark) to your add-false-positive function body, as currently it leaves the mark at the last flyspell error, which looks odd for people using transient mark mode. – Matthew Piziak Sep 23 '15 at 18:56
  • @MatthewPiziak Ah yes, I have added it, thanks. – clemera Sep 23 '15 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.