]> code.delx.au - gnu-emacs/blobdiff - lisp/nxml/nxml-mode.el
Update copyright year to 2015
[gnu-emacs] / lisp / nxml / nxml-mode.el
index c45196f0316c4bd5334f8088dbb1cd89260c086e..6c5c85b2fcc879d1ece8b96a72502db962926ef9 100644 (file)
@@ -1,9 +1,9 @@
-;;; nxml-mode.el --- a new XML mode
+;;; nxml-mode.el --- a new XML mode  -*- lexical-binding:t -*-
 
-;; Copyright (C) 2003-2004, 2007-2013 Free Software Foundation, Inc.
+;; Copyright (C) 2003-2004, 2007-2015 Free Software Foundation, Inc.
 
 ;; Author: James Clark
-;; Keywords: XML
+;; Keywords: wp, hypermedia, languages, XML
 
 ;; This file is part of GNU Emacs.
 
@@ -46,6 +46,7 @@
 
 (defgroup nxml nil
   "New XML editing mode."
+  :link '(custom-manual "(nxml-mode) Top")
   :group 'languages)
 
 (defgroup nxml-faces nil
@@ -448,6 +449,9 @@ reference.")
     (when rng-validate-mode
       (rng-validate-while-idle (current-buffer)))))
 
+(defvar tildify-space-string)
+(defvar tildify-foreach-region-function)
+
 ;;;###autoload
 (define-derived-mode nxml-mode text-mode "nXML"
   ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
@@ -504,6 +508,19 @@ be treated as a single markup item, set the variable
 Many aspects this mode can be customized using
 \\[customize-group] nxml RET."
   ;; (kill-all-local-variables)
+  ;; If encoding does not allow non-break space character, use reference.
+  ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
+  ;; FIXME: Perhaps use   if possible (e.g. XHTML)?
+  (setq-local tildify-space-string
+              (if (equal (decode-coding-string
+                          (encode-coding-string " " buffer-file-coding-system)
+                          buffer-file-coding-system) " ")
+                  " " " "))
+  ;; FIXME: Use the fact that we're parsing the document already
+  ;; rather than using regex-based filtering.
+  (setq-local tildify-foreach-region-function
+              (apply-partially 'tildify-foreach-ignore-environments
+                               '(("<! *--" . "-- *>") ("<" . ">"))))
   (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
   ;; We'll determine the fill prefix ourselves
   (make-local-variable 'adaptive-fill-mode)
@@ -540,14 +557,14 @@ Many aspects this mode can be customized using
          (nxml-scan-prolog)))))
   (add-hook 'completion-at-point-functions
             #'nxml-completion-at-point-function nil t)
-  (add-hook 'after-change-functions 'nxml-after-change nil t)
+  (setq-local syntax-propertize-function #'nxml-after-change)
   (add-hook 'change-major-mode-hook 'nxml-cleanup 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
   ;; hook. The hook also had the drawback of overriding explicit user
   ;; instruction to save as some encoding other than utf-8.
-;;;   (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
+  ;;(add-hook 'write-contents-hooks 'nxml-prepare-to-save)
   (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
     (when (and nxml-default-buffer-file-coding-system
               (not (local-variable-p 'buffer-file-coding-system)))
@@ -561,8 +578,6 @@ Many aspects this mode can be customized using
           nil  ; font-lock-keywords-case-fold-search. XML is case sensitive
           nil  ; no special syntax table
           nil  ; no automatic syntactic fontification
-          (font-lock-extend-after-change-region-function
-           . nxml-extend-after-change-region)
           (font-lock-extend-region-functions . (nxml-extend-region))
           (jit-lock-contextually . t)
           (font-lock-unfontify-region-function . nxml-unfontify-region)))
@@ -597,6 +612,7 @@ Many aspects this mode can be customized using
 
 ;;; Change management
 
+(defvar font-lock-beg) (defvar font-lock-end)
 (defun nxml-debug-region (start end)
   (interactive "r")
   (let ((font-lock-beg start)
@@ -605,22 +621,16 @@ Many aspects this mode can be customized using
     (goto-char font-lock-beg)
     (set-mark font-lock-end)))
 
-(defun nxml-after-change (start end pre-change-length)
-  ; In font-lock mode, nxml-after-change1 is called via
-  ; nxml-extend-after-change-region instead so that the updated
-  ; book-keeping information is available for fontification.
-  (unless (or font-lock-mode nxml-degraded)
+(defun nxml-after-change (start end)
+  ;; Called via syntax-propertize-function.
+  (unless nxml-degraded
     (nxml-with-degradation-on-error 'nxml-after-change
-        (save-excursion
-          (save-restriction
-            (widen)
-            (save-match-data
-              (nxml-with-invisible-motion
-                (with-silent-modifications
-                  (nxml-after-change1
-                   start end pre-change-length)))))))))
-
-(defun nxml-after-change1 (start end pre-change-length)
+      (save-restriction
+        (widen)
+        (nxml-with-invisible-motion
+         (nxml-after-change1 start end))))))
+
+(defun nxml-after-change1 (start end)
   "After-change bookkeeping.
 Returns a cons cell containing a possibly-enlarged change region.
 You must call `nxml-extend-region' on this expanded region to obtain
@@ -628,23 +638,14 @@ the full extent of the area needing refontification.
 
 For bookkeeping, call this function even when fontification is
 disabled."
-  (let ((pre-change-end (+ start pre-change-length)))
-    ;; If the prolog might have changed, rescan the prolog
-    (when (<= start
-             ;; Add 2 so as to include the < and following char that
-             ;; start the instance (document element), since changing
-             ;; these can change where the prolog ends.
-             (+ nxml-prolog-end 2))
-      ;; end must be extended to at least the end of the old prolog in
-      ;; case the new prolog is shorter
-      (when (< pre-change-end nxml-prolog-end)
-       (setq end
-             ;; don't let end get out of range even if pre-change-length
-             ;; is bogus
-             (min (point-max)
-                  (+ end (- nxml-prolog-end pre-change-end)))))
-      (nxml-scan-prolog)
-      (setq start (point-min))))
+  ;; If the prolog might have changed, rescan the prolog.
+  (when (<= start
+            ;; Add 2 so as to include the < and following char that
+            ;; start the instance (document element), since changing
+            ;; these can change where the prolog ends.
+            (+ nxml-prolog-end 2))
+    (nxml-scan-prolog)
+    (setq start (point-min)))
 
   (when (> end nxml-prolog-end)
     (goto-char start)
@@ -653,8 +654,7 @@ disabled."
     (setq end (max (nxml-scan-after-change start end)
                    end)))
 
-  (nxml-debug-change "nxml-after-change1" start end)
-  (cons start end))
+  (nxml-debug-change "nxml-after-change1" start end))
 
 ;;; Encodings
 
@@ -845,7 +845,6 @@ The XML declaration will declare an encoding depending on the buffer's
   (font-lock-default-unfontify-region start end)
   (nxml-clear-char-ref-extra-display start end))
 
-(defvar font-lock-beg) (defvar font-lock-end)
 (defun nxml-extend-region ()
   "Extend the region to hold the minimum area we can fontify with nXML.
 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
@@ -887,22 +886,9 @@ Called with `font-lock-beg' and `font-lock-end' dynamically bound."
       (nxml-debug-change "nxml-extend-region" start end)
       t)))
 
-(defun nxml-extend-after-change-region (start end pre-change-length)
-  (unless nxml-degraded
-    (nxml-with-degradation-on-error
-     'nxml-extend-after-change-region
-     (save-excursion
-       (save-restriction
-         (widen)
-         (save-match-data
-           (nxml-with-invisible-motion
-             (with-silent-modifications
-               (nxml-after-change1
-                start end pre-change-length)))))))))
-
 (defun nxml-fontify-matcher (bound)
   "Called as font-lock keyword matcher."
-
+  (syntax-propertize bound)
   (unless nxml-degraded
     (nxml-debug-change "nxml-fontify-matcher" (point) bound)
 
@@ -2597,7 +2583,7 @@ With a prefix argument, inserts the character directly."
               (> (prefix-numeric-value arg) 0))))
     (when (not (eq new nxml-char-ref-extra-display))
       (setq nxml-char-ref-extra-display new)
-      (font-lock-fontify-buffer))))
+      (font-lock-flush))))
 
 (put 'nxml-char-ref 'evaporate t)
 
@@ -2660,8 +2646,9 @@ With a prefix argument, inserts the character directly."
 (put 'entity-ref 'nxml-friendly-name "entity reference")
 (put 'char-ref 'nxml-friendly-name "character reference")
 
-;;;###autoload
-(defalias 'xml-mode 'nxml-mode)
+;; Only do this in loaddefs, so that if someone defines a different
+;; alias in .emacs, loading this file afterwards does not clobber it.
+;;;###autoload(defalias 'xml-mode 'nxml-mode)
 
 (provide 'nxml-mode)