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

From: Ikumi Keita <[email-masked]>
Date: Sat, 11 Jul 2020 14:55:18 +0900

>>>>> Ikumi Keita <[email-masked]> writes:
> 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.

After reconsideration, I realized that such giving up is not appropriate
when that unmatched close quote has, in front of it, other open-close
quote pairs. Examples:
      abc ``def ghi'' jkl $y''=x^{2}$
      abc <<def ghi>> jkl $y''=x^{2}$

So the correct way is to continue searching backwards another close
quote, I think. The revised patch is attached. I'll install it soon.

Regards,
Ikumi Keita

>From e86fdf248155cce325a4a660f6afc67fda528fa6 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 eagerly (bug#42267)

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

diff --git a/font-latex.el b/font-latex.el
index d7d26fab..09a6feb1 100644
--- a/font-latex.el
+++ b/font-latex.el
@@ -2066,19 +2066,33 @@ 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)))
-			 (if (string= (match-string 0) closing-quote)
-			     (setq nest-count (1+ nest-count))
-			   (when (/= nest-count 0)
-			     (setq nest-count (1- nest-count)))))))
-	      (when (< (point) font-lock-beg)
-                (setq font-lock-beg (point))
-		(throw 'extend t)))))))))
+	      (while (and (re-search-backward (concat opening-quote "\\|"
+						      closing-quote)
+					      point-of-surrender t)
+			  ;; Found quotes before point-of-surrender.
+			  (cond ((string= (match-string 0) closing-quote)
+				 ;; Encountered another closing quote.
+				 ;; Increase nest-count and continue
+				 ;; the inner loop.
+				 (setq nest-count (1+ nest-count)))
+				;; Found an opening quote.
+				((/= nest-count 0)
+				 ;; If in nest, decrease nest-count
+				 ;; and continue the inner loop.
+				 (setq nest-count (1- nest-count)))
+				;; Else we arrived at the opening quote
+				;; matching with the closing quote found
+				;; in the outer loop.
+				((< (point) font-lock-beg)
+				 ;; If that opening quote locates
+				 ;; before `font-lock-beg', break the
+				 ;; outer loop and extend the region.
+				 (setq font-lock-beg (point))
+				 (throw 'extend t))
+				(t
+				 ;; Else terminate the inner loop and
+				 ;; continue the outer loop.
+				 nil)))))))))))
 
 (defun font-latex-match-script (limit)
   "Match subscript and superscript patterns up to LIMIT."
-- 
2.27.0