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