Initial merge of nxml
[bpt/emacs.git] / lisp / nxml / nxml-util.el
1 ;;; nxml-util.el --- utility functions for nxml-*.el
2
3 ;; Copyright (C) 2003 Free Software Foundation, Inc.
4
5 ;; Author: James Clark
6 ;; Keywords: XML
7
8 ;; This program is free software; you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License as
10 ;; published by the Free Software Foundation; either version 2 of
11 ;; the License, or (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be
14 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
15 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 ;; PURPOSE. See the GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public
19 ;; License along with this program; if not, write to the Free
20 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 ;; MA 02111-1307 USA
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (defun nxml-make-namespace (str)
28 "Return a symbol for the namespace URI STR.
29 STR must be a string. If STR is the empty string, return nil.
30 Otherwise, return the symbol whose name is STR prefixed with a colon."
31 (if (string-equal str "")
32 nil
33 (intern (concat ":" str))))
34
35 (defun nxml-namespace-name (ns)
36 "Return the namespace URI corresponding to the symbol NS.
37 This is the inverse of `nxml-make-namespace'."
38 (and ns (substring (symbol-name ns) 1)))
39
40 (defconst nxml-xml-namespace-uri
41 (nxml-make-namespace "http://www.w3.org/XML/1998/namespace"))
42
43 (defconst nxml-xmlns-namespace-uri
44 (nxml-make-namespace "http://www.w3.org/2000/xmlns/"))
45
46 (defmacro nxml-with-unmodifying-text-property-changes (&rest body)
47 "Evaluate BODY without any text property changes modifying the buffer.
48 Any text properties changes happen as usual but the changes are not treated as
49 modifications to the buffer."
50 (let ((modified (make-symbol "modified")))
51 `(let ((,modified (buffer-modified-p))
52 (inhibit-read-only t)
53 (inhibit-modification-hooks t)
54 (buffer-undo-list t)
55 (deactivate-mark nil)
56 ;; Apparently these avoid file locking problems.
57 (buffer-file-name nil)
58 (buffer-file-truename nil))
59 (unwind-protect
60 (progn ,@body)
61 (unless ,modified
62 (restore-buffer-modified-p nil))))))
63
64 (put 'nxml-with-unmodifying-text-property-changes 'lisp-indent-function 0)
65 (def-edebug-spec nxml-with-unmodifying-text-property-changes t)
66
67 (defmacro nxml-with-invisible-motion (&rest body)
68 "Evaluate body without calling any point motion hooks."
69 `(let ((inhibit-point-motion-hooks t))
70 ,@body))
71
72 (put 'nxml-with-invisible-motion 'lisp-indent-function 0)
73 (def-edebug-spec nxml-with-invisible-motion t)
74
75 (defun nxml-display-file-parse-error (err)
76 (let* ((filename (nth 1 err))
77 (buffer (find-file-noselect filename))
78 (pos (nth 2 err))
79 (message (nth 3 err)))
80 (pop-to-buffer buffer)
81 ;; What's the right thing to do if the buffer's modified?
82 ;; The position in the saved file could be completely different.
83 (goto-char (if (buffer-modified-p) 1 pos))
84 (error "%s" message)))
85
86 (defun nxml-signal-file-parse-error (file pos message &optional error-symbol)
87 (signal (or error-symbol 'nxml-file-parse-error)
88 (list file pos message)))
89
90 (put 'nxml-file-parse-error
91 'error-conditions
92 '(error nxml-file-parse-error))
93
94 (put 'nxml-parse-file-error
95 'error-message
96 "Error parsing file")
97
98 (provide 'nxml-util)
99
100 ;;; nxml-util.el ends here