]> 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 a671319cc45940d4d050d356a4a2a8d13d743855..f7bb45efcb5c3aa099dfdb255f10dbfde3dd1364 100644 (file)
 
 #include "math_macrotable.h"
 #include "math_macrotemplate.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::string;
 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::table_type MathMacroTable::macro_table;
 
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+       : def_(def), numargs_(numargs), disp_(disp)
+{}
 
-void MathMacroTable::dump()
+
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
 {
-/*
-       lyxerr << "\n------------------------------------------" << endl;
-       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) << endl;
-       lyxerr << "------------------------------------------" << endl;
-*/
+       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);
 }
 
 
-MathAtom & MathMacroTable::provide(string const & name)
+// The global table.
+MacroTable & MacroTable::globalMacros()
 {
-       table_type::iterator pos = macro_table.find(name);
-       if (pos == macro_table.end()) {
-               lyxerr << "MathMacroTable::provideTemplate: no template with name '"
-                      << name << "' available." << endl;
-               BOOST_ASSERT(false);
-       }
-       return pos->second;
+       static MacroTable theGlobalMacros;
+       return theGlobalMacros;
 }
 
 
-void MathMacroTable::create(MathAtom const & at)
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+//     static MacroTable theLocalMacros;
+//     return theLocalMacros;
+//}
+
+
+bool MacroTable::has(string const & name) const
 {
-       //lyxerr << "MathMacroTable::create: '"
-       //      << at->asMacroTemplate()->name() << "'" << endl;
-       macro_table[at->asMacroTemplate()->name()] = at;
+       return find(name) != end();
 }
 
 
-bool MathMacroTable::has(string const & name)
+MacroData const & MacroTable::get(string const & name) const
 {
-       return macro_table.find(name) != macro_table.end();
+       const_iterator it = find(name);
+       BOOST_ASSERT(it != end());
+       return it->second;
+}
+
+
+void MacroTable::insert(string const & name, MacroData const & data)
+{
+       //lyxerr << "MacroTable::insert: " << name << endl;
+       operator[](name) = data;
+}
+
+
+void MacroTable::insert(string const & def)
+{
+       //lyxerr << "MacroTable::insert, def: " << def << endl;
+       istringstream is(def);
+       MathMacroTemplate mac(is);
+       insert(mac.name(), mac.asMacroData());
+}
+
+
+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;
 }