]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macrotable.C
Fix to bug 2362: Deleting superscript also deletes subscript.
[lyx.git] / src / mathed / math_macrotable.C
index 24be222fe35e9520402594618338d69c7f5785be..f7bb45efcb5c3aa099dfdb255f10dbfde3dd1364 100644 (file)
+/**
+ * \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.
+ */
+
 #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 "dociterator.h"
+
+#include <boost/assert.hpp>
+
+#include <sstream>
 
 using std::endl;
+using std::istringstream;
+using std::map;
+using std::pair;
+using std::string;
+using std::vector;
+using std::size_t;
+
+
+MacroData::MacroData()
+       : numargs_(0)
+{}
 
-MathMacroTable MathMacroTable::mathMTable;
 
-bool MathMacroTable::built = false;
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+       : def_(def), numargs_(numargs), disp_(disp)
+{}
 
 
-MathMacro * MathMacroTable::getMacro(string const & name) const
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
 {
-       MathMacroTemplate * mt = getTemplate(name);
-       return (mt) ? new MathMacro(mt): 0;
+       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);
 }
 
 
-// The search is currently linear but will be binary or hash, later.
-MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
+// The global table.
+MacroTable & MacroTable::globalMacros()
 {
-       for (size_type i = 0; i < macro_table.size(); ++i) {
-               if (name == macro_table[i]->GetName()) 
-                       return macro_table[i];
-       }
+       static MacroTable theGlobalMacros;
+       return theGlobalMacros;
+}
+
+
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+//     static MacroTable theLocalMacros;
+//     return theLocalMacros;
+//}
+
 
-       return 0;
+bool MacroTable::has(string const & name) const
+{
+       return find(name) != end();
 }
 
-void MathMacroTable::addTemplate(MathMacroTemplate * m)
+
+MacroData const & MacroTable::get(string const & name) const
 {
-       macro_table.push_back(m);
+       const_iterator it = find(name);
+       BOOST_ASSERT(it != end());
+       return it->second;
 }
 
 
-// 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.   ;-)
+void MacroTable::insert(string const & name, MacroData const & data)
+{
+       //lyxerr << "MacroTable::insert: " << name << endl;
+       operator[](name) = data;
+}
+
 
-void MathMacroTable::builtinMacros()
+void MacroTable::insert(string const & def)
 {
-       MathParInset * inset;// *arg;
-    
-       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;
-               MathedIter iter(&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); 
-       {
-               MathedArray array;
-               MathedIter iter(&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);
-       {
-               MathedArray array;
-               MathedIter iter(&array);
-               iter.insert(LM_bot, LM_TC_BOP);
-               m->setData(array);
-       }
+       //lyxerr << "MacroTable::insert, def: " << def << endl;
+       istringstream is(def);
+       MathMacroTemplate mac(is);
+       insert(mac.name(), mac.asMacroData());
+}
 
-       // binom has two arguments
-       m = new MathMacroTemplate("binom", 2);
-       addTemplate(m);
-       {
-               MathedArray array;
-               m->setData(array);
-               MathedIter iter(&array);
-               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);
-       }
 
-/*
-  // 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);*/
+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;
 }