Subject: bug#38152: tex-mode: paragraph separator groups text and comments

From: Michael Orlitzky <[email-masked]>
Date: Sat, 9 Nov 2019 09:29:12 -0500

When using M-q (fill-paragraph) in latex-mode from tex-mode.el, emacs 
will incorrectly wrap text into a comment block that precedes it. This 
is fairly annoying, because it can silently "delete" a huge chunk of 
your document, and leave you standing in front of a class wondering 
where your theorem went.

This was partially addressed in bug 5821,

   https://debbugs.gnu.org/cgi/bugreport.cgi?bug=5821

but only for comments that start at the beginning of a line. A minimal 
reproducer follows, and the indentation is significant:

\begin{document}
% This is a comment.
This line won't wrap into it.

   % This is also a comment.
   But this line will wrap into it.
\end{document}

This is the same problem that a stack overflow user reported,

   https://tex.stackexchange.com/questions/131774

and is perhaps also related to bug 23249,

   https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23249

which is about marking paragraphs adjacent to comments.

I don't claim that it's the correct fix for the issue, but the following 
patch helps the small test case above, and at least confirms that the 
paragraph separator has something to do with it:

--- a/tex-mode.el	2019-01-07 09:26:07.000000000 -0500
+++ b/tex-mode.el	2019-11-09 08:42:56.649424361 -0500
@@ -1155,7 +1155,7 @@
  		"\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t)
  		"\\>\\)"))
    (setq paragraph-separate
-	(concat "[\f%]\\|[ \t]*\\($\\|"
+	(concat "\\([ \t]*%\\)\\|[\f]\\|[ \t]*\\($\\|"
  		"\\\\[][]\\|"