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::htmlname() const
127 return sym_->htmlname;
132 docstring const MacroData::xmlname() const
135 return sym_->xmlname;
140 char const * MacroData::MathMLtype() const
142 return sym_ ? sym_->MathMLtype() : 0;
146 void MacroData::unlock() const
149 LASSERT(lockCount_ >= 0, lockCount_ = 0);
153 void MacroData::queryData(InsetMathMacroTemplate const & macro) const
159 definition_ = macro.definition();
160 numargs_ = macro.numArgs();
161 display_ = macro.displayDefinition();
162 redefinition_ = macro.redefinition();
163 type_ = macro.type();
164 optionals_ = macro.numOptionals();
166 macro.getDefaults(defaults_);
170 void MacroData::updateData() const
177 // Try to fix position DocIterator. Should not do anything in theory.
180 // find macro template
181 Inset * inset = pos_.nextInset();
182 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
183 lyxerr << "BUG: No macro template found by MacroData" << endl;
187 // query the data from the macro template
188 queryData(static_cast<InsetMathMacroTemplate const &>(*inset));
192 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
196 // find macro template
197 Inset * inset = pos_.nextInset();
198 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
199 lyxerr << "BUG: No macro template found by MacroData" << endl;
204 InsetMathMacroTemplate const & tmpl =
205 static_cast<InsetMathMacroTemplate const &>(*inset);
206 otexrowstream ots(os);
207 TeXMathStream wi(ots, false, true, TeXMathStream::wsDefault);
208 return tmpl.write(wi, overwriteRedefinition);
212 /////////////////////////////////////////////////////////////////////
214 // The global table of macros
216 /////////////////////////////////////////////////////////////////////
218 MacroTable & MacroTable::globalMacros()
220 static MacroTable theGlobalMacros;
221 return theGlobalMacros;
225 MacroData const * MacroTable::get(docstring const & name) const
227 const_iterator it = find(name);
228 return it == end() ? 0 : &it->second;
233 MacroTable::insert(docstring const & name, MacroData const & data)
235 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
236 iterator it = find(name);
238 it = map<docstring, MacroData>::insert(
239 make_pair(name, data)).first;
247 MacroTable::insert(Buffer * buf, docstring const & definition)
249 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
250 InsetMathMacroTemplate mac(buf);
251 mac.fromString(definition);
252 MacroData data(buf, mac);
253 return insert(mac.name(), data);
257 void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
259 for (const_iterator it = begin(); it != end(); ++it)
260 if (gethidden || !it->second.hidden())
261 names.insert(it->first);
265 void MacroTable::dump()
267 lyxerr << "\n------------------------------------------" << endl;
268 for (const_iterator it = begin(); it != end(); ++it)
269 lyxerr << to_utf8(it->first)
270 << " [" << to_utf8(it->second.definition()) << "] : "
271 << " [" << to_utf8(it->second.display()) << "] : "
273 lyxerr << "------------------------------------------" << endl;
277 /////////////////////////////////////////////////////////////////////
281 /////////////////////////////////////////////////////////////////////
283 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
284 : buf_(buf), pos_(pos)
289 MacroData const * MacroContext::get(docstring const & name) const
291 return buf_->getMacro(name, pos_);