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 "InsetMathMacroTemplate.h"
16 #include "InsetMathMacroArgument.h"
17 #include "MathParser.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "InsetMathNest.h"
23 #include "InsetList.h"
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
43 MacroData::MacroData(Buffer * buf)
44 : buffer_(buf), queried_(true), numargs_(0), sym_(0), optionals_(0),
45 lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
49 MacroData::MacroData(Buffer * buf, DocIterator const & pos)
50 : buffer_(buf), pos_(pos), queried_(false), numargs_(0), sym_(0),
51 optionals_(0), lockCount_(0), redefinition_(false),
52 type_(MacroTypeNewcommand)
57 MacroData::MacroData(Buffer * buf, InsetMathMacroTemplate const & macro)
58 : buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0),
59 lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
65 bool MacroData::expand(vector<MathData> const & args, MathData & to) const
69 // Hack. Any inset with a cell would do.
70 InsetMathSqrt inset(const_cast<Buffer *>(buffer_));
72 docstring const & definition(display_.empty() ? definition_ : display_);
73 asArray(definition, inset.cell(0), Parse::QUIET | Parse::MACRODEF);
74 //lyxerr << "MathData::expand: args: " << args << endl;
75 //LYXERR0("MathData::expand: ar: " << inset.cell(0));
76 for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) {
79 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
81 //it.cell().erase(it.pos());
82 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
83 size_t n = static_cast<InsetMathMacroArgument*>(it.nextInset())->number();
84 if (n <= args.size()) {
85 it.cell().erase(it.pos());
86 it.cell().insert(it.pos(), args[n - 1]);
89 //LYXERR0("MathData::expand: res: " << inset.cell(0));
91 // If the result is equal to the definition then we either have a
92 // recursive loop, or the definition did not contain any macro in the
94 return asString(to) != definition;
98 size_t MacroData::optionals() const
105 vector<docstring> const & MacroData::defaults() const
112 string const MacroData::required() const
115 return sym_->required;
120 bool MacroData::hidden() const
128 docstring const MacroData::htmlname() const
131 return sym_->htmlname;
136 docstring const MacroData::xmlname() const
139 return sym_->xmlname;
144 char const * MacroData::MathMLtype() const
146 return sym_ ? sym_->MathMLtype() : 0;
150 void MacroData::unlock() const
153 LASSERT(lockCount_ >= 0, lockCount_ = 0);
157 void MacroData::queryData(InsetMathMacroTemplate const & macro) const
163 definition_ = macro.definition();
164 numargs_ = macro.numArgs();
165 display_ = macro.displayDefinition();
166 redefinition_ = macro.redefinition();
167 type_ = macro.type();
168 optionals_ = macro.numOptionals();
170 macro.getDefaults(defaults_);
174 void MacroData::updateData() const
181 // Try to fix position DocIterator. Should not do anything in theory.
184 // find macro template
185 Inset * inset = pos_.nextInset();
186 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
187 lyxerr << "BUG: No macro template found by MacroData" << endl;
191 // query the data from the macro template
192 queryData(static_cast<InsetMathMacroTemplate const &>(*inset));
196 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
200 // find macro template
201 Inset * inset = pos_.nextInset();
202 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
203 lyxerr << "BUG: No macro template found by MacroData" << endl;
208 InsetMathMacroTemplate const & tmpl =
209 static_cast<InsetMathMacroTemplate const &>(*inset);
210 otexrowstream ots(os);
211 WriteStream wi(ots, false, true, WriteStream::wsDefault);
212 return tmpl.write(wi, overwriteRedefinition);
216 /////////////////////////////////////////////////////////////////////
218 // The global table of macros
220 /////////////////////////////////////////////////////////////////////
222 MacroTable & MacroTable::globalMacros()
224 static MacroTable theGlobalMacros;
225 return theGlobalMacros;
229 MacroData const * MacroTable::get(docstring const & name) const
231 const_iterator it = find(name);
232 return it == end() ? 0 : &it->second;
237 MacroTable::insert(docstring const & name, MacroData const & data)
239 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
240 iterator it = find(name);
242 it = map<docstring, MacroData>::insert(
243 make_pair(name, data)).first;
251 MacroTable::insert(Buffer * buf, docstring const & def)
253 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
254 InsetMathMacroTemplate mac(buf);
256 MacroData data(buf, mac);
257 return insert(mac.name(), data);
261 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
263 for (const_iterator it = begin(); it != end(); ++it)
264 if (gethidden || !it->second.hidden())
265 names.insert(it->first);
269 void MacroTable::dump()
271 lyxerr << "\n------------------------------------------" << endl;
272 for (const_iterator it = begin(); it != end(); ++it)
273 lyxerr << to_utf8(it->first)
274 << " [" << to_utf8(it->second.definition()) << "] : "
275 << " [" << to_utf8(it->second.display()) << "] : "
277 lyxerr << "------------------------------------------" << endl;
281 /////////////////////////////////////////////////////////////////////
285 /////////////////////////////////////////////////////////////////////
287 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
288 : buf_(buf), pos_(pos)
293 MacroData const * MacroContext::get(docstring const & name) const
295 return buf_->getMacro(name, pos_);