-#include <config.h>
+/**
+ * \file math_macrotable.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
#include "math_macrotable.h"
-#include "math_macro.h"
#include "math_macrotemplate.h"
-#include "math_iter.h"
-#include "array.h"
-#include "math_accentinset.h"
-#include "math_deliminset.h"
-#include "math_fracinset.h"
-#include "math_parinset.h"
+#include "math_macroarg.h"
+#include "math_support.h"
+#include "math_sqrtinset.h"
+
#include "debug.h"
-#include "support/LAssert.h"
+#include "dociterator.h"
-using std::endl;
-using std::make_pair;
+#include <boost/assert.hpp>
-MathMacroTable::table_type MathMacroTable::macro_table;
+#include <sstream>
-bool MathMacroTable::built = false;
+using std::endl;
+using std::istringstream;
+using std::map;
+using std::pair;
+using std::string;
+using std::vector;
+using std::size_t;
-void MathMacroTable::dump()
-{
- using std::cerr;
+MacroData::MacroData()
+ : numargs_(0)
+{}
- cerr << "\n------------------------------------------\n";
- table_type::const_iterator it;
- for (it = macro_table.begin(); it != macro_table.end(); ++it)
- cerr << it->first << ": " << it->second->GetData() << endl;
- cerr << "------------------------------------------\n";
-}
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+ : def_(def), numargs_(numargs), disp_(disp)
+{}
-MathMacroTemplate &
-MathMacroTable::provideTemplate(string const & name, int na)
+
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
{
- if (!built)
- builtinMacros();
-
- if (macro_table.find(name) == macro_table.end())
- macro_table.insert(make_pair(name, new MathMacroTemplate(name, na)));
-
- return *(macro_table.find(name)->second);
+ 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);
}
-MathMacroTemplate &
-MathMacroTable::provideTemplate(string const & name)
+// The global table.
+MacroTable & MacroTable::globalMacros()
{
- if (!built)
- builtinMacros();
-
- return *macro_table[name];
+ static MacroTable theGlobalMacros;
+ return theGlobalMacros;
}
-bool MathMacroTable::hasTemplate(string const & name)
-{
- if (!built)
- builtinMacros();
-
- return macro_table.find(name) != macro_table.end();
-}
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+// static MacroTable theLocalMacros;
+// return theLocalMacros;
+//}
-MathMacro * MathMacroTable::cloneTemplate(string const & name)
+bool MacroTable::has(string const & name) const
{
- return new MathMacro(provideTemplate(name));
+ return find(name) != end();
}
-void MathMacroTable::builtinMacros()
+MacroData const & MacroTable::get(string const & name) const
{
- built = true;
-
- lyxerr[Debug::MATHED] << "Building macros" << endl;
-
- // This macro doesn't have arguments
- {
- MathMacroTemplate & m = provideTemplate("notin", 0);
- MathedIter iter(&m.GetData());
- iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not),
- LM_TC_INSET);
- }
-
- // These two are only while we are still with LyX 2.x
- {
- MathMacroTemplate & m = provideTemplate("emptyset", 0);
- MathedIter iter(&m.GetData());
- iter.insertInset(new MathAccentInset('0', LM_TC_RM, LM_not),
- LM_TC_INSET);
- }
-
- {
- MathMacroTemplate & m = provideTemplate("perp", 0);
- MathedIter iter(&m.GetData());
- iter.insert(LM_bot, LM_TC_BOP);
- }
-
- // binom has two arguments
- {
- MathMacroTemplate & m = provideTemplate("binom", 2);
- MathedIter iter(&m.GetData());
-
- MathParInset * inset = new MathDelimInset('(', ')');
- iter.insertInset(inset, LM_TC_ACTIVE_INSET);
+ const_iterator it = find(name);
+ BOOST_ASSERT(it != end());
+ return it->second;
+}
- MathedArray array2;
- MathedIter iter2(&array2);
- MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
- iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
- frac->setData(array2);
- MathedArray array3;
- MathedIter iter3(&array3);
- iter3.insertInset(new MathMacroArgument(1), LM_TC_INSET);
+void MacroTable::insert(string const & name, MacroData const & data)
+{
+ //lyxerr << "MacroTable::insert: " << name << endl;
+ operator[](name) = data;
+}
- MathedArray array4;
- MathedIter iter4(&array4);
- iter4.insertInset(new MathMacroArgument(2), LM_TC_INSET);
- frac->SetData(array3, array4);
- }
+void MacroTable::insert(string const & def)
+{
+ //lyxerr << "MacroTable::insert, def: " << def << endl;
+ istringstream is(def);
+ MathMacroTemplate mac(is);
+ insert(mac.name(), mac.asMacroData());
+}
-/*
- {
- boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("perp", 0));
- addTemplate(m);
- MathedArray array;
- MathedIter iter(&array);
- iter.insert(LM_bot, LM_TC_BOP);
- m->setData(array);
- }
- // binom has two arguments
- {
- boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("binom", 2));
- addTemplate(m);
- MathedArray array;
- m->setData(array);
- MathedIter iter(&array);
- MathParInset * inset = new MathDelimInset('(', ')');
- iter.insertInset(inset, LM_TC_ACTIVE_INSET);
- array = MathedArray();
- MathedIter iter2(&array);
- MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
- iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
- inset->setData(array);
- array = MathedArray();
- MathedArray array2;
- MathedIter iter3(&array);
- iter3.insertInset(m->getMacroPar(0), LM_TC_INSET);
- MathedIter iter4(&array2);
- iter4.insertInset(m->getMacroPar(1), LM_TC_INSET);
- frac->SetData(array, array2);
- }
-*/
+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;
}