17

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?

Matthew Piziak
  • 5,958
  • 3
  • 29
  • 77
  • 1
    For `ispell` (not `flyspell`) this semi-related link, regarding `ispell-skip-region-alist`, looks helpful: http://superuser.com/a/345461/206164 Perhaps `flyspell` has something similar that can be implemented -- e.g., using `flyspell-mode-predicate`. – lawlist Dec 15 '14 at 19:08

2 Answers2

14

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.

fommil
  • 1,750
  • 11
  • 24
1

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}
VF1
  • 203
  • 1
  • 6