Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Ispell and Abbrev, the Perfect Auto-Correct

I am not a fantastic typist. My speed is acceptable, but I make a great deal of mistakes. The following snippet has turned me into the Messi of keyboards.

Whenever I make a typo:

  1. Hit C-x C-i, instead of erasing the mistake;
  2. Select the appropriate correction (thanks to Ispell);
  3. Sleep easier at night knowing I'll never see that mistake again (thanks to abbrev).
(define-key ctl-x-map "\C-i"

(defun endless/simple-get-word ()
  (car-safe (save-excursion (ispell-get-word nil))))

(defun endless/ispell-word-then-abbrev (p)
  "Call `ispell-word', then create an abbrev for it.
With prefix P, create local abbrev. Otherwise it will
be global.
If there's nothing wrong with the word at point, keep
looking for a typo until the beginning of buffer. You can
skip typos you don't want to fix with `SPC', and you can
abort completely with `C-g'."
  (interactive "P")
  (let (bef aft)
      (while (if (setq bef (endless/simple-get-word))
                 ;; Word was corrected or used quit.
                 (if (ispell-word nil 'quiet)
                     nil ; End the loop.
                   ;; Also end if we reach `bob'.
                   (not (bobp)))
               ;; If there's no word at point, keep looking
               ;; until `bob'.
               (not (bobp)))
      (setq aft (endless/simple-get-word)))
    (if (and aft bef (not (equal aft bef)))
        (let ((aft (downcase aft))
              (bef (downcase bef)))
            (if p local-abbrev-table global-abbrev-table)
            bef aft)
          (message "\"%s\" now expands to \"%s\" %sally"
                   bef aft (if p "loc" "glob")))
      (user-error "No typo at or before point"))))

(setq save-abbrevs 'silently)
(setq-default abbrev-mode t)

In my innocence, I had initially assigned this command to a 3-key sequence. It took me less then a month to realise it needed a shorter bind, I'd been using it 30 times a day.

Auto correction with abbrev is far from a new concept. Do you use anything similar?

Update 11 Jan 2016

The command now searches backward for the closest wrong word. So you can just hit C-x C-i even if the mistake happened several words ago.

Update 20 Jan 2016

Fixed some corner-case issues with the previous update.

comments powered by Disqus