]> git.lyx.org Git - features.git/blob - src/mathed/MacroTable.h
Make macro xhtml export more secure
[features.git] / src / mathed / MacroTable.h
1 // -*- C++ -*-
2 /**
3  * \file MacroTable.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  * \author Stefan Schimanski
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_MACROTABLE_H
14 #define MATH_MACROTABLE_H
15
16 #include "DocIterator.h"
17
18 #include "support/docstring.h"
19
20 #include <map>
21 #include <set>
22 #include <vector>
23
24 namespace lyx {
25
26 class Buffer;
27 class MathData;
28 class MathMacroTemplate;
29 class Paragraph;
30 class latexkeys;
31
32 enum MacroType {
33         MacroTypeNewcommand,
34         MacroTypeNewcommandx,
35         MacroTypeDef
36 };
37
38 ///
39 class MacroData {
40 public:
41         /// Constructor to make STL containers happy
42         MacroData(Buffer * buf = 0);
43         /// Create lazy MacroData which only queries the macro template when needed
44         MacroData(Buffer * buf, DocIterator const & pos);
45         /// Create non-lazy MacroData which directly queries the macro template
46         MacroData(Buffer * buf, MathMacroTemplate const & macro);
47
48         ///
49         docstring const & definition() const { updateData(); return definition_; }
50         ///
51         docstring const & display() const { updateData(); return display_; }
52         /// arity including optional arguments (if there is any)
53         size_t numargs() const { updateData(); return numargs_; }
54         /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
55         /// optional argument
56         /// \return whether anything was expanded
57         bool expand(std::vector<MathData> const & from, MathData & to) const;
58         /// number of optional arguments
59         size_t optionals() const;
60         ///
61         std::vector<docstring> const & defaults() const;
62         ///
63         std::string const requires() const;
64         ///
65         docstring const xmlname() const;
66         ///
67         char const * MathMLtype() const;
68         ///
69         void setSymbol(latexkeys const * sym) { sym_ = sym; }
70
71         /// lock while being drawn to avoid recursions
72         int lock() const { return ++lockCount_; }
73         /// is it being drawn?
74         bool locked() const { return lockCount_ != 0; }
75         ///
76         void unlock() const;
77
78         ///
79         bool redefinition() const { updateData(); return redefinition_; }
80
81         ///
82         MacroType type() const { updateData(); return type_; }
83
84         /// output as TeX macro, only works for lazy MacroData!!!
85         int write(odocstream & os, bool overwriteRedefinition) const;
86
87         ///
88         bool operator==(MacroData const & x) const {
89                 updateData();
90                 x.updateData();
91                 return definition_ == x.definition_
92                         && numargs_ == x.numargs_
93                         && display_ == x.display_
94                         && sym_ == x.sym_
95                         && optionals_ == x.optionals_
96                         && defaults_ == x.defaults_;
97         }
98         ///
99         bool operator!=(MacroData const & x) const { return !operator==(x); }
100
101 private:
102         ///
103         void queryData(MathMacroTemplate const & macro) const;
104         ///
105         void updateData() const;
106         ///
107         Buffer const * buffer_;
108         /// The position of the definition in the buffer.
109         /// There is no guarantee it stays valid if the buffer
110         /// changes. But it (normally) exists only until the
111         /// next Buffer::updateMacros call where new MacroData
112         /// objects are created for each macro definition.
113         /// In the worst case, it is invalidated and the MacroData
114         /// returns its defaults values and the user sees unfolded
115         /// macros.
116         mutable DocIterator pos_;
117         ///
118         mutable bool queried_;
119         ///
120         mutable docstring definition_;
121         ///
122         mutable size_t numargs_;
123         ///
124         mutable docstring display_;
125         ///
126         latexkeys const * sym_;
127         ///
128         mutable size_t optionals_;
129         ///
130         mutable std::vector<docstring> defaults_;
131         ///
132         mutable int lockCount_;
133         ///
134         mutable bool redefinition_;
135         ///
136         mutable MacroType type_;
137 };
138
139
140 ///
141 class MacroNameSet : public std::set<docstring> {};
142 ///
143 class MacroSet : public std::set<MacroData const *> {};
144
145
146 /// A lookup table of macro definitions.
147 /**
148  * This contains a table of "global" macros that are always accessible,
149  * either because they implement a feature of standard LaTeX or some
150  * hack to display certain contents nicely.
151  *
152  **/
153 class MacroTable : public std::map<docstring, MacroData>
154 {
155 public:
156         /// Parse full "\\def..." or "\\newcommand..." or ...
157         iterator insert(Buffer * buf, docstring const & definition);
158         /// Insert pre-digested macro definition
159         iterator insert(docstring const & name, MacroData const & data);
160         ///
161         MacroData const * get(docstring const & name) const;
162         ///
163         void dump();
164         ///
165         void getMacroNames(std::set<docstring> & names) const;
166
167         /// the global list
168         static MacroTable & globalMacros();
169 };
170
171
172 /// A context to lookup macros at a certain position in a buffer.
173 /**
174  * The MacroContext is used during metrics calculation to resolve
175  * macro instances according to the position of them in the buffer
176  * document. Only macro definition in front of the macro instance
177  * are visible and are resolved.
178  *
179  **/
180 class MacroContext {
181 public:
182         /// construct context for the insets at pos
183         MacroContext(Buffer const * buf, DocIterator const & pos);
184
185         /// Lookup macro
186         MacroData const * get(docstring const & name) const;
187
188 private:
189         ///
190         Buffer const * buf_;
191         ///
192         DocIterator const & pos_;
193 };
194
195 } // namespace lyx
196
197 #endif