-#include <config.h>
-
-#include <iostream>
+/**
+ * \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_parser.h"
-#include "math_mathmlstream.h"
+#include "math_macroarg.h"
+#include "math_support.h"
+#include "math_sqrtinset.h"
+
#include "debug.h"
-#include "math_support.h" // math_font_available
+#include "dociterator.h"
+#include <boost/assert.hpp>
-MathMacroTable::table_type MathMacroTable::macro_table;
+#include <sstream>
+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()
-{
-/*
- lyxerr << "\n------------------------------------------\n";
- 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) << "\n";
- lyxerr << "------------------------------------------\n";
-*/
-}
+MacroData::MacroData()
+ : numargs_(0)
+{}
-MathAtom & MathMacroTable::provide(string const & name)
-{
- builtinMacros();
- table_type::iterator pos = macro_table.find(name);
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+ : def_(def), numargs_(numargs), disp_(disp)
+{}
- if (pos == macro_table.end()) {
- lyxerr << "MathMacroTable::provideTemplate: no template with name '"
- << name << "' available.\n";
+
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
+{
+ 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);
+}
+
- return pos->second;
+// The global table.
+MacroTable & MacroTable::globalMacros()
+{
+ static MacroTable theGlobalMacros;
+ return theGlobalMacros;
}
-void MathMacroTable::create(string const & name, int na)
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+// static MacroTable theLocalMacros;
+// return theLocalMacros;
+//}
+
+
+bool MacroTable::has(string const & name) const
{
- macro_table[name] = MathAtom(new MathMacroTemplate(name, na));
+ return find(name) != end();
}
-void MathMacroTable::create
- (string const & name, int na, MathArray const & ar1, MathArray const & ar2)
+MacroData const & MacroTable::get(string const & name) const
{
- MathAtom t(new MathMacroTemplate(name, na));
- t->cell(0) = ar1;
- t->cell(1) = ar2;
- macro_table[name] = t;
+ const_iterator it = find(name);
+ BOOST_ASSERT(it != end());
+ return it->second;
}
-void MathMacroTable::define(string const & display)
+void MacroTable::insert(string const & name, MacroData const & data)
{
- string name;
- mathed_parse_macro(name, display);
+ //lyxerr << "MacroTable::insert: " << name << endl;
+ operator[](name) = data;
}
-bool MathMacroTable::has(string const & name)
+void MacroTable::insert(string const & def)
{
- builtinMacros();
- 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 MathMacroTable::builtinMacros()
+void MacroTable::dump()
{
- static bool built = false;
-
- if (built)
- return;
-
- built = true;
- //lyxerr[Debug::MATHED] << "Building macros\n";
- //define("\\def\\emptyset{\\not0}");
- define("\\def\\notin{\\not\\in}");
- define("\\def\\slash{/}");
- //define("\\def\\mathcircumflex{\\^}");
-
- // fontmath.ltx
-
- define("\\def\\lnot{\\neg}");
- define("\\def\\land{\\wedge}");
- define("\\def\\lor{\\vee}");
- define("\\def\\ne{\\neq}");
- define("\\def\\le{\\leq}");
- define("\\def\\ge{\\geq}");
- define("\\def\\owns{\\ni}");
- define("\\def\\gets{\\leftarrow}");
- define("\\def\\to{\\rightarrow}");
- define("\\def\\|{\\Vert}");
-
- define("\\def\\longleftrightarrow{\\leftarrow\\kern-8mu\\rightarrow}");
- define("\\def\\Longleftrightarrow{\\Leftarrow\\kern-8mu\\Rightarrow}");
- define("\\def\\doteq{\\stackrel{\\cdot}{\\=}}");
-
- //if (math_font_available(LM_TC_CMSY)) {
- define("\\def\\longrightarrow{\\lyxbar\\kern-6mu\\rightarrow}");
- define("\\def\\longleftarrow{\\leftarrow\\kern-6mu\\lyxbar}");
- define("\\def\\mapsto{\\mapstochar\\kern-4mu\\rightarrow}");
- define("\\def\\longmapsto{\\mapstochar\\kern-3mu\\lyxbar\\kern-6mu\\rightarrow}");
- //}
-
- //if (math_font_available(LM_TC_CMR) && math_font_available(LM_TC_CMSY)) {
- define("\\def\\Longrightarrow{\\lyxeq\\kern-5mu\\Rightarrow}");
- define("\\def\\Longleftarrow{\\Leftarrow\\kern-5mu\\lyxeq}");
- define("\\def\\models{\\vert\\kern-3mu\\lyxeq}");
- //}
-
- //if (math_font_available(LM_TC_CMM)) {
- define("\\def\\hookrightarrow{\\lhook\\kern-8mu\\rightarrow}");
- define("\\def\\hookleftarrow{\\leftarrow\\kern-8mu\\rhook}");
- define("\\def\\bowtie{\\triangleright\\kern-2mu\\triangleleft}");
- //}
-
- //if (math_font_available(LM_TC_MSA)) {
- //amsfonts.sty
-
- define("\\def\\dashrightarrow{\\lyxdabar\\lyxdabar\\lyxright}");
- define("\\def\\dashleftarrow{\\lyxleft\\lyxdabar\\lyxdabar}");
- define("\\def\\dasharrow{\\dashrightarrow}");
- define("\\def\\Box{\\square}");
- define("\\def\\Diamond{\\lozenge}");
- define("\\def\\leadsto{\\rightsquigarrow}");
-
- // amssymb.sty
-
- define("\\def\\restriction{\\upharpoonright}");
- define("\\def\\Doteq{\\doteqdot}");
- define("\\def\\doublecup{\\Cup}");
- define("\\def\\doublecap{\\Cap}");
- define("\\def\\llless{\\lll}");
- define("\\def\\gggtr{\\ggg}");
- //}
-
- //if (math_font_available(LM_TC_MSB)) {
- define("\\def\\Join{\\ltimes\\kern-12mu\\rtimes}");
- //}
-
- //
- define("\\def\\mathcircumflex{\\mbox{\\^{}}}\n" "{\\hat{}}");
-
- //define("\def\lint", 4, "\\int_#1^#2#3 d#4}");
- //define("\\def\\silentmult{\\cdot}");
- //define("\def\binom", 2, "\\left(\\frac#1#2\\right)}");
+ lyxerr << "\n------------------------------------------" << endl;
+ for (const_iterator it = begin(); it != end(); ++it)
+ lyxerr << it->first
+ << " [" << it->second.def() << "] : "
+ << " [" << it->second.disp() << "] : "
+ << endl;
+ lyxerr << "------------------------------------------" << endl;
}