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/lassert.h"
36 /////////////////////////////////////////////////////////////////////
40 /////////////////////////////////////////////////////////////////////
42 MacroData::MacroData()
43 : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
44 redefinition_(false), type_(MacroTypeNewcommand)
49 MacroData::MacroData(Buffer const & 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(MathMacroTemplate const & macro)
58 : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
59 redefinition_(false), type_(MacroTypeNewcommand)
65 void MacroData::expand(vector<MathData> const & args, MathData & to) const
69 // Hack. Any inset with a cell would do.
70 static InsetMathSqrt inset;
73 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
74 //lyxerr << "MathData::expand: args: " << args << endl;
75 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
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<MathMacroArgument*>(it.nextInset())->number();
84 if (n <= args.size()) {
85 it.cell().erase(it.pos());
86 it.cell().insert(it.pos(), args[n - 1]);
89 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
94 size_t MacroData::optionals() const
101 vector<docstring> const & MacroData::defaults() const
108 void MacroData::unlock() const
111 LASSERT(lockCount_ >= 0, /**/);
115 void MacroData::queryData(MathMacroTemplate const & macro) const
121 definition_ = macro.definition();
122 numargs_ = macro.numArgs();
123 display_ = macro.displayDefinition();
124 redefinition_ = macro.redefinition();
125 type_ = macro.type();
126 optionals_ = macro.numOptionals();
128 macro.getDefaults(defaults_);
132 void MacroData::updateData() const
137 LASSERT(buffer_ != 0, /**/);
139 // Try to fix position DocIterator. Should not do anything in theory.
142 // find macro template
143 Inset * inset = pos_.nextInset();
144 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
145 lyxerr << "BUG: No macro template found by MacroData" << endl;
149 // query the data from the macro template
150 queryData(static_cast<MathMacroTemplate const &>(*inset));
154 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
158 // find macro template
159 Inset * inset = pos_.nextInset();
160 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
161 lyxerr << "BUG: No macro template found by MacroData" << endl;
166 MathMacroTemplate const & tmpl =
167 static_cast<MathMacroTemplate const &>(*inset);
168 WriteStream wi(os, false, true, false);
169 tmpl.write(wi, overwriteRedefinition);
173 /////////////////////////////////////////////////////////////////////
175 // The global table of macros
177 /////////////////////////////////////////////////////////////////////
179 MacroTable & MacroTable::globalMacros()
181 static MacroTable theGlobalMacros;
182 return theGlobalMacros;
186 MacroData const * MacroTable::get(docstring const & name) const
188 const_iterator it = find(name);
189 return it == end() ? 0 : &it->second;
193 void MacroTable::insert(docstring const & name, MacroData const & data)
195 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
196 operator[](name) = data;
200 void MacroTable::insert(docstring const & def, string const & requires)
202 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
203 MathMacroTemplate mac(def);
205 data.requires() = requires;
206 insert(mac.name(), data);
210 void MacroTable::getMacroNames(std::set<docstring> & names) const
212 for (const_iterator it = begin(); it != end(); ++it)
213 names.insert(it->first);
217 void MacroTable::dump()
219 lyxerr << "\n------------------------------------------" << endl;
220 for (const_iterator it = begin(); it != end(); ++it)
221 lyxerr << to_utf8(it->first)
222 << " [" << to_utf8(it->second.definition()) << "] : "
223 << " [" << to_utf8(it->second.display()) << "] : "
225 lyxerr << "------------------------------------------" << endl;
229 /////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////
235 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
236 : buf_(buf), pos_(pos)
241 MacroData const * MacroContext::get(docstring const & name) const
243 return buf_.getMacro(name, pos_);