2 * \file math_macrotable.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 "math_macrotable.h"
14 #include "math_macrotemplate.h"
15 #include "math_macroarg.h"
16 #include "math_support.h"
17 #include "math_sqrtinset.h"
20 #include "dociterator.h"
22 #include <boost/assert.hpp>
27 using std::istringstream;
35 MacroData::MacroData()
40 MacroData::MacroData(string const & def, int numargs, string const & disp)
41 : def_(def), numargs_(numargs), disp_(disp)
45 void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
47 MathSqrtInset inset; // Hack. Any inset with a cell would do.
48 asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
49 //lyxerr << "MathData::expand: args: " << args << endl;
50 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
51 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
54 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
56 //it.cell().erase(it.pos());
57 //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
58 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
59 if (n <= args.size()) {
60 it.cell().erase(it.pos());
61 it.cell().insert(it.pos(), args[n - 1]);
64 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
70 MacroTable & MacroTable::globalMacros()
72 static MacroTable theGlobalMacros;
73 return theGlobalMacros;
78 //MacroTable & MacroTable::localMacros()
80 // static MacroTable theLocalMacros;
81 // return theLocalMacros;
85 bool MacroTable::has(string const & name) const
87 return find(name) != end();
91 MacroData const & MacroTable::get(string const & name) const
93 const_iterator it = find(name);
94 BOOST_ASSERT(it != end());
99 void MacroTable::insert(string const & name, MacroData const & data)
101 //lyxerr << "MacroTable::insert: " << name << endl;
102 operator[](name) = data;
106 void MacroTable::insert(string const & def)
108 //lyxerr << "MacroTable::insert, def: " << def << endl;
109 istringstream is(def);
110 MathMacroTemplate mac(is);
111 insert(mac.name(), mac.asMacroData());
115 void MacroTable::dump()
117 lyxerr << "\n------------------------------------------" << endl;
118 for (const_iterator it = begin(); it != end(); ++it)
120 << " [" << it->second.def() << "] : "
121 << " [" << it->second.disp() << "] : "
123 lyxerr << "------------------------------------------" << endl;