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 "support/std_sstream.h"
24 #include <boost/assert.hpp>
27 using std::istringstream;
34 MacroData::MacroData()
39 MacroData::MacroData(string const & def, int numargs, string const & disp)
40 : def_(def), numargs_(numargs), disp_(disp)
44 void MacroData::expand(MathArray const & args, MathArray & to) const
46 MathSqrtInset inset; // Hack. Any inset with a cell would do.
47 asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
48 //lyxerr << "MathData::expand: args: " << args << endl;
49 //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
50 for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
53 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
55 //it.cell().erase(it.pos());
56 //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
57 int n = static_cast<MathMacroArgument*>(it.nextInset())->number();
58 if (n <= args.size()) {
59 if (args[n - 1]->asBraceInset()) {
60 it.cell().erase(it.pos());
61 it.cell().insert(it.pos(), args[n - 1]->cell(0));
63 it.cell()[it.pos()] = args[n - 1];
67 //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
74 MacroTable & MacroTable::globalMacros()
76 static MacroTable theGlobalMacros;
77 return theGlobalMacros;
81 bool MacroTable::has(string const & name) const
83 return macros_.find(name) != macros_.end();
87 MacroData const & MacroTable::get(string const & name) const
89 table_type::const_iterator it = macros_.find(name);
90 BOOST_ASSERT(it != macros_.end());
95 void MacroTable::insert(string const & name, MacroData const & data)
97 lyxerr << "MathMacroTable::insert: " << name << endl;
102 void MacroTable::insert(string const & def)
104 //lyxerr << "MathMacroTable::insert, def: " << def << endl;
105 istringstream is(def);
106 MathMacroTemplate mac(is);
107 insert(mac.name(), mac.asMacroData());
111 void MacroTable::dump()
113 lyxerr << "\n------------------------------------------" << endl;
114 table_type::const_iterator it;
115 for (it = macros_.begin(); it != macros_.end(); ++it)
117 << " [" << it->second.def() << "] : "
118 << " [" << it->second.disp() << "] : "
120 lyxerr << "------------------------------------------" << endl;