13

I edit a lot of MoinMoinWiki pages in my emacs and love the flyspell-mode. Preformatted stuff in {{{...}}} (in multiple lines) as well as "backtick text backtick" usually contains snippets of programming code which make no sense to spellcheck.

Can I configure ispell/flyspell not to include the programming code?

Example:

Bla bla lorem ipsum die Standardcontainer wie `vector` eine
''Methode'' haben, die ein einzelnes Argument nimmt, also
`vector<T>::swap(vector<T)>&)`. Bla bla und diese `swap`-Methoden sind
von dieser Sorte. Warum das so ist, sehen wir gleich. Bla bla
was '''kanonisch''' ist bla bla Template-Funktion<<tlitref(stdswap)>>

{{{#!highlight c++ title="Man könnte 'std::swap@LT@@GT@' spezialisieren"
namespace std {
  template<> // wir können durchaus im namespace std spezialisieren
  void swap<Thing>(Thing&, Thing&) {
    // ...hier swappen...
  }
}
}}}

Nun, das würde sicherlich in diesem Fall helfen, doch es bleibt ein
größeres Problem: Eine teilweise Spezialisierung lorem ipsum bla bla

1 Answer 1

18

The variable ispell-skip-region-alist does what you want when spell checking the buffer, but not for flyspell. Just add an entry like

(add-to-list 'ispell-skip-region-alist
             '("^{{{" . "^}}}"))

Unfortunately, it's not as easy to get flyspell to ignore certain regions. You have to use flyspell-generic-check-word-predicate which is a function. Several modes already define this so you would have to add the following as advice to those functions. I'll assume for simplicity though that you are using a mode (I used text-mode below) which doesn't have it defined. Then you can add the following to your .emacs:

(defun flyspell-ignore-verbatim ()
  "Function used for `flyspell-generic-check-word-predicate' to ignore {{{ }}} blocks."
  (save-excursion
    (widen)
    (let ((p (point))
          (count 0))
      (not (or (and (re-search-backward "^{{{" nil t)
                    (> p (point))
                    ;; If there is no closing }}} then assume we're still in it
                    (or (not (re-search-forward "^}}}" nil t))
                        (< p (point))))
               (eq 1 (progn (while (re-search-backward "`" (line-beginning-position) t)
                              (setq count (1+ count)))
                            (- count (* 2 (/ count 2))))))))))
(put 'text-mode 'flyspell-mode-predicate 'flyspell-ignore-verbatim)
5
  • perfect! My mode-line said (Fundamental Fly). Hooking it to flyspell-mode did not work, but fundamental-mode instead of text-mode seems fine.
    – towi
    Commented Oct 20, 2011 at 8:02
  • Hmm... how to I handle both: the ^{{{...^}}} regex and the Backtick-word-Backtick one?
    – towi
    Commented Oct 20, 2011 at 8:06
  • I added support for the backtick-text-backtick. It assumes that such statements only appear on a single line—it counts whether there is an even or odd number of backticks on the line before it. Commented Oct 23, 2011 at 20:11
  • What should I do if the I want to ignore ispell inside \href{ }?
    – alper
    Commented Jan 11, 2023 at 13:51
  • Basically, change the regular expressions from ^{{{ and ^}}} to \\href{ and }. There will probably other differences, but fitting them in a comment would be difficult. You would either add it as another statement in the (or ...) or remove the other expressions from the (or ...) depending on if you want to ignore those too. Commented Jan 23, 2023 at 6:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .