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 static InsetMathSqrt inset(0);
72 inset.setBuffer(const_cast<Buffer &>(*buffer_));
74 docstring const & definition(display_.empty() ? definition_ : display_);
75 asArray(definition, inset.cell(0));
76 //lyxerr << "MathData::expand: args: " << args << endl;
77 //LYXERR0("MathData::expand: ar: " << inset.cell(0));
78 for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) {
81 if (it.nextInset()->lyxCode() != MATH_MACROARG_CODE)
83 //it.cell().erase(it.pos());
84 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
85 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
86 if (n <= args.size()) {
87 it.cell().erase(it.pos());
88 it.cell().insert(it.pos(), args[n - 1]);
91 //LYXERR0("MathData::expand: res: " << inset.cell(0));
93 // If the result is equal to the definition then we either have a
94 // recursive loop, or the definition did not contain any macro in the
96 return asString(to) != definition;
100 size_t MacroData::optionals() const
107 vector<docstring> const & MacroData::defaults() const
114 string const MacroData::requires() const
117 return sym_->requires;
122 void MacroData::unlock() const
125 LASSERT(lockCount_ >= 0, lockCount_ = 0);
129 void MacroData::queryData(MathMacroTemplate const & macro) const
135 definition_ = macro.definition();
136 numargs_ = macro.numArgs();
137 display_ = macro.displayDefinition();
138 redefinition_ = macro.redefinition();
139 type_ = macro.type();
140 optionals_ = macro.numOptionals();
142 macro.getDefaults(defaults_);
146 void MacroData::updateData() const
153 // Try to fix position DocIterator. Should not do anything in theory.
156 // find macro template
157 Inset * inset = pos_.nextInset();
158 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
159 lyxerr << "BUG: No macro template found by MacroData" << endl;
163 // query the data from the macro template
164 queryData(static_cast<MathMacroTemplate const &>(*inset));
168 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
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;
180 MathMacroTemplate const & tmpl =
181 static_cast<MathMacroTemplate const &>(*inset);
182 WriteStream wi(os, false, true, WriteStream::wsDefault);
183 return tmpl.write(wi, overwriteRedefinition);
187 /////////////////////////////////////////////////////////////////////
189 // The global table of macros
191 /////////////////////////////////////////////////////////////////////
193 MacroTable & MacroTable::globalMacros()
195 static MacroTable theGlobalMacros;
196 return theGlobalMacros;
200 MacroData const * MacroTable::get(docstring const & name) const
202 const_iterator it = find(name);
203 return it == end() ? 0 : &it->second;
208 MacroTable::insert(docstring const & name, MacroData const & data)
210 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
211 iterator it = find(name);
213 it = map<docstring, MacroData>::insert(
214 make_pair(name, data)).first;
222 MacroTable::insert(Buffer * buf, docstring const & def)
224 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
225 MathMacroTemplate mac(buf, def);
226 MacroData data(buf, mac);
227 return insert(mac.name(), data);
231 void MacroTable::getMacroNames(std::set<docstring> & names) const
233 for (const_iterator it = begin(); it != end(); ++it)
234 names.insert(it->first);
238 void MacroTable::dump()
240 lyxerr << "\n------------------------------------------" << endl;
241 for (const_iterator it = begin(); it != end(); ++it)
242 lyxerr << to_utf8(it->first)
243 << " [" << to_utf8(it->second.definition()) << "] : "
244 << " [" << to_utf8(it->second.display()) << "] : "
246 lyxerr << "------------------------------------------" << endl;
250 /////////////////////////////////////////////////////////////////////
254 /////////////////////////////////////////////////////////////////////
256 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
257 : buf_(buf), pos_(pos)
262 MacroData const * MacroContext::get(docstring const & name) const
264 return buf_->getMacro(name, pos_);