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 "MathStream.h"
18 #include "MathSupport.h"
19 #include "InsetMathNest.h"
22 #include "DocIterator.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), optionals_(0), lockCount_(0),
45 redefinition_(false), type_(MacroTypeNewcommand)
49 MacroData::MacroData(Buffer * buf, DocIterator const & pos)
50 : buffer_(buf), pos_(pos), queried_(false), numargs_(0),
51 optionals_(0), lockCount_(0), redefinition_(false),
52 type_(MacroTypeNewcommand)
57 MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro)
58 : buffer_(buf), queried_(false), numargs_(0), optionals_(0), lockCount_(0),
59 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 static InsetMathSqrt inset(0);
71 inset.setBuffer(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 void MacroData::unlock() const
116 LASSERT(lockCount_ >= 0, lockCount_ = 0);
120 void MacroData::queryData(MathMacroTemplate const & macro) const
126 definition_ = macro.definition();
127 numargs_ = macro.numArgs();
128 display_ = macro.displayDefinition();
129 redefinition_ = macro.redefinition();
130 type_ = macro.type();
131 optionals_ = macro.numOptionals();
133 macro.getDefaults(defaults_);
137 void MacroData::updateData() const
144 // Try to fix position DocIterator. Should not do anything in theory.
147 // find macro template
148 Inset * inset = pos_.nextInset();
149 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
150 lyxerr << "BUG: No macro template found by MacroData" << endl;
154 // query the data from the macro template
155 queryData(static_cast<MathMacroTemplate const &>(*inset));
159 int MacroData::write(odocstream & os, bool overwriteRedefinition) const
163 // find macro template
164 Inset * inset = pos_.nextInset();
165 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
166 lyxerr << "BUG: No macro template found by MacroData" << endl;
171 MathMacroTemplate const & tmpl =
172 static_cast<MathMacroTemplate const &>(*inset);
173 WriteStream wi(os, false, true, WriteStream::wsDefault);
174 return tmpl.write(wi, overwriteRedefinition);
178 /////////////////////////////////////////////////////////////////////
180 // The global table of macros
182 /////////////////////////////////////////////////////////////////////
184 MacroTable & MacroTable::globalMacros()
186 static MacroTable theGlobalMacros;
187 return theGlobalMacros;
191 MacroData const * MacroTable::get(docstring const & name) const
193 const_iterator it = find(name);
194 return it == end() ? 0 : &it->second;
199 MacroTable::insert(docstring const & name, MacroData const & data)
201 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
202 iterator it = find(name);
204 it = map<docstring, MacroData>::insert(
205 make_pair(name, data)).first;
213 MacroTable::insert(Buffer * buf, docstring const & def, string const & requires)
215 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
216 MathMacroTemplate mac(buf, def);
217 MacroData data(buf, mac);
218 data.requires() = requires;
219 return insert(mac.name(), data);
223 void MacroTable::getMacroNames(std::set<docstring> & names) const
225 for (const_iterator it = begin(); it != end(); ++it)
226 names.insert(it->first);
230 void MacroTable::dump()
232 lyxerr << "\n------------------------------------------" << endl;
233 for (const_iterator it = begin(); it != end(); ++it)
234 lyxerr << to_utf8(it->first)
235 << " [" << to_utf8(it->second.definition()) << "] : "
236 << " [" << to_utf8(it->second.display()) << "] : "
238 lyxerr << "------------------------------------------" << endl;
242 /////////////////////////////////////////////////////////////////////
246 /////////////////////////////////////////////////////////////////////
248 MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
249 : buf_(buf), pos_(pos)
254 MacroData const * MacroContext::get(docstring const & name) const
256 return buf_->getMacro(name, pos_);