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: Mon, 27 May 2019 08:02:18 -0400

Vincent Lefevre <[email-masked]> writes:

> On 2019-05-26 18:17:55 -0400, Noam Postavsky wrote:
>> And about the highlighting of quoted text outside tags, we can just
>> disable fontification, while leaving the syntax code untouched:
> [...]
>
> I've applied it with a minor change against Emacs 26 (context lines
> for hunk #1 of sgml-mode.el are different), but the comments are
> no longer highlighted as comments.

Ah, I guess reusing the default font-lock-syntactic-face-function
doesn't really make sense after all.  So sgml-font-lock-syntactic-face
should be like this:

    (defun sgml-font-lock-syntactic-face (state)
      "`font-lock-syntactic-face-function' for `sgml-mode'."
      ;; Don't use string face outside of tags.
      (cond ((and (nth 9 state) (nth 3 state)) font-lock-string-face)
            ((nth 4 state) font-lock-comment-face)))

>From 0c3e6a97f92dec31e7e186dae933c86700034089 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sun, 26 May 2019 17:41:22 -0400
Subject: [PATCH] Don't fontify text outside of SGML/XML tags (Bug#33887)

* lisp/font-lock.el (font-lock-syntactic-face-function-default): New
function.
(font-lock-syntactic-face-function): Use it as default value.
* lisp/textmodes/sgml-mode.el (sgml-font-lock-syntactic-face): New
function.
(sgml-mode):
* lisp/nxml/nxml-mode.el (nxml-mode): Use it as
font-lock-syntactic-face-function value.
---
 lisp/font-lock.el           |  7 +++++--
 lisp/nxml/nxml-mode.el      |  4 +++-
 lisp/textmodes/sgml-mode.el | 12 ++++++++++--
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/lisp/font-lock.el b/lisp/font-lock.el
index 3991a4ee8e..ddf1cbdb9f 100644
--- a/lisp/font-lock.el
+++ b/lisp/font-lock.el
@@ -527,9 +527,12 @@ (defvar font-lock-syntactically-fontified 0
 sometimes be slightly incorrect.")
 (make-variable-buffer-local 'font-lock-syntactically-fontified)
 
+(defun font-lock-syntactic-face-function-default (state)
+  "Default value for `font-lock-syntactic-face-function'."
+  (if (nth 3 state) font-lock-string-face font-lock-comment-face))
+
 (defvar font-lock-syntactic-face-function
-  (lambda (state)
-    (if (nth 3 state) font-lock-string-face font-lock-comment-face))
+  #'font-lock-syntactic-face-function-default
   "Function to determine which face to use when fontifying syntactically.
 The function is called with a single parameter (the state as returned by
 `parse-partial-sexp' at the beginning of the region to highlight) and
diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el
index da01b2a342..05044d66df 100644
--- a/lisp/nxml/nxml-mode.el
+++ b/lisp/nxml/nxml-mode.el
@@ -551,7 +551,9 @@ (define-derived-mode nxml-mode text-mode "nXML"
           nil  ; no special syntax table
           (font-lock-extend-region-functions . (nxml-extend-region))
           (jit-lock-contextually . t)
-          (font-lock-unfontify-region-function . nxml-unfontify-region)))
+          (font-lock-unfontify-region-function . nxml-unfontify-region)
+          (font-lock-syntactic-face-function
+           . sgml-font-lock-syntactic-face)))
 
   (with-demoted-errors (rng-nxml-mode-init)))
 
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
index 1df7e78afc..da25665e62 100644
--- a/lisp/textmodes/sgml-mode.el
+++ b/lisp/textmodes/sgml-mode.el
@@ -329,6 +329,12 @@ (defconst sgml-font-lock-keywords-2
 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
   "Rules for highlighting SGML code.  See also `sgml-tag-face-alist'.")
 
+(defun sgml-font-lock-syntactic-face (state)
+  "`font-lock-syntactic-face-function' for `sgml-mode'."
+  ;; Don't use string face outside of tags.
+  (cond ((and (nth 9 state) (nth 3 state)) font-lock-string-face)
+        ((nth 4 state) font-lock-comment-face)))
+
 (defvar-local sgml--syntax-propertize-ppss nil)
 
 (defun sgml--syntax-propertize-ppss (pos)
@@ -573,7 +579,7 @@ (define-derived-mode sgml-mode text-mode '(sgml-xml-mode "XML" "SGML")
   ;; This is desirable because SGML discards a newline that appears
   ;; immediately after a start tag or immediately before an end tag.
   (setq-local paragraph-start (concat "[ \t]*$\\|\
-[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
+\[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
   (setq-local paragraph-separate (concat paragraph-start "$"))
   (setq-local adaptive-fill-regexp "[ \t]*")
   (add-hook 'fill-nobreak-predicate 'sgml-fill-nobreak nil t)
@@ -591,7 +597,9 @@ (define-derived-mode sgml-mode text-mode '(sgml-xml-mode "XML" "SGML")
   (setq font-lock-defaults '((sgml-font-lock-keywords
 			      sgml-font-lock-keywords-1
 			      sgml-font-lock-keywords-2)
-			     nil t))
+                             nil t nil
+                             (font-lock-syntactic-face-function
+                              . sgml-font-lock-syntactic-face)))
   (setq-local syntax-propertize-function #'sgml-syntax-propertize)
   (setq-local facemenu-add-face-function 'sgml-mode-facemenu-add-face-function)
   (setq-local sgml-xml-mode (sgml-xml-guess))
-- 
2.11.0