3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathSqrt.h"
14 #include "MacroTable.h"
15 #include "MathMacroTemplate.h"
16 #include "MathMacroArgument.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "InsetMathNest.h"
23 #include "DocIterator.h"
24 #include "InsetList.h"
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lassert.h"
38 /////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////
44 MacroData::MacroData(Buffer * buf)
45 : buffer_(buf), queried_(true), numargs_(0), sym_(0), optionals_(0),
46 lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
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)
58 MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro)
59 : buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0),
60 lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
66 bool MacroData::expand(vector<MathData> const & args, MathData & to) const
70 // Hack. Any inset with a cell would do.
71 InsetMathSqrt inset(const_cast<Buffer *>(buffer_));
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()) {
80 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
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]);
90 //LYXERR0("MathData::expand: res: " << 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
95 return asString(to) != definition;
99 size_t MacroData::optionals() const
106 vector<docstring> const & MacroData::defaults() const
113 string const MacroData::requires() const
116 return sym_->requires;
121 bool MacroData::hidden() const
129 docstring const MacroData::xmlname() const
132 return sym_->xmlname;
137 char const * MacroData::MathMLtype() const
139 return sym_ ? sym_->MathMLtype() : 0;
143 void MacroData::unlock() const
146 LASSERT(lockCount_ >= 0, lockCount_ = 0);
150 void MacroData::queryData(MathMacroTemplate const & macro) const
156 definition_ = macro.definition();
157 numargs_ = macro.numArgs();
158 display_ = macro.displayDefinition();
159 redefinition_ = macro.redefinition();
160 type_ = macro.type();
161 optionals_ = macro.numOptionals();
163 macro.getDefaults(defaults_);
167 void MacroData::updateData() const
174 // Try to fix position DocIterator. Should not do anything in theory.
177 // find macro template
178 Inset * inset = pos_.nextInset();
179 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
180 lyxerr << "BUG: No macro template found by MacroData" << endl;
184 // query the data from the macro template
185 queryData(static_cast<MathMacroTemplate const &>(*inset));
189 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
193 // find macro template
194 Inset * inset = pos_.nextInset();
195 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
196 lyxerr << "BUG: No macro template found by MacroData" << endl;
201 MathMacroTemplate const & tmpl =
202 static_cast<MathMacroTemplate const &>(*inset);
203 WriteStream wi(os, false, true, WriteStream::wsDefault);
204 return tmpl.write(wi, overwriteRedefinition);
208 /////////////////////////////////////////////////////////////////////
210 // The global table of macros
212 /////////////////////////////////////////////////////////////////////
214 MacroTable & MacroTable::globalMacros()
216 static MacroTable theGlobalMacros;
217 return theGlobalMacros;
221 MacroData const * MacroTable::get(docstring const & name) const
223 const_iterator it = find(name);
224 return it == end() ? 0 : &it->second;
229 MacroTable::insert(docstring const & name, MacroData const & data)
231 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
232 iterator it = find(name);
234 it = map<docstring, MacroData>::insert(
235 make_pair(name, data)).first;
243 MacroTable::insert(Buffer * buf, docstring const & def)
245 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
246 MathMacroTemplate mac(buf, def);
247 MacroData data(buf, mac);
248 return insert(mac.name(), data);
252 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
254 for (const_iterator it = begin(); it != end(); ++it)
255 if (gethidden || !it->second.hidden())
256 names.insert(it->first);
260 void MacroTable::dump()
262 lyxerr << "\n------------------------------------------" << endl;
263 for (const_iterator it = begin(); it != end(); ++it)
264 lyxerr << to_utf8(it->first)
265 << " [" << to_utf8(it->second.definition()) << "] : "
266 << " [" << to_utf8(it->second.display()) << "] : "
268 lyxerr << "------------------------------------------" << endl;
272 /////////////////////////////////////////////////////////////////////
276 /////////////////////////////////////////////////////////////////////
278 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
279 : buf_(buf), pos_(pos)
284 MacroData const * MacroContext::get(docstring const & name) const
286 return buf_->getMacro(name, pos_);