]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
4af30d4c5438c25e0d3e332b3223a49471dc6237
[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 #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), optionals_(0), lockCount_(0),
45           redefinition_(false), type_(MacroTypeNewcommand)
46 {}
47
48
49 MacroData::MacroData(Buffer * buf, DocIterator const & pos)
50         : buffer_(buf), pos_(pos), queried_(false), numargs_(0),
51           optionals_(0), lockCount_(0), redefinition_(false),
52           type_(MacroTypeNewcommand)
53 {
54 }
55
56
57 MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro)
58         : buffer_(buf), queried_(false), numargs_(0), optionals_(0), lockCount_(0),
59           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         static InsetMathSqrt inset(0);
71         inset.setBuffer(const_cast<Buffer &>(*buffer_));
72
73         docstring const & definition(display_.empty() ? definition_ : display_);
74         asArray(definition, inset.cell(0));
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<MathMacroArgument*>(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 void MacroData::unlock() const
114 {
115         --lockCount_;
116         LASSERT(lockCount_ >= 0, lockCount_ = 0);
117 }
118
119
120 void MacroData::queryData(MathMacroTemplate const & macro) const
121 {
122         if (queried_)
123                 return;
124
125         queried_ = true;
126         definition_ = macro.definition();
127         numargs_ = macro.numArgs();
128         display_ = macro.displayDefinition();
129         redefinition_ = macro.redefinition();
130         type_ = macro.type();
131         optionals_ = macro.numOptionals();
132
133         macro.getDefaults(defaults_);
134 }
135
136
137 void MacroData::updateData() const
138 {
139         if (queried_)
140                 return;
141
142         LBUFERR(buffer_);
143
144         // Try to fix position DocIterator. Should not do anything in theory.
145         pos_.fixIfBroken();
146
147         // find macro template
148         Inset * inset = pos_.nextInset();
149         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
150                 lyxerr << "BUG: No macro template found by MacroData" << endl;
151                 return;
152         }
153
154         // query the data from the macro template
155         queryData(static_cast<MathMacroTemplate const &>(*inset));
156 }
157
158
159 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
160 {
161         updateData();
162
163         // find macro template
164         Inset * inset = pos_.nextInset();
165         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
166                 lyxerr << "BUG: No macro template found by MacroData" << endl;
167                 return 0;
168         }
169
170         // output template
171         MathMacroTemplate const & tmpl =
172                 static_cast<MathMacroTemplate const &>(*inset);
173         WriteStream wi(os, false, true, WriteStream::wsDefault);
174         return tmpl.write(wi, overwriteRedefinition);
175 }
176
177
178 /////////////////////////////////////////////////////////////////////
179 //
180 // The global table of macros
181 //
182 /////////////////////////////////////////////////////////////////////
183
184 MacroTable & MacroTable::globalMacros()
185 {
186         static MacroTable theGlobalMacros;
187         return theGlobalMacros;
188 }
189
190
191 MacroData const * MacroTable::get(docstring const & name) const
192 {
193         const_iterator it = find(name);
194         return it == end() ? 0 : &it->second;
195 }
196
197
198 MacroTable::iterator
199 MacroTable::insert(docstring const & name, MacroData const & data)
200 {
201         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
202         iterator it = find(name);
203         if (it == end())
204                 it = map<docstring, MacroData>::insert(
205                                 make_pair(name, data)).first;
206         else
207                 it->second = data;
208         return it;
209 }
210
211
212 MacroTable::iterator
213 MacroTable::insert(Buffer * buf, docstring const & def, string const & requires)
214 {
215         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
216         MathMacroTemplate mac(buf, def);
217         MacroData data(buf, mac);
218         data.requires() = requires;
219         return insert(mac.name(), data);
220 }
221
222
223 void MacroTable::getMacroNames(std::set<docstring> & names) const
224 {
225         for (const_iterator it = begin(); it != end(); ++it)
226                 names.insert(it->first);
227 }
228
229
230 void MacroTable::dump()
231 {
232         lyxerr << "\n------------------------------------------" << endl;
233         for (const_iterator it = begin(); it != end(); ++it)
234                 lyxerr << to_utf8(it->first)
235                         << " [" << to_utf8(it->second.definition()) << "] : "
236                         << " [" << to_utf8(it->second.display()) << "] : "
237                         << endl;
238         lyxerr << "------------------------------------------" << endl;
239 }
240
241
242 /////////////////////////////////////////////////////////////////////
243 //
244 // MacroContext
245 //
246 /////////////////////////////////////////////////////////////////////
247
248 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
249         : buf_(buf), pos_(pos)
250 {
251 }
252
253
254 MacroData const * MacroContext::get(docstring const & name) const
255 {
256         return buf_->getMacro(name, pos_);
257 }
258
259 } // namespace lyx