Merge branch 'master' into comment-cache
[emacs.git] / lisp / nxml / nxml-util.el
blob9f085458d88f1de3cb101ac8a2ff86931ffc2add
1 ;;; nxml-util.el --- utility functions for nxml-*.el -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003, 2007-2017 Free Software Foundation, Inc.
5 ;; Author: James Clark
6 ;; Keywords: wp, hypermedia, languages, XML
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; 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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (defconst nxml-debug nil
28 "Enable nxml debugging. Effective only at compile time.")
30 (defsubst nxml-debug (format &rest args)
31 (when nxml-debug
32 (apply #'message format args)))
34 (defmacro nxml-debug-change (name start end)
35 (when nxml-debug
36 `(nxml-debug "%s: %S" ,name
37 (buffer-substring-no-properties ,start ,end))))
39 (defun nxml-make-namespace (str)
40 "Return a symbol for the namespace URI STR.
41 STR must be a string. If STR is the empty string, return nil.
42 Otherwise, return the symbol whose name is STR prefixed with a colon."
43 (if (string-equal str "")
44 nil
45 (intern (concat ":" str))))
47 (defun nxml-namespace-name (ns)
48 "Return the namespace URI corresponding to the symbol NS.
49 This is the inverse of `nxml-make-namespace'."
50 (and ns (substring (symbol-name ns) 1)))
52 (defconst nxml-xml-namespace-uri
53 (nxml-make-namespace "http://www.w3.org/XML/1998/namespace"))
55 (defconst nxml-xmlns-namespace-uri
56 (nxml-make-namespace "http://www.w3.org/2000/xmlns/"))
58 (defmacro nxml-with-degradation-on-error (context &rest body)
59 (declare (indent 1) (debug t))
60 (if (not nxml-debug)
61 (let ((error-symbol (make-symbol "err")))
62 `(condition-case ,error-symbol
63 (progn ,@body)
64 (error
65 (nxml-degrade ,context ,error-symbol))))
66 `(progn ,@body)))
68 (defmacro nxml-with-invisible-motion (&rest body)
69 "Evaluate body without calling any point motion hooks."
70 (declare (indent 0) (debug t))
71 `(let ((inhibit-point-motion-hooks t))
72 ,@body))
74 (defun nxml-display-file-parse-error (err)
75 (let* ((filename (nth 1 err))
76 (buffer (find-file-noselect filename))
77 (pos (nth 2 err))
78 (message (nth 3 err)))
79 (pop-to-buffer buffer)
80 ;; What's the right thing to do if the buffer's modified?
81 ;; The position in the saved file could be completely different.
82 (goto-char (if (buffer-modified-p) 1 pos))
83 (error "%s" message)))
85 (defun nxml-signal-file-parse-error (file pos message &optional error-symbol)
86 (signal (or error-symbol 'nxml-file-parse-error)
87 (list file pos message)))
89 (define-error 'nxml-error nil)
90 (define-error 'nxml-file-parse-error "Error parsing file" 'nxml-error)
92 (provide 'nxml-util)
94 ;;; nxml-util.el ends here