Subject: bug#14419: 24.3.50; ispell behaves badly with some TeX documents

From: Ivan Andrus <[email-masked]>
Date: Fri, 17 May 2013 23:21:26 -0600

ispell can overlook misspellings and (more annoyingly though easier to 
detect) can start spell checking everything in math mode.

1. emacs -Q

2. Edit the following document in latex-mode (built in or AUCTeX):

% \documentclass[12pt,draft,a4paper]{report}
\documentclass[12pt,a4paper]{report}

\begin{document}

Representation theory concerns itself with the cases when $Y$ is the general linear group $\GL_{n}(k)$, for some field $k$.

spelling is basd $badmath$ here

\end{document}

3. Run ispell-buffer

4. It asks you to correct the word badmath.  This is in math mode and
  should be skipped.  Moreover, it missed basd outside of math mode.

5. Delete the commented documentclass line (or move it after the other)
  and things work as they are supposed to.

I ran ispell-buffer-with-debug and it seems that it skips the region
starting with \documentclass and ending with \begin{document}, but since
there was a comment before it the first line gets treated as a comment.
This is bad in and of itself if that's what's happening.  However,
because the _ in $\GL_{n}(k)$ matches the final element of
`ispell-skip-region-alist' (not sure what the purpose of that is), the
first part of the line is a comment.  That last part of the line, namely

{n}(k)$, for some field $k$.

is the first line that the spell checker sees as not a comment and
therefore it gets confused about what is math and what isn't.  Other
cases of skipping in comments can also lead to bad scenarios.  For
example in the case below no misspellings are found:

% \addcontentsline{1}{2}
spelling is basd $badmath$ here.

Perhaps it could be fixed by a small change to ispell-region to test if
the skip region is in a comment and if so, not skip that region since
comments can contain all sorts of malformed TeX.  I would try this, but
I couldn't find a canonical way to test if point is inside a comment.
Feel free to point me to one and I'll give it a shot.

-Ivan