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"
28 #include "support/lassert.h"
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
43 MacroData::MacroData()
44 : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
45 redefinition_(false), type_(MacroTypeNewcommand)
50 MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
51 : buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
52 optionals_(0), lockCount_(0), redefinition_(false),
53 type_(MacroTypeNewcommand)
58 MacroData::MacroData(MathMacroTemplate const & macro)
59 : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
60 redefinition_(false), type_(MacroTypeNewcommand)
66 void MacroData::expand(vector<MathData> const & args, MathData & to) const
70 InsetMathSqrt inset; // Hack. Any inset with a cell would do.
72 asArray(display_.empty() ? definition_ : display_, inset.cell(0));
73 //lyxerr << "MathData::expand: args: " << args << endl;
74 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
75 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
78 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
80 //it.cell().erase(it.pos());
81 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
82 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
83 if (n <= args.size()) {
84 it.cell().erase(it.pos());
85 it.cell().insert(it.pos(), args[n - 1]);
88 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
93 size_t MacroData::optionals() const
100 vector<docstring> const & MacroData::defaults() const
107 void MacroData::unlock() const
110 LASSERT(lockCount_ >= 0, /**/);
114 void MacroData::queryData(MathMacroTemplate const & macro) const
120 definition_ = macro.definition();
121 numargs_ = macro.numArgs();
122 display_ = macro.displayDefinition();
123 redefinition_ = macro.redefinition();
124 type_ = macro.type();
125 optionals_ = macro.numOptionals();
127 macro.getDefaults(defaults_);
131 void MacroData::updateData() const
136 LASSERT(buffer_ != 0, /**/);
138 // Try to fix position DocIterator. Should not do anything in theory.
141 // find macro template
142 Inset * inset = pos_.nextInset();
143 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
144 lyxerr << "BUG: No macro template found by MacroData" << endl;
148 // query the data from the macro template
149 queryData(static_cast<MathMacroTemplate const &>(*inset));
153 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
157 // find macro template
158 Inset * inset = pos_.nextInset();
159 if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
160 lyxerr << "BUG: No macro template found by MacroData" << endl;
165 MathMacroTemplate const & tmpl
166 = static_cast<MathMacroTemplate const &>(*inset);
167 WriteStream wi(os, false, true);
168 tmpl.write(wi, overwriteRedefinition);
172 /////////////////////////////////////////////////////////////////////
174 // The global table of macros
176 /////////////////////////////////////////////////////////////////////
178 MacroTable & MacroTable::globalMacros()
180 static MacroTable theGlobalMacros;
181 return theGlobalMacros;
185 MacroData const * MacroTable::get(docstring const & name) const
187 const_iterator it = find(name);
188 return it == end() ? 0 : &it->second;
192 void MacroTable::insert(docstring const & name, MacroData const & data)
194 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
195 operator[](name) = data;
199 void MacroTable::insert(docstring const & def, string const & requires)
201 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
202 MathMacroTemplate mac(def);
204 data.requires() = requires;
205 insert(mac.name(), data);
209 void MacroTable::getMacroNames(std::set<docstring> & names) const
211 for (const_iterator it = begin(); it != end(); ++it)
212 names.insert(it->first);
216 void MacroTable::dump()
218 lyxerr << "\n------------------------------------------" << endl;
219 for (const_iterator it = begin(); it != end(); ++it)
220 lyxerr << to_utf8(it->first)
221 << " [" << to_utf8(it->second.definition()) << "] : "
222 << " [" << to_utf8(it->second.display()) << "] : "
224 lyxerr << "------------------------------------------" << endl;
228 /////////////////////////////////////////////////////////////////////
232 /////////////////////////////////////////////////////////////////////
234 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
235 : buf_(buf), pos_(pos)
240 MacroData const * MacroContext::get(docstring const & name) const
242 return buf_.getMacro(name, pos_);