#include "math_macrotable.h"
#include "math_macrotemplate.h"
+#include "math_macroarg.h"
+#include "math_support.h"
+#include "math_sqrtinset.h"
+
#include "debug.h"
+#include "dociterator.h"
+#include <boost/assert.hpp>
+
+#include <sstream>
-using std::string;
using std::endl;
+using std::istringstream;
+using std::map;
+using std::pair;
+using std::string;
+using std::vector;
+using std::size_t;
-MathMacroTable::table_type MathMacroTable::macro_table;
+MacroData::MacroData()
+ : numargs_(0)
+{}
-void MathMacroTable::dump()
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+ : def_(def), numargs_(numargs), disp_(disp)
+{}
+
+
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
{
-/*
- lyxerr << "\n------------------------------------------" << endl;
- table_type::const_iterator it;
- for (it = macro_table.begin(); it != macro_table.end(); ++it)
- lyxerr << it->first
- << " [" << it->second->asMacroTemplate()->nargs() << "] : "
- << it->second->cell(0) << endl;
- lyxerr << "------------------------------------------" << endl;
-*/
+ MathSqrtInset inset; // Hack. Any inset with a cell would do.
+ asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
+ //lyxerr << "MathData::expand: args: " << args << endl;
+ //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
+ for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
+ if (!it.nextInset())
+ continue;
+ if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
+ continue;
+ //it.cell().erase(it.pos());
+ //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
+ size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
+ if (n <= args.size()) {
+ it.cell().erase(it.pos());
+ it.cell().insert(it.pos(), args[n - 1]);
+ }
+ }
+ //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
+ to = inset.cell(0);
}
-MathAtom & MathMacroTable::provide(string const & name)
+// The global table.
+MacroTable & MacroTable::globalMacros()
{
- table_type::iterator pos = macro_table.find(name);
- if (pos == macro_table.end()) {
- lyxerr << "MathMacroTable::provideTemplate: no template with name '"
- << name << "' available." << endl;
- }
- return pos->second;
+ static MacroTable theGlobalMacros;
+ return theGlobalMacros;
+}
+
+
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+// static MacroTable theLocalMacros;
+// return theLocalMacros;
+//}
+
+
+bool MacroTable::has(string const & name) const
+{
+ return find(name) != end();
+}
+
+
+MacroData const & MacroTable::get(string const & name) const
+{
+ const_iterator it = find(name);
+ BOOST_ASSERT(it != end());
+ return it->second;
}
-void MathMacroTable::create(MathAtom const & at)
+void MacroTable::insert(string const & name, MacroData const & data)
{
- macro_table[at->asMacroTemplate()->name()] = at;
+ //lyxerr << "MacroTable::insert: " << name << endl;
+ operator[](name) = data;
}
-bool MathMacroTable::has(string const & name)
+void MacroTable::insert(string const & def)
{
- return macro_table.find(name) != macro_table.end();
+ //lyxerr << "MacroTable::insert, def: " << def << endl;
+ istringstream is(def);
+ MathMacroTemplate mac(is);
+ insert(mac.name(), mac.asMacroData());
+}
+
+
+void MacroTable::dump()
+{
+ lyxerr << "\n------------------------------------------" << endl;
+ for (const_iterator it = begin(); it != end(); ++it)
+ lyxerr << it->first
+ << " [" << it->second.def() << "] : "
+ << " [" << it->second.disp() << "] : "
+ << endl;
+ lyxerr << "------------------------------------------" << endl;
}