]> 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 89065350ab1f7d185db7c497feb8698d012cfe00..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 "support.h" // math_font_available
+#include "dociterator.h"
 
+#include <boost/assert.hpp>
 
-MathMacroTable::table_type MathMacroTable::macro_table;
+#include <sstream>
 
+using std::endl;
+using std::istringstream;
+using std::map;
+using std::pair;
+using std::string;
+using std::vector;
+using std::size_t;
 
-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->asMacroTemplate()->nargs() << "] : "
-                       << it->second->cell(0) << "\n";
-       lyxerr << "------------------------------------------\n";
-}
 
+MacroData::MacroData()
+       : numargs_(0)
+{}
 
-MathAtom & MathMacroTable::provide(string const & name)
-{
-       builtinMacros();
-       
-       table_type::iterator pos = macro_table.find(name);
 
-       if (pos == macro_table.end()) {
-               lyxerr << "MathMacroTable::provideTemplate: no template with name '"
-                      << name << "' available.\n";
-       }
-               
-       return pos->second;
-}
+MacroData::MacroData(string const & def, int numargs, string const & disp)
+       : def_(def), numargs_(numargs), disp_(disp)
+{}
 
 
-void MathMacroTable::create(string const & name, int na, string const & text)
+void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
 {
-       MathAtom t(new MathMacroTemplate(name, na));
-       mathed_parse_cell(t->cell(0), text);
-       macro_table[name] = t;
+       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::create(string const & name, int na, MathArray const & ar)
+// The global table.
+MacroTable & MacroTable::globalMacros()
 {
-       MathAtom t(new MathMacroTemplate(name, na));
-       t->cell(0) = ar;
-       macro_table[name] = t;
+       static MacroTable theGlobalMacros;
+       return theGlobalMacros;
 }
 
 
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+//     static MacroTable theLocalMacros;
+//     return theLocalMacros;
+//}
+
 
-bool MathMacroTable::has(string const & name)
+bool MacroTable::has(string const & name) const
 {
-       builtinMacros();
-       return macro_table.find(name) != macro_table.end();
+       return find(name) != end();
 }
 
 
-void MathMacroTable::builtinMacros()
+MacroData const & MacroTable::get(string const & name) const
 {
-       static bool built = false;
-       
-       if (built)
-               return; 
-
-       built = true;
-       //lyxerr[Debug::MATHED] << "Building macros\n";
-       //create("emptyset",     0, "\\not0");
-       create("notin",        0, "\\not\\in");
-       create("slash",        0, "/");
-
-       // fontmath.ltx
-
-       create("lnot",         0, "\\neg");
-       create("land",         0, "\\wedge");
-       create("lor",          0, "\\vee");
-       create("ne",           0, "\\neq");
-       create("le",           0, "\\leq");
-       create("ge",           0, "\\geq");
-       create("owns",         0, "\\ni");
-       create("gets",         0, "\\leftarrow");
-       create("to",           0, "\\rightarrow");
-       create("|",            0, "\\Vert");
-
-       create("longleftrightarrow", 0, "\\leftarrow\\kern-6mu\\rightarrow");
-       create("Longleftrightarrow", 0, "\\Leftarrow\\kern-6mu\\Rightarrow");
-       create("doteq", 0, "\\stackrel{\\cdot}{=}");
-
-       //create("models",         0, "|\\kern-9mu=");
-
-       if (math_font_available(LM_TC_CMSY)) {
-               create("longrightarrow", 0, "\\lyxbar\\kern-5mu\\rightarrow");
-               create("longleftarrow",  0, "\\leftarrow\\kern-5mu\\lyxbar");
-               create("mapsto",  0, "\\mapstochar\\rightarrow");
-               create("longmapsto",  0, "\\mapstochar\\lyxbar\\kern-5mu\\rightarrow");
-       }
-
-       if (math_font_available(LM_TC_CMR)) {
-               create("Longrightarrow", 0, "\\lyxeq\\kern-3mu\\Rightarrow");
-               create("Longleftarrow",  0, "\\Leftarrow\\kern-9mu\\lyxeq");
-       }
+       const_iterator it = find(name);
+       BOOST_ASSERT(it != end());
+       return it->second;
+}
 
-       if (math_font_available(LM_TC_CMM)) {
-               create("hookrightarrow", 0, "\\lhook\\kern-5mu\\rightarrow");
-               create("hookleftarrow",  0, "\\leftarrow\\kern-5mu\\rhook");
-               create("bowtie",         0, "\\triangleright\\kern-3mu\\triangleleft");
-       }
 
-       if (math_font_available(LM_TC_MSA)) {
-               //amsfonts.sty
+void MacroTable::insert(string const & name, MacroData const & data)
+{
+       //lyxerr << "MacroTable::insert: " << name << endl;
+       operator[](name) = data;
+}
 
-               create("dashrightarrow", 0, "\\lyxdabar\\lyxdabar\\lyxright");
-               create("dashleftarrow", 0, "\\lyxleft\\lyxdabar\\lyxdabar");
-               create("dasharrow",    0, "\\dashrightarrow");
-               create("Box",          0, "\\square");
-               create("Diamond",      0, "\\lozenge");
-               create("leadsto",      0, "\\rightsquigarrow");
 
-               // amssymb.sty
+void MacroTable::insert(string const & def)
+{
+       //lyxerr << "MacroTable::insert, def: " << def << endl;
+       istringstream is(def);
+       MathMacroTemplate mac(is);
+       insert(mac.name(), mac.asMacroData());
+}
 
-               create("restriction",  0, "\\upharpoonright");
-               create("Doteq",        0, "\\doteqdot");
-               create("doublecup",    0, "\\Cup");
-               create("doublecap",    0, "\\Cap");
-               create("llless",       0, "\\lll");
-               create("gggtr",        0, "\\ggg");
-       }
 
-       //create("lint",       4, "\\int_#1^#2#3 d#4");
-       //create("silentmult", 0, "\\cdot");
-       //create("binom",        2, "\\left(\\frac#1#2\\right)");
+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;
 }