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 "MacroTable.h"
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"
24 #include "InsetList.h"
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lassert.h"
38 /////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////
44 MacroData::MacroData(const Buffer * buf)
45 : buffer_(buf), queried_(true)
49 MacroData::MacroData(Buffer const * buf, DocIterator const & pos)
50 : buffer_(buf), pos_(pos)
54 MacroData::MacroData(Buffer const * buf, InsetMathMacroTemplate const & macro)
61 bool MacroData::expand(vector<MathData> const & from, MathData & to) const
65 // Hack. Any inset with a cell would do.
66 InsetMathSqrt inset(const_cast<Buffer *>(buffer_));
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()) {
75 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
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]);
85 //LYXERR0("MathData::expand: res: " << 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
90 return asString(to) != definition;
94 size_t MacroData::optionals() const
101 vector<docstring> const & MacroData::defaults() const
108 string const MacroData::required() const
111 return sym_->required;
116 bool MacroData::hidden() const
124 docstring const MacroData::xmlname() const
127 return sym_->xmlname;
132 char const * MacroData::MathMLtype() const
134 return sym_ ? sym_->MathMLtype() : 0;
138 void MacroData::unlock() const
141 LASSERT(lockCount_ >= 0, lockCount_ = 0);
145 void MacroData::queryData(InsetMathMacroTemplate const & macro) const
151 definition_ = macro.definition();
152 numargs_ = macro.numArgs();
153 display_ = macro.displayDefinition();
154 redefinition_ = macro.redefinition();
155 type_ = macro.type();
156 optionals_ = macro.numOptionals();
158 macro.getDefaults(defaults_);
162 void MacroData::updateData() const
169 // Try to fix position DocIterator. Should not do anything in theory.
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;
179 // query the data from the macro template
180 queryData(static_cast<InsetMathMacroTemplate const &>(*inset));
184 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
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;
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);
204 /////////////////////////////////////////////////////////////////////
206 // The global table of macros
208 /////////////////////////////////////////////////////////////////////
210 MacroTable & MacroTable::globalMacros()
212 static MacroTable theGlobalMacros;
213 return theGlobalMacros;
217 MacroData const * MacroTable::get(docstring const & name) const
219 const_iterator it = find(name);
220 return it == end() ? 0 : &it->second;
225 MacroTable::insert(docstring const & name, MacroData const & data)
227 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
228 iterator it = find(name);
230 it = map<docstring, MacroData>::insert(
231 make_pair(name, data)).first;
239 MacroTable::insert(Buffer * buf, docstring const & definition)
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);
249 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
251 for (const_iterator it = begin(); it != end(); ++it)
252 if (gethidden || !it->second.hidden())
253 names.insert(it->first);
257 void MacroTable::dump()
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()) << "] : "
265 lyxerr << "------------------------------------------" << endl;
269 /////////////////////////////////////////////////////////////////////
273 /////////////////////////////////////////////////////////////////////
275 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
276 : buf_(buf), pos_(pos)
281 MacroData const * MacroContext::get(docstring const & name) const
283 return buf_->getMacro(name, pos_);