]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macrotable.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_macrotable.C
index d8216b9e73c338d31a5f6c18c6d1e633dba73236..978766ab5be514833619ea5047d77fa827b3c17d 100644 (file)
 #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 "math_parser.h"
 #include "array.h"
-#include "math_accentinset.h"
+#include "math_decorationinset.h"
 #include "math_deliminset.h"
 #include "math_fracinset.h"
-#include "math_parinset.h"
+#include "math_inset.h"
 #include "debug.h"
+#include "support/LAssert.h"
 
 using std::endl;
 
-MathMacroTable MathMacroTable::mathMTable;
+MathMacroTable::table_type MathMacroTable::macro_table;
+
+
+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->nargs() << "] : "
+                       << it->second << endl;
+       lyxerr << "------------------------------------------" << endl;;
+}
+
+
+void MathMacroTable::updateTemplate(MathMacroTemplate * par)
+{
+       table_type::iterator pos = macro_table.find(par->name());
 
-bool MathMacroTable::built = false;
+       if (pos == macro_table.end())
+               lyxerr << "MathMacroTable::updateTemplate: no template with name '"
+                      << par->name() << "' available." << endl;
+       else
+               pos->second = par;
+}
 
 
-MathMacro * MathMacroTable::getMacro(string const & name) const
+void MathMacroTable::insertTemplate(MathMacroTemplate * p)
 {
-       MathMacroTemplate * mt = getTemplate(name);
-       return (mt) ? new MathMacro(mt): 0;
+       macro_table[p->name()] = p;
 }
 
 
-// The search is currently linear but will be binary or hash, later.
-MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
+MathMacroTemplate & MathMacroTable::provideTemplate(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);
 
-       return 0;
+       if (pos == macro_table.end()) {
+               lyxerr << "MathMacroTable::provideTemplate: no template with name '"
+                      << name << "' available." << endl;
+       }
+               
+       return *pos->second;
 }
 
-void MathMacroTable::addTemplate(MathMacroTemplate * m)
+
+bool MathMacroTable::hasTemplate(string const & name)
 {
-       macro_table.push_back(m);
+       builtinMacros();
+       return macro_table.find(name) != macro_table.end();
 }
 
 
-// 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.   ;-)
+MathMacro * MathMacroTable::cloneTemplate(string const & name)
+{
+       return new MathMacro(provideTemplate(name));
+}
+
 
 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.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // 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.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // 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);
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
+               MathDecorationInset * p = new MathDecorationInset("not", LM_not);
+               p->cell(0).push_back(LM_in, LM_TC_BOPS);
+               t->push_back(p);
+               insertTemplate(t);
+       }
+
+/*
+       // This macro doesn't have arguments
+       {
+               MathMacroTemplate & m = createTemplate("silentmult", 0);
+               istringstream is("\\cdot\0");
+               mathed_parser_file(is, 0);
+               MathMatrixInset * p = &m;
+       mathed_parse(m.array, p, 0);
+       }
+*/
+
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0);
+               MathDecorationInset * p = new MathDecorationInset("not", LM_not);
+               p->cell(0).push_back('O', LM_TC_VAR);
+               t->push_back(p);
+               insertTemplate(t);
+       }
+
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("land", 0);
+               t->push_back(LM_wedge, LM_TC_SYMB);
+               insertTemplate(t);
+       }
+
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("lor", 0);
+               t->push_back(LM_vee, LM_TC_SYMB);
+               insertTemplate(t);
+       }
+
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("to", 0);
+               t->push_back(LM_rightarrow, LM_TC_SYMB);
+               insertTemplate(t);
+       }
+
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("perp", 0);
+               t->push_back(LM_bot, LM_TC_BOP);
+               insertTemplate(t);
+       }
+
+/*
+       {
+               MathMacroTemplate & m = createTemplate("lint", 4);
+               istringstream is("\\int_{#1}^{#2}#3 d#4\0");
+               mathed_parser_file(is, 0);
+               MathMatrixInset * p = &m;
+       mathed_parse(m.array, p, 0);
+       }
+*/
+/*
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("binomii", 2);
+               istringstream is("\\left(\\frac{#1}{#2}\\right)\0");
+               mathed_parser_file(is, 0);
+       mathed_parse(t->array, t, 0);
+               insertTemplate(t);
+       }
+*/
 
        // binom has two arguments
-       m = new MathMacroTemplate("binom", 2);
-       addTemplate(m);
-       array = new MathedArray; 
-       m->setData(array);
-       iter.SetData(array);
-       inset = new MathDelimInset('(', ')');
-       iter.Insert(inset, LM_TC_ACTIVE_INSET);
-       array = new MathedArray; 
-       iter.SetData(array);
-       MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
-       iter.Insert(frac, LM_TC_ACTIVE_INSET);
-       inset->setData(array);
-       array = new MathedArray;
-       array2 = new MathedArray;  
-       iter.SetData(array);
-       iter.Insert(m->getMacroPar(0));
-       iter.SetData(array2);
-       iter.Insert(m->getMacroPar(1));
-       frac->SetData(array, array2);
+       {
+               MathFracInset * frac = new MathFracInset("atop");
+               frac->cell(0).push_back(new MathMacroArgument(1));
+               frac->cell(1).push_back(new MathMacroArgument(2));
+
+               MathInset * inset = new MathDelimInset('(', ')');
+               inset->push_back(frac);
+
+               MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
+               t->push_back(inset);
+
+               insertTemplate(t);
+       }
 
 /*
-  // 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);*/
+       {
+               MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
+               frac->cell(0)->push_back(new MathMacroArgument(1));
+               frac->cell(1)->push_back(new MathMacroArgument(2));
+
+               MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
+               t->push_back(frac);
+
+               insertTemplate(t);
+       }
+*/
 }