]> 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 c18456063f73c69f00536d36f27f7526d30c9f9f..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 "array.h"
-#include "math_decorationinset.h"
-#include "math_deliminset.h"
-#include "math_fracinset.h"
-#include "math_inset.h"
+#include "math_macroarg.h"
+#include "math_support.h"
+#include "math_sqrtinset.h"
+
 #include "debug.h"
-#include "support/LAssert.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 << 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);
 }
 
 
-void MathMacroTable::updateTemplate(MathMacroTemplate * par)
+// The global table.
+MacroTable & MacroTable::globalMacros()
 {
-       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;
+       static MacroTable theGlobalMacros;
+       return theGlobalMacros;
 }
 
 
-void MathMacroTable::insertTemplate(MathMacroTemplate * p)
-{
-       macro_table[p->name()] = p;
-}
+// The local table.
+//MacroTable & MacroTable::localMacros()
+//{
+//     static MacroTable theLocalMacros;
+//     return theLocalMacros;
+//}
 
 
-MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
+bool MacroTable::has(string const & name) const
 {
-       builtinMacros();
-       
-       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;
+       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 = new MathMacroTemplate(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" << endl;
-   
-/* 
-       // This macro doesn't have arguments
-       {
-               MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
-               MathDecorationInset * p = new MathDecorationInset("not");
-               p->cell(0).push_back(LM_in, LM_TC_SYMB);
-               t->push_back(p);
-               insertTemplate(t);
-       }
-*/
-
-       createTemplate("silentmult", 0, "\\cdot");
-       createTemplate("emptyset",   0, "\\not0");
-       createTemplate("notin",      0, "\\not\\in");
-
-
-
-/*
-       {
-               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);
-       }
-*/
-
-       {
-
-               MathMacroTemplate * t = new MathMacroTemplate("land", 0);
-               t->push_back(LM_wedge, LM_TC_SYMB);
-               insertTemplate(t);
-       }
-
-       {
-               MathMacroTemplate * t = new MathMacroTemplate("lor", 0);
-               t->push_back(LM_vee, LM_TC_SYMB);
-               insertTemplate(t);
-       }
-
-       {
-               MathMacroTemplate * t = new MathMacroTemplate("to", 0);
-               t->push_back(LM_rightarrow, LM_TC_SYMB);
-               insertTemplate(t);
-       }
-
-       {
-               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);
-       }
-*/
-/*
-       {
-               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
-       {
-               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);
-               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);
-       }
-*/
+       lyxerr << "\n------------------------------------------" << endl;
+       for (const_iterator it = begin(); it != end(); ++it)
+               lyxerr << it->first
+                       << " [" << it->second.def() << "] : "
+                       << " [" << it->second.disp() << "] : "
+                       << endl;
+       lyxerr << "------------------------------------------" << endl;
 }