1 ;;; nxml-ns.el --- XML namespace processing
3 ;; Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This file uses a prefix of `nxml-ns'.
33 (defvar nxml-ns-state nil
34 "Contains the state of namespace processing. The state
35 is never modified destructively and so can be saved and restored
38 The value is a stack represented by a list. The list has length N + 1
39 where N is the number of open elements. Each member of the list
40 represents the bindings in effect for a particular element. Each
41 member is itself a list whose car is the default namespace
42 \(a symbol or nil) and whose cdr is an alist of (PREFIX . NS) pairs
43 where PREFIX is a string (never nil) and NS is the namespace URI
46 (defconst nxml-ns-initial-state
47 (list (list nil (cons "xml" nxml-xml-namespace-uri)))
48 "A list to be used as the initial value of nxml-ns-state. This
49 represents the state with no open elements and with the default
50 namespace bindings (no default namespace and only the xml prefix bound).")
52 (defsubst nxml-ns-state () nxml-ns-state)
54 (defsubst nxml-ns-set-state (state)
55 (setq nxml-ns-state state))
57 (defsubst nxml-ns-state-equal (state)
58 (equal nxml-ns-state state))
60 (defmacro nxml-ns-save (&rest body)
61 `(let ((nxml-ns-state nxml-ns-initial-state))
64 (put 'nxml-ns-save 'lisp-indent-function 0)
65 (def-edebug-spec nxml-ns-save t)
67 (defun nxml-ns-init ()
68 (setq nxml-ns-state nxml-ns-initial-state))
70 (defun nxml-ns-push-state ()
71 "Change the state by starting a new element. Namespace declarations
72 are inherited from the parent state."
73 (setq nxml-ns-state (cons (car nxml-ns-state) nxml-ns-state)))
75 (defun nxml-ns-pop-state ()
76 "Change the state by ending an element. The behaviour is undefined
77 if there is no open element."
78 (setq nxml-ns-state (cdr nxml-ns-state)))
80 (defun nxml-ns-get-prefix (prefix)
81 "Return the symbol for namespace bound to PREFIX, or nil if PREFIX
82 is unbound. PREFIX is a string, never nil."
83 (let ((binding (assoc prefix (cdar nxml-ns-state))))
84 (and binding (cdr binding))))
86 (defun nxml-ns-set-prefix (prefix ns)
87 "Change the binding of PREFIX. PREFIX is a string (never nil). NS
88 is a symbol (never nil). The change will be in effect until the end of
91 (let ((bindings (car nxml-ns-state)))
92 (cons (cons (car bindings)
93 (cons (cons prefix ns) (cdr bindings)))
94 (cdr nxml-ns-state)))))
96 (defun nxml-ns-get-default ()
97 "Return the current default namespace as a symbol, or nil
98 if there is no default namespace."
101 (defun nxml-ns-set-default (ns)
102 "Changes the current default namespace. The change
103 will be in effect until the end of the current element.
104 NS is a symbol or nil."
106 (cons (cons ns (cdar nxml-ns-state))
107 (cdr nxml-ns-state))))
109 (defun nxml-ns-get-context ()
112 (defun nxml-ns-prefixes-for (ns &optional attributep)
113 (let ((current (car nxml-ns-state))
117 (eq (car current) ns))
118 (setq prefixes '(nil)))
119 (setq current (cdr current))
120 (while (let ((binding (rassq ns current)))
122 (when (eq (nxml-ns-get-prefix (car binding)) ns)
123 (add-to-list 'prefixes
126 (cdr (member binding current))))))
129 (defun nxml-ns-prefix-for (ns)
130 (car (rassq ns (cdar nxml-ns-state))))
132 (defun nxml-ns-changed-prefixes ()
133 (let ((old (cadr nxml-ns-state))
134 (new (car nxml-ns-state))
138 (unless (eq (car new) (car old))
139 (setq changed '(nil)))
142 (while (not (eq new old))
144 (cons (caar new) changed))
145 (setq new (cdr new))))
150 ;; arch-tag: 5968e4b7-fb37-46ce-8621-c65db9793028
151 ;;; nxml-ns.el ends here