]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macrotable.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_macrotable.C
index 596b19f2946e69ed3939428cd26cb6006689a460..f7bb45efcb5c3aa099dfdb255f10dbfde3dd1364 100644 (file)
-#include <config.h>
-
-#include <iostream>
+/**
+ * \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.
+ */
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
 
 #include "math_macrotable.h"
-#include "math_macro.h"
 #include "math_macrotemplate.h"
-#include "math_parser.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;
 
-MathArray mathed_parse_cell(string const &);
 
+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------------------------------------------\n";
-       table_type::const_iterator it;
-       for (it = macro_table.begin(); it != macro_table.end(); ++it)
-               lyxerr << it->first << " [" << it->second.nargs() << "] : "
-                       << it->second << "\n";
-       lyxerr << "------------------------------------------\n";
+       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);
 }
 
 
-void MathMacroTable::insertTemplate(MathMacroTemplate const & p)
+// The global table.
+MacroTable & MacroTable::globalMacros()
 {
-       if (macro_table.find(p.name()) != macro_table.end()) 
-               lyxerr << "macro '" << p.name() << "' not new\n";
-       macro_table[p.name()] = p;
+       static MacroTable theGlobalMacros;
+       return theGlobalMacros;
 }
 
 
-MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
-{
-       builtinMacros();
-       
-       table_type::iterator pos = macro_table.find(name);
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+//     static MacroTable theLocalMacros;
+//     return theLocalMacros;
+//}
 
-       if (pos == macro_table.end()) {
-               lyxerr << "MathMacroTable::provideTemplate: no template with name '"
-                      << name << "' available.\n";
-       }
-               
-       return pos->second;
+
+bool MacroTable::has(string const & name) const
+{
+       return find(name) != end();
 }
 
 
-void MathMacroTable::createTemplate
-       (string const & name, int na, string const & text)
+MacroData const & MacroTable::get(string const & name) const
 {
-       MathMacroTemplate t(name, na);
-       t.cell(0) = mathed_parse_cell(text);
-       insertTemplate(t);
+       const_iterator it = find(name);
+       BOOST_ASSERT(it != end());
+       return it->second;
 }
 
 
-bool MathMacroTable::hasTemplate(string const & name)
+void MacroTable::insert(string const & name, MacroData const & data)
 {
-       builtinMacros();
-       return macro_table.find(name) != macro_table.end();
+       //lyxerr << "MacroTable::insert: " << name << endl;
+       operator[](name) = data;
 }
 
 
-MathMacro * MathMacroTable::cloneTemplate(string const & name)
+void MacroTable::insert(string const & def)
 {
-       return new MathMacro(provideTemplate(name));
+       //lyxerr << "MacroTable::insert, def: " << def << endl;
+       istringstream is(def);
+       MathMacroTemplate mac(is);
+       insert(mac.name(), mac.asMacroData());
 }
 
 
-void MathMacroTable::builtinMacros()
+void MacroTable::dump()
 {
-       static bool built = false;
-       
-       if (built)
-               return; 
-
-       built = true;
-       //lyxerr[Debug::MATHED] << "Building macros\n";
-   
-       createTemplate("emptyset",     0, "\\not0");
-       createTemplate("ne",           0, "\\not=");
-       createTemplate("ge",           0, "\\geq");
-       createTemplate("gets",         0, "\\leftarrow");
-       createTemplate("land",         0, "\\wedge");
-       createTemplate("le",           0, "\\leq");
-       createTemplate("lor",          0, "\\vee");
-       createTemplate("notin",        0, "\\not\\in");
-       createTemplate("perp",         0, "\\bot");
-       createTemplate("owns",         0, "\\ni");
-       createTemplate("to",           0, "\\rightarrow");
-#ifdef WITH_WARNINGS
-#warning 9em looks like too much but it is somehow working on screen..
-#endif
-       createTemplate("ll",           0, "<\\kern-9em<");
-       createTemplate("gg",           0, ">\\kern-9em>");
-       //createTemplate("lint",       4, "\\int_#1^#2#3 d#4");
-       //createTemplate("silentmult", 0, "\\cdot");
-       //createTemplate("binom",        2, "\\left(\\frac#1#2\\right)");
+       lyxerr << "\n------------------------------------------" << endl;
+       for (const_iterator it = begin(); it != end(); ++it)
+               lyxerr << it->first
+                       << " [" << it->second.def() << "] : "
+                       << " [" << it->second.disp() << "] : "
+                       << endl;
+       lyxerr << "------------------------------------------" << endl;
 }