]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
fix (potential?) memory leaks; cosmetics
[lyx.git] / src / mathed / math_macrotable.C
1 #include <config.h>
2
3 #include <iostream>
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include "math_macrotable.h"
10 #include "math_macro.h"
11 #include "math_macrotemplate.h"
12 #include "math_parser.h"
13 #include "math_deliminset.h"
14 #include "math_fracinset.h"
15 #include "debug.h"
16
17
18 MathArray mathed_parse_cell(string const &);
19
20
21 MathMacroTable::table_type MathMacroTable::macro_table;
22
23
24 void MathMacroTable::dump()
25 {
26         lyxerr << "\n------------------------------------------\n";
27         table_type::const_iterator it;
28         for (it = macro_table.begin(); it != macro_table.end(); ++it)
29                 lyxerr << it->first << " [" << it->second->nargs() << "] : "
30                         << it->second << "\n";
31         lyxerr << "------------------------------------------\n";
32 }
33
34
35 void MathMacroTable::insertTemplate(MathMacroTemplate * p)
36 {
37         macro_table[p->name()] = p;
38 }
39
40
41 MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
42 {
43         builtinMacros();
44         
45         table_type::iterator pos = macro_table.find(name);
46
47         if (pos == macro_table.end()) {
48                 lyxerr << "MathMacroTable::provideTemplate: no template with name '"
49                        << name << "' available.\n";
50         }
51                 
52         return *pos->second;
53 }
54
55
56 void MathMacroTable::createTemplate
57         (string const & name, int na, string const & text)
58 {
59         MathMacroTemplate * t = new MathMacroTemplate(name, na);
60         t->cell(0) = mathed_parse_cell(text);
61         insertTemplate(t);
62 #ifdef WITH_WARNINGS
63 #warning who frees this?
64 #endif
65 }
66
67
68 bool MathMacroTable::hasTemplate(string const & name)
69 {
70         builtinMacros();
71         return macro_table.find(name) != macro_table.end();
72 }
73
74
75 MathMacro * MathMacroTable::cloneTemplate(string const & name)
76 {
77         return new MathMacro(provideTemplate(name));
78 }
79
80
81 void MathMacroTable::builtinMacros()
82 {
83         static bool built = false;
84         
85         if (built)
86                 return; 
87
88         built = true;
89         //lyxerr[Debug::MATHED] << "Building macros\n";
90    
91         createTemplate("emptyset",     0, "\\not0");
92         createTemplate("ge",           0, "\\geq");
93         createTemplate("gets",         0, "\\leftarrow");
94         createTemplate("land",         0, "\\wedge");
95         createTemplate("le",           0, "\\leq");
96         createTemplate("lor",          0, "\\vee");
97         createTemplate("notin",        0, "\\not\\in");
98         createTemplate("perp",         0, "\\bot");
99         createTemplate("to",           0, "\\rightarrow");
100         //createTemplate("lint",       4, "\\int_{#1}^{#2}#3 d#4");
101         //createTemplate("silentmult", 0, "\\cdot");
102         //createTemplate("binomi",     2, "\\left(\\frac{#1}{#2}\\right)");
103
104         // binom has two arguments
105         {
106                 MathFracInset * frac = new MathFracInset("atop");
107                 frac->cell(0).push_back(new MathMacroArgument(1));
108                 frac->cell(1).push_back(new MathMacroArgument(2));
109
110                 MathInset * inset = new MathDelimInset('(', ')');
111                 inset->push_back(frac);
112
113                 MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
114                 t->push_back(inset);
115
116                 insertTemplate(t);
117         }
118
119 /*
120         {
121                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
122                 frac->cell(0)->push_back(new MathMacroArgument(1));
123                 frac->cell(1)->push_back(new MathMacroArgument(2));
124
125                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
126                 t->push_back(frac);
127
128                 insertTemplate(t);
129         }
130 */
131 }