When I use flyspell-mode
, it reports spelling errors whenever I type a URL. Is there a way I can tell Flyspell to stop checking URLs?
2 Answers
After a bit of digging, I found a hint in [this Superuser.com answer: you need to set flyspell-mode-predicate
to a function that will decide whether words should be checked or not. Here's a way to get Flyspell to ignore anything starting with "http" or "https":
(defun flyspell-ignore-http-and-https ()
"Function used for `flyspell-generic-check-word-predicate' to ignore stuff starting with \"http\" or \"https\"."
(save-excursion
(forward-whitespace -1)
(when (looking-at " ")
(forward-char)
(not (looking-at "https?\\b")))))
(put 'text-mode 'flyspell-mode-predicate 'flyspell-ignore-http-and-https)
There are some shortcomings, of course:
- I'm assuming that anything beginning with "http" or "https" should be skipped; that includes "http://cnn.com" and "https://google.com" (good), but also "httpomatic" and "httpstatisticiansarehip" (presumably bad)
- I'm not bothering with mailto:, ftp:, file:, etc etc. (But that way may lie madness...)
But as a quick-and-dirty method, it should work.
-
Can we apply this in the latex-mode? and also for the url inside
href{}
?– alperCommented Jan 11, 2023 at 13:51
I've something along these lines (in my case for markdown mode) to be slightly more resistant to some common but pathological cases from Saint Aardvark the Carpeted's answer:
(require 'thingatpt)
(defun markdown-flyspell-predicate ()
(not (thing-at-point 'url)))
(put 'markdown-mode 'flyspell-mode-predicate 'markdown-flyspell-predicate)
In particular, if you start by looking at the whitespace before a word, the URL won't necessarily start with https
. Consider these cases:
(https://emacs.stackexchange.com)
[text text](https://emacs.stackexchange.com)
\url{https://emacs.stackexchange.com}
ispell
(notflyspell
) this semi-related link, regardingispell-skip-region-alist
, looks helpful: superuser.com/a/345461/206164 Perhapsflyspell
has something similar that can be implemented -- e.g., usingflyspell-mode-predicate
.