#include <config.h>
+#include <iostream>
+
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
#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_parser.h"
+#include "math_mathmlstream.h"
#include "debug.h"
+#include "math_support.h" // math_font_available
-using std::endl;
-MathMacroTable MathMacroTable::mathMTable;
+MathMacroTable::table_type MathMacroTable::macro_table;
-bool MathMacroTable::built = false;
-
-MathMacro * MathMacroTable::getMacro(string const & name) const
+void MathMacroTable::dump()
{
- MathMacroTemplate * mt = getTemplate(name);
- return (mt) ? new MathMacro(mt): 0;
+/*
+ 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";
+*/
}
-// The search is currently linear but will be binary or hash, later.
-MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
+MathAtom & MathMacroTable::provide(string const & name)
{
- for (size_type i = 0; i < macro_table.size(); ++i) {
- if (name == macro_table[i]->GetName())
- return macro_table[i];
+ builtinMacros();
+
+ table_type::iterator pos = macro_table.find(name);
+
+ if (pos == macro_table.end()) {
+ lyxerr << "MathMacroTable::provideTemplate: no template with name '"
+ << name << "' available.\n";
}
+
+ return pos->second;
+}
+
- return 0;
+void MathMacroTable::create(string const & name, int na, string const & text)
+{
+ MathAtom t(new MathMacroTemplate(name, na));
+ mathed_parse_cell(t->cell(0), text);
+ macro_table[name] = t;
}
-void MathMacroTable::addTemplate(MathMacroTemplate * m)
+
+
+void MathMacroTable::create(string const & name, int na, MathArray const & ar)
{
- macro_table.push_back(m);
+ MathAtom t(new MathMacroTemplate(name, na));
+ t->cell(0) = ar;
+ macro_table[name] = t;
}
-// All this stuff aparently leaks because it's created here and is not
-// deleted never, but it have to live all the LyX sesion. OK, would not
-// so hard to do it in the MacroTable destructor, but this doesn't harm
-// seriously, so don't bother me with purify results here. ;-)
+
+bool MathMacroTable::has(string const & name)
+{
+ builtinMacros();
+ return macro_table.find(name) != macro_table.end();
+}
+
void MathMacroTable::builtinMacros()
{
- MathedIter iter;
- MathParInset * inset;// *arg;
- MathedArray * array2;
-
+ static bool built = false;
+
+ if (built)
+ return;
+
built = true;
-
- lyxerr[Debug::MATHED] << "Building macros" << endl;
-
- // This macro doesn't have arguments
- MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
- addTemplate(m);
- MathedArray * array = new MathedArray; // this leaks
- iter.SetData(array);
- iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not),
- LM_TC_INSET); // this leaks
- m->setData(array);
-
- // These two are only while we are still with LyX 2.x
- m = new MathMacroTemplate("emptyset"); // this leaks
- addTemplate(m);
- array = new MathedArray; // this leaks
- iter.SetData(array);
- iter.insertInset(new MathAccentInset('O', LM_TC_RM, LM_not),
- LM_TC_INSET); // this leaks
- m->setData(array);
-
- m = new MathMacroTemplate("perp"); // this leaks
- addTemplate(m);
- array = new MathedArray; // this leaks
- iter.SetData(array);
- iter.insert(LM_bot, LM_TC_BOP);
- m->setData(array);
-
- // binom has two arguments
- m = new MathMacroTemplate("binom", 2);
- addTemplate(m);
- array = new MathedArray;
- m->setData(array);
- iter.SetData(array);
- inset = new MathDelimInset('(', ')');
- iter.insertInset(inset, LM_TC_ACTIVE_INSET);
- array = new MathedArray;
- iter.SetData(array);
- MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
- iter.insertInset(frac, LM_TC_ACTIVE_INSET);
- inset->setData(array);
- array = new MathedArray;
- array2 = new MathedArray;
- iter.SetData(array);
- iter.insertInset(m->getMacroPar(0), LM_TC_INSET);
- iter.SetData(array2);
- iter.insertInset(m->getMacroPar(1), LM_TC_INSET);
- frac->SetData(array, array2);
+ //lyxerr[Debug::MATHED] << "Building macros\n";
+ //create("emptyset", 0, "\\not0");
+ create("notin", 0, "\\not\\in");
+ create("slash", 0, "/");
+
+ // fontmath.ltx
+
+ create("lnot", 0, "\\neg");
+ create("land", 0, "\\wedge");
+ create("lor", 0, "\\vee");
+ create("ne", 0, "\\neq");
+ create("le", 0, "\\leq");
+ create("ge", 0, "\\geq");
+ create("owns", 0, "\\ni");
+ create("gets", 0, "\\leftarrow");
+ create("to", 0, "\\rightarrow");
+ create("|", 0, "\\Vert");
+
+ create("longleftrightarrow", 0, "\\leftarrow\\kern-8mu\\rightarrow");
+ create("Longleftrightarrow", 0, "\\Leftarrow\\kern-8mu\\Rightarrow");
+ create("doteq", 0, "\\stackrel{\\cdot}{=}");
+
+ //create("models", 0, "|\\kern-9mu=");
+
+ if (math_font_available(LM_TC_CMSY)) {
+ create("longrightarrow", 0, "\\lyxbar\\kern-6mu\\rightarrow");
+ create("longleftarrow", 0, "\\leftarrow\\kern-6mu\\lyxbar");
+ create("mapsto", 0, "\\mapstochar\\kern-4mu\\rightarrow");
+ create("longmapsto", 0, "\\mapstochar\\kern-3mu\\lyxbar\\kern-6mu\\rightarrow");
+ }
-/*
- // Cases has 1 argument
- m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
- addTemplate(m);
- array = new MathedArray; // this leaks
- iter.SetData(array);
- arg = new MathMatrixInset(2, 1); // this leaks
-
- m->setArgument(arg);
- arg->SetAlign('c', "ll");
- iter.Insert(arg, LM_TC_ACTIVE_INSET);
- inset = new MathDelimInset('{', '.'); // this leaks
- inset->SetData(array);
- array = new MathedArray; // this leaks
- iter.SetData(array);
- iter.Insert(inset, LM_TC_ACTIVE_INSET);
- m->SetData(array);
-
-
- // the environment substack has 1 argument
- m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
- addTemplate(m);
- arg = new MathMatrixInset(1, 1); // this leaks
- m->setArgument(arg);
- arg->SetType(LM_OT_MACRO);
- array = new MathedArray; // this leaks
- iter.SetData(array);
- iter.Insert(arg, LM_TC_ACTIVE_INSET);
- m->SetData(array);*/
+ if (math_font_available(LM_TC_CMR)) {
+ create("Longrightarrow", 0, "\\lyxeq\\kern-5mu\\Rightarrow");
+ create("Longleftarrow", 0, "\\Leftarrow\\kern-5mu\\lyxeq");
+ }
+
+ if (math_font_available(LM_TC_CMM)) {
+ create("hookrightarrow", 0, "\\lhook\\kern-8mu\\rightarrow");
+ create("hookleftarrow", 0, "\\leftarrow\\kern-8mu\\rhook");
+ create("bowtie", 0, "\\triangleright\\kern-2mu\\triangleleft");
+ }
+
+ if (math_font_available(LM_TC_MSA)) {
+ //amsfonts.sty
+
+ create("dashrightarrow", 0, "\\lyxdabar\\lyxdabar\\lyxright");
+ create("dashleftarrow", 0, "\\lyxleft\\lyxdabar\\lyxdabar");
+ create("dasharrow", 0, "\\dashrightarrow");
+ create("Box", 0, "\\square");
+ create("Diamond", 0, "\\lozenge");
+ create("leadsto", 0, "\\rightsquigarrow");
+
+ // amssymb.sty
+
+ create("restriction", 0, "\\upharpoonright");
+ create("Doteq", 0, "\\doteqdot");
+ create("doublecup", 0, "\\Cup");
+ create("doublecap", 0, "\\Cap");
+ create("llless", 0, "\\lll");
+ create("gggtr", 0, "\\ggg");
+ }
+
+ //create("lint", 4, "\\int_#1^#2#3 d#4");
+ //create("silentmult", 0, "\\cdot");
+ //create("binom", 2, "\\left(\\frac#1#2\\right)");
}