2 * \file MathMacroTable.C
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 "MathMacroTable.h"
14 #include "MathMacroTemplate.h"
15 #include "MathMacroArgument.h"
16 #include "MathSupport.h"
17 #include "InsetMathSqrt.h"
20 #include "dociterator.h"
22 #include <boost/assert.hpp>
30 using std::istringstream;
38 MacroData::MacroData()
43 MacroData::MacroData(docstring const & def, int numargs, docstring const & disp, string const & requires)
44 : def_(def), numargs_(numargs), disp_(disp), requires_(requires)
48 void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
50 InsetMathSqrt inset; // Hack. Any inset with a cell would do.
52 asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
53 //lyxerr << "MathData::expand: args: " << args << endl;
54 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
55 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
58 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
60 //it.cell().erase(it.pos());
61 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
62 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
63 if (n <= args.size()) {
64 it.cell().erase(it.pos());
65 it.cell().insert(it.pos(), args[n - 1]);
68 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
74 MacroTable & MacroTable::globalMacros()
76 static MacroTable theGlobalMacros;
77 return theGlobalMacros;
81 bool MacroTable::has(docstring const & name) const
83 return find(name) != end();
87 MacroData const & MacroTable::get(docstring const & name) const
89 const_iterator it = find(name);
90 BOOST_ASSERT(it != end());
95 void MacroTable::insert(docstring const & name, MacroData const & data)
97 //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
98 operator[](name) = data;
102 void MacroTable::insert(docstring const & def, string const & requires)
104 //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
105 MathMacroTemplate mac(def);
106 MacroData data = mac.asMacroData();
107 data.requires() = requires;
108 insert(mac.name(), data);
112 void MacroTable::dump()
114 lyxerr << "\n------------------------------------------" << endl;
115 for (const_iterator it = begin(); it != end(); ++it)
116 lyxerr << to_utf8(it->first)
117 << " [" << to_utf8(it->second.def()) << "] : "
118 << " [" << to_utf8(it->second.disp()) << "] : "
120 lyxerr << "------------------------------------------" << endl;