]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
Make BufferView::singeParUpdate more robust
[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::xmlname() const
125 {
126         if (sym_)
127                 return sym_->xmlname;
128         return docstring();
129 }
130
131
132 char const * MacroData::MathMLtype() const
133 {
134         return sym_ ? sym_->MathMLtype() : 0;
135 }
136
137
138 void MacroData::unlock() const
139 {
140         --lockCount_;
141         LASSERT(lockCount_ >= 0, lockCount_ = 0);
142 }
143
144
145 void MacroData::queryData(InsetMathMacroTemplate const & macro) const
146 {
147         if (queried_)
148                 return;
149
150         queried_ = true;
151         definition_ = macro.definition();
152         numargs_ = macro.numArgs();
153         display_ = macro.displayDefinition();
154         redefinition_ = macro.redefinition();
155         type_ = macro.type();
156         optionals_ = macro.numOptionals();
157
158         macro.getDefaults(defaults_);
159 }
160
161
162 void MacroData::updateData() const
163 {
164         if (queried_)
165                 return;
166
167         LBUFERR(buffer_);
168
169         // Try to fix position DocIterator. Should not do anything in theory.
170         pos_.fixIfBroken();
171
172         // find macro template
173         Inset * inset = pos_.nextInset();
174         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
175                 lyxerr << "BUG: No macro template found by MacroData" << endl;
176                 return;
177         }
178
179         // query the data from the macro template
180         queryData(static_cast<InsetMathMacroTemplate const &>(*inset));
181 }
182
183
184 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
185 {
186         updateData();
187
188         // find macro template
189         Inset * inset = pos_.nextInset();
190         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
191                 lyxerr << "BUG: No macro template found by MacroData" << endl;
192                 return 0;
193         }
194
195         // output template
196         InsetMathMacroTemplate const & tmpl =
197                 static_cast<InsetMathMacroTemplate const &>(*inset);
198         otexrowstream ots(os);
199         TeXMathStream wi(ots, false, true, TeXMathStream::wsDefault);
200         return tmpl.write(wi, overwriteRedefinition);
201 }
202
203
204 /////////////////////////////////////////////////////////////////////
205 //
206 // The global table of macros
207 //
208 /////////////////////////////////////////////////////////////////////
209
210 MacroTable & MacroTable::globalMacros()
211 {
212         static MacroTable theGlobalMacros;
213         return theGlobalMacros;
214 }
215
216
217 MacroData const * MacroTable::get(docstring const & name) const
218 {
219         const_iterator it = find(name);
220         return it == end() ? 0 : &it->second;
221 }
222
223
224 MacroTable::iterator
225 MacroTable::insert(docstring const & name, MacroData const & data)
226 {
227         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
228         iterator it = find(name);
229         if (it == end())
230                 it = map<docstring, MacroData>::insert(
231                                 make_pair(name, data)).first;
232         else
233                 it->second = data;
234         return it;
235 }
236
237
238 MacroTable::iterator
239 MacroTable::insert(Buffer * buf, docstring const & definition)
240 {
241         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
242         InsetMathMacroTemplate mac(buf);
243         mac.fromString(definition);
244         MacroData data(buf, mac);
245         return insert(mac.name(), data);
246 }
247
248
249 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
250 {
251         for (const_iterator it = begin(); it != end(); ++it)
252                 if (gethidden || !it->second.hidden())
253                         names.insert(it->first);
254 }
255
256
257 void MacroTable::dump()
258 {
259         lyxerr << "\n------------------------------------------" << endl;
260         for (const_iterator it = begin(); it != end(); ++it)
261                 lyxerr << to_utf8(it->first)
262                         << " [" << to_utf8(it->second.definition()) << "] : "
263                         << " [" << to_utf8(it->second.display()) << "] : "
264                         << endl;
265         lyxerr << "------------------------------------------" << endl;
266 }
267
268
269 /////////////////////////////////////////////////////////////////////
270 //
271 // MacroContext
272 //
273 /////////////////////////////////////////////////////////////////////
274
275 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
276         : buf_(buf), pos_(pos)
277 {
278 }
279
280
281 MacroData const * MacroContext::get(docstring const & name) const
282 {
283         return buf_->getMacro(name, pos_);
284 }
285
286 } // namespace lyx