Subject: bug#18871: 24.4; nxml-mode: DTD edition breaks the file structure

From: Noam Postavsky <[email-masked]>
Date: Sat, 18 May 2019 14:58:53 -0400

tags 18871 + patch
merge 18871 23668
quit

Vincent Lefevre <[email-masked]> writes:

> Consider the following XML file:
>
> ------------------------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE root [
> <!ELEMENT root (item)*>
> <!ELEMENT item (#PCDATA)>
> ]>
>
> <root>
> <!-- 1234567890 1234567890 1234567890 1234567890 1234567890 -->
> </root>
> ------------------------------------------------------------------
>
> 1. Open it with "emacs -Q".
> 2. Go to line 2 and type C-SPC.
> 3. Go to line 6 and type C-w to delete the DTD.
>
> The highlighting gets completely wrong. And if I delete the closing
> tag </root> (with C-k) and type C-c C-f, I get the error:
>
>   No matching start-tag
>
> In more complex files, slightly editing the DTD gives the same behavior.

Should be fixed by this patch.

>From 5f6abb03b3b0d10dd39ece8c151a8a7857b85895 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sat, 18 May 2019 14:54:36 -0400
Subject: [PATCH] Keep nxml-prolog-end up to date (Bug#18871)

The change on 2016-01-16 "lisp/nxml: Use syntax-tables for comments"
removed the update of nxml-prolog-end, although it already failed to
work correctly after 2013-10-07 "* lisp/nxml/nxml-mode.el: Use
lexical-binding and syntax-propertize."
* lisp/nxml/nxml-rap.el: New function.
* lisp/nxml/nxml-mode.el (nxml-mode): Add it to
after-change-functions.
---
 lisp/nxml/nxml-mode.el | 1 +
 lisp/nxml/nxml-rap.el  | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/lisp/nxml/nxml-mode.el b/lisp/nxml/nxml-mode.el
index 7c39c5023c..b816bba819 100644
--- a/lisp/nxml/nxml-mode.el
+++ b/lisp/nxml/nxml-mode.el
@@ -534,6 +534,7 @@ nxml-mode
   (setq-local syntax-ppss-table sgml-tag-syntax-table)
   (setq-local syntax-propertize-function #'nxml-syntax-propertize)
   (add-hook 'change-major-mode-hook #'nxml-cleanup nil t)
+  (add-hook 'after-change-functions #'nxml-maybe-rescan-prolog nil t)
 
   ;; Emacs 23 handles the encoding attribute on the xml declaration
   ;; transparently to nxml-mode, so there is no longer a need for the below
diff --git a/lisp/nxml/nxml-rap.el b/lisp/nxml/nxml-rap.el
index 21dbaded25..2be3981047 100644
--- a/lisp/nxml/nxml-rap.el
+++ b/lisp/nxml/nxml-rap.el
@@ -108,6 +108,12 @@ nxml-scan-prolog
     (setq nxml-prolog-regions (xmltok-forward-prolog))
     (setq nxml-prolog-end (point))))
 
+(defun nxml-maybe-rescan-prolog (start _end _length)
+  "Reparse the prolog if START lies within it.
+`nxml-mode' adds this function on `after-change-functions'."
+  (when (<= beg nxml-prolog-end)
+    (save-excursion
+      (nxml-scan-prolog))))
 
 ;;; Random access parsing
 
-- 
2.11.0