]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
Remove duplicate and unused header includes in .cpp files
[lyx.git] / src / mathed / MacroTable.cpp
1 /**
2  * \file MacroTable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathSqrt.h"
14 #include "MacroTable.h"
15 #include "InsetMathMacroTemplate.h"
16 #include "InsetMathMacroArgument.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "InsetMathNest.h"
21
22 #include "Buffer.h"
23 #include "InsetList.h"
24 #include "Text.h"
25
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
29
30 #include <sstream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 /////////////////////////////////////////////////////////////////////
38 //
39 // MacroData
40 //
41 /////////////////////////////////////////////////////////////////////
42
43 MacroData::MacroData(Buffer * buf)
44         : buffer_(buf), queried_(true), numargs_(0), sym_(0), optionals_(0),
45           lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
46 {}
47
48
49 MacroData::MacroData(Buffer * buf, DocIterator const & pos)
50         : buffer_(buf), pos_(pos), queried_(false), numargs_(0), sym_(0),
51           optionals_(0), lockCount_(0), redefinition_(false),
52           type_(MacroTypeNewcommand)
53 {
54 }
55
56
57 MacroData::MacroData(Buffer * buf, InsetMathMacroTemplate const & macro)
58         : buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0),
59           lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
60 {
61         queryData(macro);
62 }
63
64
65 bool MacroData::expand(vector<MathData> const & args, MathData & to) const
66 {
67         updateData();
68
69         // Hack. Any inset with a cell would do.
70         InsetMathSqrt inset(const_cast<Buffer *>(buffer_));
71
72         docstring const & definition(display_.empty() ? definition_ : display_);
73         asArray(definition, inset.cell(0), Parse::QUIET | Parse::MACRODEF);
74         //lyxerr << "MathData::expand: args: " << args << endl;
75         //LYXERR0("MathData::expand: ar: " << inset.cell(0));
76         for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) {
77                 if (!it.nextInset())
78                         continue;
79                 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
80                         continue;
81                 //it.cell().erase(it.pos());
82                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
83                 size_t n = static_cast<InsetMathMacroArgument*>(it.nextInset())->number();
84                 if (n <= args.size()) {
85                         it.cell().erase(it.pos());
86                         it.cell().insert(it.pos(), args[n - 1]);
87                 }
88         }
89         //LYXERR0("MathData::expand: res: " << inset.cell(0));
90         to = inset.cell(0);
91         // If the result is equal to the definition then we either have a
92         // recursive loop, or the definition did not contain any macro in the
93         // first place.
94         return asString(to) != definition;
95 }
96
97
98 size_t MacroData::optionals() const
99 {
100         updateData();
101         return optionals_;
102 }
103
104
105 vector<docstring> const & MacroData::defaults() const
106 {
107         updateData();
108         return defaults_;
109 }
110
111
112 string const MacroData::required() const
113 {
114         if (sym_)
115                 return sym_->required;
116         return string();
117 }
118
119
120 bool MacroData::hidden() const
121 {
122         if (sym_)
123                 return sym_->hidden;
124         return false;
125 }
126
127
128 docstring const MacroData::htmlname() const
129 {
130         if (sym_)
131                 return sym_->htmlname;
132         return docstring();
133 }
134
135
136 docstring const MacroData::xmlname() const
137 {
138         if (sym_)
139                 return sym_->xmlname;
140         return docstring();
141 }
142
143
144 char const * MacroData::MathMLtype() const
145 {
146         return sym_ ? sym_->MathMLtype() : 0;
147 }
148
149
150 void MacroData::unlock() const
151 {
152         --lockCount_;
153         LASSERT(lockCount_ >= 0, lockCount_ = 0);
154 }
155
156
157 void MacroData::queryData(InsetMathMacroTemplate const & macro) const
158 {
159         if (queried_)
160                 return;
161
162         queried_ = true;
163         definition_ = macro.definition();
164         numargs_ = macro.numArgs();
165         display_ = macro.displayDefinition();
166         redefinition_ = macro.redefinition();
167         type_ = macro.type();
168         optionals_ = macro.numOptionals();
169
170         macro.getDefaults(defaults_);
171 }
172
173
174 void MacroData::updateData() const
175 {
176         if (queried_)
177                 return;
178
179         LBUFERR(buffer_);
180
181         // Try to fix position DocIterator. Should not do anything in theory.
182         pos_.fixIfBroken();
183
184         // find macro template
185         Inset * inset = pos_.nextInset();
186         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
187                 lyxerr << "BUG: No macro template found by MacroData" << endl;
188                 return;
189         }
190
191         // query the data from the macro template
192         queryData(static_cast<InsetMathMacroTemplate const &>(*inset));
193 }
194
195
196 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
197 {
198         updateData();
199
200         // find macro template
201         Inset * inset = pos_.nextInset();
202         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
203                 lyxerr << "BUG: No macro template found by MacroData" << endl;
204                 return 0;
205         }
206
207         // output template
208         InsetMathMacroTemplate const & tmpl =
209                 static_cast<InsetMathMacroTemplate const &>(*inset);
210         otexrowstream ots(os);
211         WriteStream wi(ots, false, true, WriteStream::wsDefault);
212         return tmpl.write(wi, overwriteRedefinition);
213 }
214
215
216 /////////////////////////////////////////////////////////////////////
217 //
218 // The global table of macros
219 //
220 /////////////////////////////////////////////////////////////////////
221
222 MacroTable & MacroTable::globalMacros()
223 {
224         static MacroTable theGlobalMacros;
225         return theGlobalMacros;
226 }
227
228
229 MacroData const * MacroTable::get(docstring const & name) const
230 {
231         const_iterator it = find(name);
232         return it == end() ? 0 : &it->second;
233 }
234
235
236 MacroTable::iterator
237 MacroTable::insert(docstring const & name, MacroData const & data)
238 {
239         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
240         iterator it = find(name);
241         if (it == end())
242                 it = map<docstring, MacroData>::insert(
243                                 make_pair(name, data)).first;
244         else
245                 it->second = data;
246         return it;
247 }
248
249
250 MacroTable::iterator
251 MacroTable::insert(Buffer * buf, docstring const & def)
252 {
253         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
254         InsetMathMacroTemplate mac(buf);
255         mac.fromString(def);
256         MacroData data(buf, mac);
257         return insert(mac.name(), data);
258 }
259
260
261 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
262 {
263         for (const_iterator it = begin(); it != end(); ++it)
264                 if (gethidden || !it->second.hidden())
265                         names.insert(it->first);
266 }
267
268
269 void MacroTable::dump()
270 {
271         lyxerr << "\n------------------------------------------" << endl;
272         for (const_iterator it = begin(); it != end(); ++it)
273                 lyxerr << to_utf8(it->first)
274                         << " [" << to_utf8(it->second.definition()) << "] : "
275                         << " [" << to_utf8(it->second.display()) << "] : "
276                         << endl;
277         lyxerr << "------------------------------------------" << endl;
278 }
279
280
281 /////////////////////////////////////////////////////////////////////
282 //
283 // MacroContext
284 //
285 /////////////////////////////////////////////////////////////////////
286
287 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
288         : buf_(buf), pos_(pos)
289 {
290 }
291
292
293 MacroData const * MacroContext::get(docstring const & name) const
294 {
295         return buf_->getMacro(name, pos_);
296 }
297
298 } // namespace lyx