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