]> 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 c5ab680d3933c6f5b0cec651ee71e820f8a7ff88..978766ab5be514833619ea5047d77fa827b3c17d 100644 (file)
@@ -1,5 +1,7 @@
 #include <config.h>
 
+#include <iostream>
+
 #ifdef __GNUG__
 #pragma implementation
 #endif
@@ -7,12 +9,12 @@
 #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"
 
@@ -20,47 +22,54 @@ using std::endl;
 
 MathMacroTable::table_type MathMacroTable::macro_table;
 
-bool MathMacroTable::built = false;
-
 
 void MathMacroTable::dump()
 {
-       cerr << "\n------------------------------------------\n";
+       lyxerr << "\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";
+               lyxerr << it->first << " [" << it->second->nargs() << "] : "
+                       << it->second << endl;
+       lyxerr << "------------------------------------------" << endl;;
 }
 
 
-MathMacroTemplate &
-MathMacroTable::provideTemplate(string const & name, int na)
+void MathMacroTable::updateTemplate(MathMacroTemplate * par)
 {
-       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);
+       table_type::iterator pos = macro_table.find(par->name());
+
+       if (pos == macro_table.end())
+               lyxerr << "MathMacroTable::updateTemplate: no template with name '"
+                      << par->name() << "' available." << endl;
+       else
+               pos->second = par;
 }
 
 
-MathMacroTemplate &
-MathMacroTable::provideTemplate(string const & name)
+void MathMacroTable::insertTemplate(MathMacroTemplate * p)
 {
-       if (!built)
-               builtinMacros();
+       macro_table[p->name()] = p;
+}
+
+
+MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
+{
+       builtinMacros();
        
-       return *macro_table[name];
+       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;
 }
 
 
 bool MathMacroTable::hasTemplate(string const & name)
 {
-       if (!built)
-               builtinMacros();
-       
+       builtinMacros();
        return macro_table.find(name) != macro_table.end();
 }
 
@@ -73,88 +82,111 @@ MathMacro * MathMacroTable::cloneTemplate(string const & name)
 
 void MathMacroTable::builtinMacros()
 {
+       static bool built = false;
+       
+       if (built)
+               return; 
+
        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);
+               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);
        }
 
-       // These two are only while we are still with LyX 2.x
+/*
+       // This macro doesn't have arguments
        {
-               MathMacroTemplate & m = provideTemplate("emptyset", 0);
-               MathedIter iter(&m.GetData());
-               iter.insertInset(new MathAccentInset('0', LM_TC_RM, LM_not),
-                                LM_TC_INSET);
+               MathMacroTemplate & m = createTemplate("silentmult", 0);
+               istringstream is("\\cdot\0");
+               mathed_parser_file(is, 0);
+               MathMatrixInset * p = &m;
+       mathed_parse(m.array, p, 0);
        }
+*/
 
        {
-               MathMacroTemplate & m = provideTemplate("perp", 0);
-               MathedIter iter(&m.GetData());
-               iter.insert(LM_bot, LM_TC_BOP);
+               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);
        }
 
-       // binom has two arguments
        {
-               MathMacroTemplate & m = provideTemplate("binom", 2);
-               MathedIter iter(&m.GetData());
-
-               MathParInset * inset = new MathDelimInset('(', ')');
-               iter.insertInset(inset, LM_TC_ACTIVE_INSET);
-
-               MathedArray array2;
-               MathedIter iter2(&array2);
-               MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
-               iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
-               frac->setData(array2);
+               MathMacroTemplate * t = new MathMacroTemplate("land", 0);
+               t->push_back(LM_wedge, LM_TC_SYMB);
+               insertTemplate(t);
+       }
 
-               MathedArray array3;
-               MathedIter iter3(&array3);
-               iter3.insertInset(new MathMacroArgument(1), LM_TC_INSET);
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("lor", 0);
+               t->push_back(LM_vee, LM_TC_SYMB);
+               insertTemplate(t);
+       }
 
-               MathedArray array4;
-               MathedIter iter4(&array4);
-               iter4.insertInset(new MathMacroArgument(2), LM_TC_INSET);
+       {
+               MathMacroTemplate * t = new MathMacroTemplate("to", 0);
+               t->push_back(LM_rightarrow, LM_TC_SYMB);
+               insertTemplate(t);
+       }
 
-               frac->SetData(array3, array4);
+       {
+               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);
+       }
+*/
+/*
        {
-               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);
+               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
        {
-               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("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);
+       }
+
+/*
+       {
                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);
+               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);
        }
 */
 }