d12frosted / flyspell-correct

Distraction-free words correction with flyspell via selected interface.
Other
200 stars 14 forks source link

Highlight the word that is currently being corrected #52

Closed fikovnik closed 4 years ago

fikovnik commented 5 years ago

First, thank you for this package! Perhaps it can be achieved by other means, but I was wondering if it would be possible to add a custom face that would highlight the word that is currently being corrected?

d12frosted commented 5 years ago

Hey Filip,

That's a great idea. Thank you for sharing it. I am in travel mode, and will implement it once I am back to route.

On Sat, May 25, 2019 at 19:50, Filip Krikava notifications@github.com wrote:

First, thank you for this package! Perhaps it can be achieved by other means, but I was wondering if it would be possible to add a custom face that would highlight the word that is currently being corrected?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

hubisan commented 4 years ago

Was thinking the same and noticed that ispell-word is doing this. So I copied what is needed from ispell-word and added an advice before and after flyspell-correct-at-point to highlight and unhighlight the current spelling error. Seems to work so far.

When ispell-lazy-highlight is non-nil (default), all text in the buffer matching the current spelling error is highlighted lazily using isearch lazy highlighting. This might be a little bit strange in a prog-mode as not only words in comments or strings are highlighted.

This would probably be trivial to add as a feature. Don't have the time to make a pull request unfortunately.

(defvar my-feat-spell-check/highlight-spelling-window nil
  "Window which has highlighted spellings.")

(defun my-feat-spell-check/flyspell-overlay-start-and-end ()
  "Return `cons' with start and end of `flyspell' overlay at point.
Returns nil if no overlay is found."
  (let ((ovs (overlays-at (point)))
        ov)
    (while (and (not ov) ovs)
      (let ((current (pop ovs)))
        (when (flyspell-overlay-p current)
          (setq ov current))))
    (when ov
      (let ((ov-start (overlay-start ov))
            (ov-end (overlay-end ov)))
        (cons ov-start ov-end)))))

(defun my-feat-spell-check/highlight-spelling-error ()
  "Highlight the spelling error at point."
  (setq my-feat-spell-check/highlight-spelling-window (selected-window))
  (when-let ((ov (my-feat-spell-check/flyspell-overlay-start-and-end))
             (ov-start (car ov))
             (ov-end (cdr ov)))
    (ispell-highlight-spelling-error ov-start ov-end t)))

(defun my-feat-spell-check/unhighlight-spelling-error ()
  "Highlight the spelling error at point."
  (save-window-excursion
    (select-window my-feat-spell-check/highlight-spelling-window)
    (ispell-highlight-spelling-error nil nil)))

(advice-add 'flyspell-correct-at-point :before #'my-feat-spell-check/highlight-spelling-error)
(advice-add 'flyspell-correct-at-point :after  #'my-feat-spell-check/unhighlight-spelling-error)
d12frosted commented 4 years ago

Wow, thank you for sharing the code. Will take a look on my weekends.

d12frosted commented 4 years ago

I tried it, but it seems it doesn't play nicely with overlays from flyspell.

hubisan commented 4 years ago

Works here. What you mean by not playing nicely so I can test it in my setup?

Forgot to mention, had to set an ispell variable (had to reload emacs after).:

(setq ispell-highlight-face 'isearch)
;; defcustom ispell-highlight-face (if ispell-lazy-highlight 'isearch 'highlight)

Before correct: image

While correcting (avy-menu interface): image

Afterwards: image

d12frosted commented 4 years ago

Ok, there is something funky with my faces, that's why I didn't see the difference. Thanks for pointing out the value of ispell-highlight-face.

d12frosted commented 4 years ago

@hubisan I've created a PR based on your code. Would be grateful for your review ;)

d12frosted commented 4 years ago

@fikovnik, @gusbrs, @hubisan the feature is now available on master. Give some time for MELPA to build it :)

P. S. Don't have any plans for stable MELPA. So let me know if you really-really need it there.

gusbrs commented 4 years ago

I'm trying it out here already, and it looks very nice indeed.

Thank you very much @d12frosted and @hubisan !