Subject: bug#20773: bug#27088: 11.90; preview-latex fails for file names of certain length

From: Ikumi Keita <[email-masked]>
Date: Fri, 22 Sep 2017 15:07:15 +0900

Hi all,

At last I managed to figure out the roots of this wierd symptom.  And it
also turned out that bug#20773 has the same origin, so I'm cc'ing to
[email-masked], too.

>>>>> Ikumi Keita <[email-masked]> writes:

> Preview-latex fails if the length of the file name or the amount of the
> document is sufficiently large.

> [How to reproduce]
> (0) Enable preview-latex.
> (1) Save the attached file "preview-section-test.tex" in /tmp/ and open it.
> (2) Type C-c C-p C-d and answer with "n" to "Cache preamble?" question.
> (3) The ghostscript process hangs with error message "No bounding box"
> and the process buffer is displayed.  Only the partial "\section{xxx}"
> are covered by the expected images and rests remains icons of road
> construction sign.  See the attached screenshot.

> It is really strage to me that the length of the file name seems to be
> related to this symptom.  If the *same* file is saved under the file
> name "xxxxxxxxxxxxxxxxxx.tex" (18 x's), no errors occur and all
> "\section{xxx}" are covered by suitable images after C-c C-p C-d.  But
> the symptom occurs again if the file name is "xxxxxxxxxxxxxxxxxxx.tex"
> (19 x's).

[Analysis]
When LaTeX runs it always outputs a lot of messages to console and log
file, and it inserts line breaks in the 80th column when it outputs long
messages.  Here are some samples:
----------------------------------------------------------------------
./preview-section-test.tex:10: Preview: Snippet 4 ended.(655359+183500x22609920
).
----------------------------------------------------------------------
./xxxxxxxxxxxxxxxxxxxABC.tex:8: Preview: Snippet 3 ended.(655359+183500x2260992
0).
----------------------------------------------------------------------
./formalism_atranslation.tex:1: Preview: Snippet 1 ended.(541848+14544x15728640
).
----------------------------------------------------------------------

It is this line break that makes preview-latex to fail.  The part which
parses the LaTeX messages in `preview-parse-messages' of preview.el is:
----------------------------------------------------------------------
		(if (looking-at "\
\\(?:Preview\\|Package Preview Error\\): Snippet \\([---0-9]+\\) \\(started\\|ended\\(\
\\.? *(\\([---0-9]+\\)\\+\\([---0-9]+\\)x\\([---0-9]+\\))\\)?\\)\\.")
		    (progn
... (snip) ...
		      (setq snippet (string-to-number (match-string 1))
			    box (unless
				    (string= (match-string 2) "started")
				  (if (match-string 4)
				      (mapcar #'(lambda (x)
						  (* (preview-get-magnification)
						     (string-to-number x)))
					      (list
					       (match-string 4)
					       (match-string 5)
					       (match-string 6)))
				    t))
----------------------------------------------------------------------
Comparing the regexp and the examples quoted above, you can tell that
the regexp fails to pick up the numbers embedded in the LaTeX messages
such as "655359+183500x22609920" due to the extra line break.  This the
reason that preview-latex cannot provide the necessary information to
ghostscript and complains that "No bounding box".

[Solution]
Thanks to
https://tex.stackexchange.com/questions/52988/avoid-linebreaks-in-latex-console-log-output
, I learned that we can (effectively) inhibit to insert extra line
breaks by setting environment variable max_print_line to sufficiently
large value.

I confirmed that the attached patch solves the both bug#20773 and
bug#27088.  I will install this fix in a week or so.  According to the
article above, MikTeX also understands max_print_line, so I think this
fix is effective regardless of TeX distribution.  As always, any
comments and suggestions are welcome.

And I also expect that the same prescription can be applied to solve
reliably similar problem handled in `TeX-format-filter' at the part
"Remove line breaks at columns 79 and 80" comment.  We can get rid of
heuristic logic used there.

Regards,
Ikumi Keita

>From [email-masked] Fri Sep 22 14:52:07 2017
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: [PATCH] Make preview-latex parsing robust to fix Bug#20773, Bug#27088
X-Mercurial-Node: 47711b2553dcbf4858a1b4f53d63e82a3003bca8
X-Mercurial-Series-Index: 1
X-Mercurial-Series-Total: 1
Message-Id: <[email-masked]>
X-Mercurial-Series-Id: <[email-masked]>
User-Agent: Mercurial-patchbomb/4.3.1
Date: Fri, 22 Sep 2017 14:52:07 +0900
From: Ikumi Keita <[email-masked]>
To: dummy

* preview.el.in (TeX-inline-preview-internal): Set environment
variable max_print_line to sufficiently large value of 1000 so that
LaTeX process does not insert newline in lines necessary to identify
Bounding Boxes.

diff --git a/preview.el.in b/preview.el.in
--- a/preview.el.in
+++ b/preview.el.in
@@ -3537,19 +3537,25 @@
       ((preview-format-name (shell-quote-argument
 			     (preview-dump-file-name
 			      (file-name-nondirectory master))))
+       (process-environment process-environment)
        (process
-	(TeX-run-command
-	 "Preview-LaTeX"
-	 (if (consp (cdr dumped-cons))
-	     (preview-do-replacements
-	      command
-	      (append preview-undump-replacements
-		      ;; Since the command options provided in
-		      ;; (TeX-engine-alist) are dropped, give them
-		      ;; back.
-		      (list (list "\\`\\([^ ]+\\)"
-			    (TeX-command-expand "%(PDF)%(latex)" nil)))))
-	   command) file)))
+	(progn
+	  ;; Fix Bug#20773, Bug#27088.
+	  ;; Make LaTeX not to insert newline in lines necessary to
+	  ;; identify Bounding Boxes.
+	  (setenv "max_print_line" "1000")
+	  (TeX-run-command
+	   "Preview-LaTeX"
+	   (if (consp (cdr dumped-cons))
+	       (preview-do-replacements
+		command
+		(append preview-undump-replacements
+			;; Since the command options provided in
+			;; (TeX-engine-alist) are dropped, give them
+			;; back.
+			(list (list "\\`\\([^ ]+\\)"
+				    (TeX-command-expand "%(PDF)%(latex)" nil)))))
+	     command) file))))
     (condition-case err
 	(progn
 	  (when str