Subject: bug#33887: 26.1; Emacs hangs for several seconds when going to the end of an XML file in nXML mode

From: Noam Postavsky <[email-masked]>
Date: Thu, 16 May 2019 08:15:58 -0400

Noam Postavsky <[email-masked]> writes:

> [1: e7e92dc5d2]: 2019-05-15 19:04:14 -0400
>   Fix merge of sgml-syntax-propertize-rules
>   https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=e7e92dc5d24ac3bcde69732bab6a6c3c0d9de97b

Uh, I goofed that one, Stefan fixed it [2: 9a74e5666b].  The corrected patch would be as follows:

>From 2221c244ee01c4c336ec860cf52a1ef37111ff19 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Wed, 15 May 2019 18:51:30 -0400
Subject: [PATCH] Backport sgml-syntax-propertize-rules speedup (Bug#33887)

* lisp/textmodes/sgml-mode.el (sgml-syntax-propertize-rules): Reapply
2019-01-17 "* lisp/textmodes/sgml-mode.el: Try and fix bug#33887."
taking into account 2019-05-09 "Recognize single quote attribute
values in nxml and sgml (Bug#35381)" which means we have to handle
single quotes as well.
* test/lisp/textmodes/sgml-mode-tests.el (sgml-quote-works): New test.
---
 lisp/textmodes/sgml-mode.el            | 21 +++++++++++++++------
 test/lisp/textmodes/sgml-mode-tests.el |  7 +++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 128e58810e..1c307d12b0 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -347,12 +347,21 @@ sgml-font-lock-keywords
      ("--[ \t\n]*\\(>\\)" (1 "> b"))
      ("\\(<\\)[?!]" (1 (prog1 "|>"
                          (sgml-syntax-propertize-inside end))))
-     ;; Quotes outside of tags should not introduce strings.
-     ;; Be careful to call `syntax-ppss' on a position before the one we're
-     ;; going to change, so as not to need to flush the data we just computed.
-     ("[\"']" (0 (if (prog1 (zerop (car (syntax-ppss (match-beginning 0))))
-                       (goto-char (match-end 0)))
-                     (string-to-syntax ".")))))))
+     ;; Quotes outside of tags should not introduce strings which end up
+     ;; hiding tags.  We used to test every quote and mark it as "."
+     ;; if it's outside of tags, but there are too many quotes and
+     ;; the resulting number of calls to syntax-ppss made it too slow
+     ;; (bug#33887), so we're now careful to leave alone any pair
+     ;; of quotes that doesn't hold a < or > char, which is the vast majority.
+     ("\\(?:\\(?1:\"\\)[^\"<>]*[<>\"]\\|\\(?1:'\\)[^'<>]*[<>']\\)"
+      (1 (unless (memq (char-before) '(?\' ?\"))
+           ;; Be careful to call `syntax-ppss' on a position before the one
+           ;; we're going to change, so as not to need to flush the data we
+           ;; just computed.
+           (if (prog1 (zerop (car (syntax-ppss (match-beginning 0))))
+                 (goto-char (1- (match-end 0))))
+               (string-to-syntax ".")))))
+     )))
 
 (defun sgml-syntax-propertize (start end)
   "Syntactic keywords for `sgml-mode'."
diff --git a/test/lisp/textmodes/sgml-mode-tests.el b/test/lisp/textmodes/sgml-mode-tests.el
index 7318a667b3..1c501abf38 100644
--- a/test/lisp/textmodes/sgml-mode-tests.el
+++ b/test/lisp/textmodes/sgml-mode-tests.el
@@ -130,5 +130,12 @@ sgml-with-content
    (sgml-delete-tag 1)
    (should (string= "Winter is comin'" (buffer-string)))))
 
+(ert-deftest sgml-tests--quotes-syntax ()
+  (with-temp-buffer
+    (sgml-mode)
+    (insert "a\"b <tag>c'd</tag>")
+    (should (= 1 (car (syntax-ppss (1- (point-max))))))
+    (should (= 0 (car (syntax-ppss (point-max)))))))
+
 (provide 'sgml-mode-tests)
 ;;; sgml-mode-tests.el ends here
-- 
2.11.0

[2: 9a74e5666b]: 2019-05-15 22:21:36 -0400
  * lisp/textmodes/sgml-mode.el (sgml-syntax-propertize-rules): Fix typo
  https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=9a74e5666b022098c63d0047c0df90c66e1aa64a