nxml-util.el 3.63 KB
Newer Older
1
;;; nxml-util.el --- utility functions for nxml-*.el  -*- lexical-binding:t -*-
Mark A. Hershberger's avatar
Mark A. Hershberger committed
2

3
;; Copyright (C) 2003, 2007-2014 Free Software Foundation, Inc.
Mark A. Hershberger's avatar
Mark A. Hershberger committed
4 5

;; Author: James Clark
6
;; Keywords: wp, hypermedia, languages, XML
Mark A. Hershberger's avatar
Mark A. Hershberger committed
7

Glenn Morris's avatar
Glenn Morris committed
8
;; This file is part of GNU Emacs.
Mark A. Hershberger's avatar
Mark A. Hershberger committed
9

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Glenn Morris's avatar
Glenn Morris committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Mark A. Hershberger's avatar
Mark A. Hershberger committed
14

Glenn Morris's avatar
Glenn Morris committed
15 16 17 18 19 20
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Mark A. Hershberger's avatar
Mark A. Hershberger committed
22 23 24 25 26

;;; Commentary:

;;; Code:

Michael Olson's avatar
Michael Olson committed
27
(defconst nxml-debug nil
28
  "Enable nxml debugging.  Effective only at compile time.")
Michael Olson's avatar
Michael Olson committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

(defsubst nxml-debug (format &rest args)
  (when nxml-debug
    (apply #'message format args)))

(defmacro nxml-debug-change (name start end)
  (when nxml-debug
    `(nxml-debug "%s: %S" ,name
                (buffer-substring-no-properties ,start ,end))))

(defmacro nxml-debug-set-inside (start end)
  (when nxml-debug
    `(let ((overlay (make-overlay ,start ,end)))
       (overlay-put overlay 'face '(:background "red"))
       (overlay-put overlay 'nxml-inside-debug t)
       (nxml-debug-change "nxml-set-inside" ,start ,end))))

(defmacro nxml-debug-clear-inside (start end)
  (when nxml-debug
48
    `(cl-loop for overlay in (overlays-in ,start ,end)
Michael Olson's avatar
Michael Olson committed
49 50 51 52
           if (overlay-get overlay 'nxml-inside-debug)
           do (delete-overlay overlay)
           finally (nxml-debug-change "nxml-clear-inside" ,start ,end))))

Mark A. Hershberger's avatar
Mark A. Hershberger committed
53 54
(defun nxml-make-namespace (str)
  "Return a symbol for the namespace URI STR.
55
STR must be a string.  If STR is the empty string, return nil.
Mark A. Hershberger's avatar
Mark A. Hershberger committed
56 57 58 59 60 61 62 63 64 65
Otherwise, return the symbol whose name is STR prefixed with a colon."
  (if (string-equal str "")
      nil
    (intern (concat ":" str))))

(defun nxml-namespace-name (ns)
  "Return the namespace URI corresponding to the symbol NS.
This is the inverse of `nxml-make-namespace'."
  (and ns (substring (symbol-name ns) 1)))

Michael Olson's avatar
Michael Olson committed
66
(defconst nxml-xml-namespace-uri
Mark A. Hershberger's avatar
Mark A. Hershberger committed
67 68 69 70 71
  (nxml-make-namespace "http://www.w3.org/XML/1998/namespace"))

(defconst nxml-xmlns-namespace-uri
  (nxml-make-namespace "http://www.w3.org/2000/xmlns/"))

Michael Olson's avatar
Michael Olson committed
72
(defmacro nxml-with-degradation-on-error (context &rest body)
73
  (declare (indent 1) (debug t))
Michael Olson's avatar
Michael Olson committed
74 75 76 77 78 79 80 81
  (if (not nxml-debug)
      (let ((error-symbol (make-symbol "err")))
        `(condition-case ,error-symbol
             (progn ,@body)
           (error
            (nxml-degrade ,context ,error-symbol))))
    `(progn ,@body)))

Mark A. Hershberger's avatar
Mark A. Hershberger committed
82 83
(defmacro nxml-with-invisible-motion (&rest body)
  "Evaluate body without calling any point motion hooks."
84
  (declare (indent 0) (debug t))
Mark A. Hershberger's avatar
Mark A. Hershberger committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  `(let ((inhibit-point-motion-hooks t))
     ,@body))

(defun nxml-display-file-parse-error (err)
  (let* ((filename (nth 1 err))
	 (buffer (find-file-noselect filename))
	 (pos (nth 2 err))
	 (message (nth 3 err)))
    (pop-to-buffer buffer)
    ;; What's the right thing to do if the buffer's modified?
    ;; The position in the saved file could be completely different.
    (goto-char (if (buffer-modified-p) 1 pos))
    (error "%s" message)))

(defun nxml-signal-file-parse-error (file pos message &optional error-symbol)
  (signal (or error-symbol 'nxml-file-parse-error)
	  (list file pos message)))

103 104
(define-error 'nxml-error nil)
(define-error 'nxml-file-parse-error "Error parsing file" 'nxml-error)
Mark A. Hershberger's avatar
Mark A. Hershberger committed
105 106 107 108

(provide 'nxml-util)

;;; nxml-util.el ends here