declare smobs in alloc.c
[bpt/emacs.git] / lisp / nxml / nxml-ns.el
CommitLineData
da3e5ebb 1;;; nxml-ns.el --- XML namespace processing -*- lexical-binding:t -*-
8cd39fb3 2
ba318903 3;; Copyright (C) 2003, 2007-2014 Free Software Foundation, Inc.
8cd39fb3
MH
4
5;; Author: James Clark
3e77f05d 6;; Keywords: wp, hypermedia, languages, XML
8cd39fb3 7
341543da 8;; This file is part of GNU Emacs.
8cd39fb3 9
4936186e 10;; GNU Emacs is free software: you can redistribute it and/or modify
341543da 11;; it under the terms of the GNU General Public License as published by
4936186e
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
8cd39fb3 14
341543da
GM
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.
19
20;; You should have received a copy of the GNU General Public License
4936186e 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8cd39fb3
MH
22
23;;; Commentary:
24
25;; This file uses a prefix of `nxml-ns'.
26
27;;; Code:
28
29(require 'nxml-util)
30
31(defvar nxml-ns-state nil
10545bd8
JB
32 "Contains the state of namespace processing.
33The state is never modified destructively and so can be saved and
34restored without copying.
35
36The value is a stack represented by a list. The list has length
37N + 1 where N is the number of open elements. Each member of the
38list represents the bindings in effect for a particular element.
39Each member is itself a list whose car is the default namespace
8cd39fb3
MH
40\(a symbol or nil) and whose cdr is an alist of (PREFIX . NS) pairs
41where PREFIX is a string (never nil) and NS is the namespace URI
42symbol.")
43
44(defconst nxml-ns-initial-state
45 (list (list nil (cons "xml" nxml-xml-namespace-uri)))
10545bd8
JB
46 "A list to be used as the initial value of `nxml-ns-state'.
47This represents the state with no open elements and with the default
8cd39fb3
MH
48namespace bindings (no default namespace and only the xml prefix bound).")
49
50(defsubst nxml-ns-state () nxml-ns-state)
51
52(defsubst nxml-ns-set-state (state)
53 (setq nxml-ns-state state))
54
55(defsubst nxml-ns-state-equal (state)
56 (equal nxml-ns-state state))
57
58(defmacro nxml-ns-save (&rest body)
da3e5ebb 59 (declare (indent 0) (debug t))
8cd39fb3
MH
60 `(let ((nxml-ns-state nxml-ns-initial-state))
61 ,@body))
62
8cd39fb3
MH
63(defun nxml-ns-init ()
64 (setq nxml-ns-state nxml-ns-initial-state))
65
66(defun nxml-ns-push-state ()
10545bd8
JB
67 "Change the state by starting a new element.
68Namespace declarations are inherited from the parent state."
8cd39fb3
MH
69 (setq nxml-ns-state (cons (car nxml-ns-state) nxml-ns-state)))
70
71(defun nxml-ns-pop-state ()
10545bd8
JB
72 "Change the state by ending an element.
73The behavior is undefined if there is no open element."
8cd39fb3
MH
74 (setq nxml-ns-state (cdr nxml-ns-state)))
75
76(defun nxml-ns-get-prefix (prefix)
10545bd8
JB
77 "Return the symbol for namespace bound to PREFIX.
78Return nil if PREFIX is unbound. PREFIX is a string, never nil."
8cd39fb3
MH
79 (let ((binding (assoc prefix (cdar nxml-ns-state))))
80 (and binding (cdr binding))))
81
82(defun nxml-ns-set-prefix (prefix ns)
10545bd8
JB
83 "Change the binding of PREFIX.
84PREFIX is a string (never nil). NS is a symbol (never nil).
85The change will be in effect until the end of the current element."
8cd39fb3
MH
86 (setq nxml-ns-state
87 (let ((bindings (car nxml-ns-state)))
88 (cons (cons (car bindings)
89 (cons (cons prefix ns) (cdr bindings)))
90 (cdr nxml-ns-state)))))
91
92(defun nxml-ns-get-default ()
10545bd8
JB
93 "Return the current default namespace as a symbol.
94Return nil if there is no default namespace."
8cd39fb3
MH
95 (caar nxml-ns-state))
96
97(defun nxml-ns-set-default (ns)
10545bd8
JB
98 "Changes the current default namespace.
99The change will be in effect until the end of the current element.
8cd39fb3
MH
100NS is a symbol or nil."
101 (setq nxml-ns-state
102 (cons (cons ns (cdar nxml-ns-state))
103 (cdr nxml-ns-state))))
104
105(defun nxml-ns-get-context ()
106 (car nxml-ns-state))
107
108(defun nxml-ns-prefixes-for (ns &optional attributep)
109 (let ((current (car nxml-ns-state))
110 prefixes)
111 (when (if attributep
112 (not ns)
113 (eq (car current) ns))
114 (setq prefixes '(nil)))
115 (setq current (cdr current))
116 (while (let ((binding (rassq ns current)))
117 (when binding
da3e5ebb
SM
118 (let ((prefix (car binding)))
119 (when (eq (nxml-ns-get-prefix prefix) ns)
120 (unless (member prefix prefixes)
121 (push prefix prefixes))))
122 (setq current
123 (cdr (member binding current))))))
8cd39fb3
MH
124 prefixes))
125
126(defun nxml-ns-prefix-for (ns)
127 (car (rassq ns (cdar nxml-ns-state))))
128
129(defun nxml-ns-changed-prefixes ()
130 (let ((old (cadr nxml-ns-state))
131 (new (car nxml-ns-state))
132 changed)
133 (if (eq old new)
134 nil
135 (unless (eq (car new) (car old))
136 (setq changed '(nil)))
137 (setq new (cdr new))
138 (setq old (cdr old))
139 (while (not (eq new old))
140 (setq changed
141 (cons (caar new) changed))
142 (setq new (cdr new))))
143 changed))
10545bd8 144
8cd39fb3
MH
145(provide 'nxml-ns)
146
147;;; nxml-ns.el ends here