nrnrnr

Emacs autocorrect with flyspell

Nov 27th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.75 KB | None | 0 0
  1. (defun endless/flyspell-word-then-abbrev (p)
  2.   "Call `ispell-word', then create an abbrev for it.
  3. With prefix P, create local abbrev. Otherwise it will
  4. be global."
  5.   (interactive "P")
  6.   (save-excursion
  7.     (if (flyspell-goto-previous-word (point))
  8.         (let ((bef (downcase (or (thing-at-point 'word)
  9.                                  "")))
  10.               aft)
  11.           (call-interactively 'ispell-word)
  12.           (setq aft (downcase
  13.                      (or (thing-at-point 'word) "")))
  14.           (unless (or (string= aft bef)
  15.                       (string= aft "")
  16.                       (string= bef ""))
  17.             (message "\"%s\" now expands to \"%s\" %sally"
  18.                      bef aft (if p "loc" "glob"))
  19.             (define-abbrev
  20.               (if p local-abbrev-table global-abbrev-table)
  21.               bef aft)))
  22.       (message "Cannot find a misspelled word"))))
  23.  
  24. (define-key ctl-x-map "\C-i"
  25.   #'endless/flyspell-word-then-abbrev)
  26.  
  27.  
  28.  
  29. ;;*---------------------------------------------------------------------*/
  30. ;;*    flyspell-goto-previous-word ...                          */
  31. ;;*---------------------------------------------------------------------*/
  32. (defun flyspell-goto-previous-word (position)
  33.   "Go to the first misspelled word that occurs before point.
  34. But don't look beyond what's visible on the screen."
  35.   (interactive "d")
  36.  
  37.   (let ((top (window-start))
  38.     (bot (window-end)))
  39.     (save-restriction
  40.       (narrow-to-region top bot)
  41.       (overlay-recenter (point))
  42.  
  43.       (add-hook 'pre-command-hook
  44.                 (function flyspell-auto-correct-previous-hook) t t)
  45.  
  46.       (unless flyspell-auto-correct-previous-pos
  47.         ;; only reset if a new overlay exists
  48.         (setq flyspell-auto-correct-previous-pos nil)
  49.  
  50.         (let ((overlay-list (overlays-in (point-min) position))
  51.               (new-overlay 'dummy-value))
  52.  
  53.           ;; search for previous (new) flyspell overlay
  54.           (while (and new-overlay
  55.                       (or (not (flyspell-overlay-p new-overlay))
  56.                           ;; check if its face has changed
  57.                           (not (eq (get-char-property
  58.                                     (overlay-start new-overlay) 'face)
  59.                                    'flyspell-incorrect))))
  60.             (setq new-overlay (car-safe overlay-list))
  61.             (setq overlay-list (cdr-safe overlay-list)))
  62.  
  63.           ;; if nothing new exits new-overlay should be nil
  64.           (if new-overlay ;; the length of the word may change so go to the start
  65.               (setq flyspell-auto-correct-previous-pos
  66.                     (overlay-start new-overlay)))))
  67.  
  68.       (if (not flyspell-auto-correct-previous-pos)
  69.           nil
  70.         (goto-char flyspell-auto-correct-previous-pos)
  71.         t))))
Advertisement
Add Comment
Please, Sign In to add comment