Subject: bug#42267: Fontification takes a long time when an equation contains a double prime

From: Ikumi Keita <[email-masked]>
Date: Fri, 10 Jul 2020 16:53:39 +0900

Hi Najib and all,

>>>>> Najib Idrissi-Kaïtouni <[email-masked]> writes:
> Yes, this is much, much better with this patch. I don't experience any
> noticeable slowdown anymore, whether in the test file or in my "real
> world" file. Thanks!

Thanks for confirmation. We'll fix the problem soon.

To AUCTeX developers:
I propose to install the attached fix. Any comments and suggestions are
welcome.

[Analysis]
The backgrounds of the problem are the following two:
1. When there is a closing quote (or double prime) '' without matching
   opening quote `` before it, current
   `font-latex-extend-region-backwards-quotation' ends up to decrease
   `font-lock-beg' by 5000 (=default value of
   `font-latex-multiline-boundary') and returns t.
2. Previously, font-latex-extend-region-backwards-* functions were
   stored in `font-latex-extend-region-functions' and called just once
   per fontify operation. However, now they are in
   `font-lock-extend-region-functions' and can be called repeatedly in
   `font-lock-default-fontify-region', until all of them return nil.

Therefore, when there is math expression like $f''(x)=x^{3}$, current
f-l-e-r-b-q repeatedly decreases `font-lock-beg' so that the font lock
region begins with BOB eventually. Thus every key stroke forces to
re-fontify all the region from top of the buffer. This is the reason of
the apparent slowdown in a large buffer.

I think the behavior of the above item 1 is a bug. When f-l-e-r-b-q
cannot find a matching opening quote, it should just give up to extend
the region anymore.

Regards,
Ikumi Keita

>From f4cda23f94b803b499c96b58924ef57f24170834 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Thu, 9 Jul 2020 23:26:53 +0900
Subject: [PATCH] Don't extend font lock region too much (bug#42267)

* font-latex.el (font-latex-extend-region-backwards-quotation): If
there is no accompanying open quote, don't extend the font lock
region.
---
 font-latex.el | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/font-latex.el b/font-latex.el
index d7d26fab..ac7573be 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -2066,16 +2066,25 @@ set to french, and >>german<< (and 8-bit) are used if set to german."
 		    (setq opening-quote (car elt))
 		    (throw 'found nil))))
 	      ;; Find opening quote taking nested quotes into account.
-	      (while (progn
-		       (re-search-backward (concat opening-quote "\\|"
-						   closing-quote)
-					   point-of-surrender 'move)
-		       (when (and (> (point) point-of-surrender)
-				  (not (bobp)))
+	      (while (if (re-search-backward (concat opening-quote "\\|"
+						     closing-quote)
+					     point-of-surrender t)
+			 ;; Found quotes before point-of-surrender.
 			 (if (string= (match-string 0) closing-quote)
+			     ;; Increase nest-count and continue the
+			     ;; inner loop.
 			     (setq nest-count (1+ nest-count))
-			   (when (/= nest-count 0)
-			     (setq nest-count (1- nest-count)))))))
+			   ;; If in nest, decrease nest-count and
+			   ;; continue. Else terminate the inner loop.
+			   (if (/= nest-count 0)
+			       (setq nest-count (1- nest-count))))
+		       ;; Didn't find until point-of-surrender.
+		       ;; Terminate the outer loop and don't extend
+		       ;; the region. (bug#42267)
+		       (throw 'extend nil)))
+	      ;; If the opening quote begins before `font-lock-beg',
+	      ;; terminate the outer loop and extend the region. Else
+	      ;; continue.
 	      (when (< (point) font-lock-beg)
                 (setq font-lock-beg (point))
 		(throw 'extend t)))))))))
-- 
2.27.0