autocorrect magic
authorJoerg Jaspert <joerg@debian.org>
Fri, 4 Mar 2016 20:40:01 +0000 (21:40 +0100)
committerJoerg Jaspert <joerg@debian.org>
Fri, 4 Mar 2016 20:42:34 +0000 (21:42 +0100)
.emacs.d/config/emacs.org

index d1fa7a9..4713aec 100644 (file)
@@ -670,6 +670,48 @@ ways.
                 (add-hook 'expand-expand-hook 'indent-according-to-mode)
                 (add-hook 'expand-jump-hook 'indent-according-to-mode)))))
 #+END_SRC
+** autocorrect
+[2016-02-15 Mon 22:19]
+See [[http://endlessparentheses.com/ispell-and-abbrev-the-perfect-auto-correct.html][Ispell and Abbrev, the Perfect Auto-Correct]].
+#+BEGIN_SRC emacs-lisp
+(define-key ctl-x-map "\C-i"
+  #'endless/ispell-word-then-abbrev)
+
+(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)
+    (save-excursion
+      (while (if (setq bef (thing-at-point '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)))
+        (backward-word))
+      (setq aft (thing-at-point 'word)))
+    (if (and aft bef (not (equal aft bef)))
+        (let ((aft (downcase aft))
+              (bef (downcase bef)))
+          (define-abbrev
+            (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)
+#+END_SRC
 ** avy-mode
 [2013-04-28 So 11:26]
 avy is a GNU Emacs package for jumping to visible text using a char-based decision tree.