]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
infrastructure for 'graceful asserts'
[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 "MathMacroTemplate.h"
16 #include "MathMacroArgument.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19 #include "InsetMathNest.h"
20
21 #include "Buffer.h"
22 #include "DocIterator.h"
23 #include "InsetList.h"
24 #include "Text.h"
25
26 #include "support/debug.h"
27
28 #include "support/assert.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()
44         : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
45           redefinition_(false), type_(MacroTypeNewcommand)
46 {}
47
48         
49         
50 MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
51         : buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
52           optionals_(0), lockCount_(0), redefinition_(false),
53           type_(MacroTypeNewcommand)
54 {
55 }
56         
57         
58 MacroData::MacroData(MathMacroTemplate const & macro)
59         : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
60           redefinition_(false), type_(MacroTypeNewcommand)
61 {
62         queryData(macro);
63 }
64
65
66 void MacroData::expand(vector<MathData> const & args, MathData & to) const
67 {
68         updateData();
69
70         InsetMathSqrt inset; // Hack. Any inset with a cell would do.
71         // FIXME UNICODE
72         asArray(display_.empty() ? definition_ : display_, inset.cell(0));
73         //lyxerr << "MathData::expand: args: " << args << endl;
74         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
75         for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
76                 if (!it.nextInset())
77                         continue;
78                 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
79                         continue;
80                 //it.cell().erase(it.pos());
81                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
82                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
83                 if (n <= args.size()) {
84                         it.cell().erase(it.pos());
85                         it.cell().insert(it.pos(), args[n - 1]);
86                 }
87         }
88         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
89         to = inset.cell(0);
90 }
91
92
93 size_t MacroData::optionals() const
94 {
95         updateData();
96         return optionals_;
97 }
98
99
100 vector<docstring> const &  MacroData::defaults() const
101 {
102         updateData();
103         return defaults_;
104 }
105
106
107 void MacroData::unlock() const
108 {
109         --lockCount_;
110         LASSERT(lockCount_ >= 0, /**/);
111 }
112
113
114 void MacroData::queryData(MathMacroTemplate const & macro) const
115 {
116         if (queried_)
117                 return;
118
119         queried_ = true;
120         definition_ = macro.definition();
121         numargs_ = macro.numArgs();
122         display_ = macro.displayDefinition();
123         redefinition_ = macro.redefinition();
124         type_ = macro.type();
125         optionals_ = macro.numOptionals();
126         
127         macro.getDefaults(defaults_);
128 }
129
130
131 void MacroData::updateData() const
132 {
133         if (queried_)
134                 return;
135
136         LASSERT(buffer_ != 0, /**/);
137         
138         // Try to fix position DocIterator. Should not do anything in theory.
139         pos_.fixIfBroken();
140         
141         // find macro template
142         Inset * inset = pos_.nextInset();
143         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
144                 lyxerr << "BUG: No macro template found by MacroData" << endl;
145                 return;
146         }
147         
148         // query the data from the macro template
149         queryData(static_cast<MathMacroTemplate const &>(*inset));      
150 }
151         
152
153 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
154 {
155         updateData();
156
157         // find macro template
158         Inset * inset = pos_.nextInset();
159         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
160                 lyxerr << "BUG: No macro template found by MacroData" << endl;
161                 return;
162         }
163                 
164         // output template
165         MathMacroTemplate const & tmpl 
166         = static_cast<MathMacroTemplate const &>(*inset);
167         WriteStream wi(os, false, true);
168         tmpl.write(wi, overwriteRedefinition);
169 }
170
171
172 /////////////////////////////////////////////////////////////////////
173 //
174 // The global table of macros
175 //
176 /////////////////////////////////////////////////////////////////////
177
178 MacroTable & MacroTable::globalMacros()
179 {
180         static MacroTable theGlobalMacros;
181         return theGlobalMacros;
182 }
183
184
185 MacroData const * MacroTable::get(docstring const & name) const
186 {
187         const_iterator it = find(name);
188         return it == end() ? 0 : &it->second;
189 }
190
191
192 void MacroTable::insert(docstring const & name, MacroData const & data)
193 {
194         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
195         operator[](name) = data;
196 }
197
198
199 void MacroTable::insert(docstring const & def, string const & requires)
200 {
201         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
202         MathMacroTemplate mac(def);
203         MacroData data(mac);
204         data.requires() = requires;
205         insert(mac.name(), data);
206 }
207
208
209 void MacroTable::getMacroNames(std::set<docstring> & names) const
210 {
211         for (const_iterator it = begin(); it != end(); ++it)
212                 names.insert(it->first);
213 }
214
215
216 void MacroTable::dump()
217 {
218         lyxerr << "\n------------------------------------------" << endl;
219         for (const_iterator it = begin(); it != end(); ++it)
220                 lyxerr << to_utf8(it->first)
221                         << " [" << to_utf8(it->second.definition()) << "] : "
222                         << " [" << to_utf8(it->second.display()) << "] : "
223                         << endl;
224         lyxerr << "------------------------------------------" << endl;
225 }
226
227
228 /////////////////////////////////////////////////////////////////////
229 //
230 // MacroContext
231 //
232 /////////////////////////////////////////////////////////////////////
233
234 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
235         : buf_(buf), pos_(pos)
236 {
237 }
238
239
240 MacroData const * MacroContext::get(docstring const & name) const
241 {
242         return buf_.getMacro(name, pos_);
243 }
244
245 } // namespace lyx