]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
work around static initialization problem
[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 "array.h"
14 #include "math_decorationinset.h"
15 #include "math_deliminset.h"
16 #include "math_fracinset.h"
17 #include "math_inset.h"
18 #include "debug.h"
19 #include "support/LAssert.h"
20
21 using std::endl;
22
23 MathArray mathed_parse_cell(string const &);
24
25
26 MathMacroTable::table_type MathMacroTable::macro_table;
27
28
29 void MathMacroTable::dump()
30 {
31         lyxerr << "\n------------------------------------------\n";
32         table_type::const_iterator it;
33         for (it = macro_table.begin(); it != macro_table.end(); ++it)
34                 lyxerr << it->first << " [" << it->second->nargs() << "] : "
35                         << it->second << endl;
36         lyxerr << "------------------------------------------" << endl;;
37 }
38
39
40 void MathMacroTable::updateTemplate(MathMacroTemplate * par)
41 {
42         table_type::iterator pos = macro_table.find(par->name());
43
44         if (pos == macro_table.end())
45                 lyxerr << "MathMacroTable::updateTemplate: no template with name '"
46                        << par->name() << "' available." << endl;
47         else
48                 pos->second = par;
49 }
50
51
52 void MathMacroTable::insertTemplate(MathMacroTemplate * p)
53 {
54         macro_table[p->name()] = p;
55 }
56
57
58 MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
59 {
60         builtinMacros();
61         
62         table_type::iterator pos = macro_table.find(name);
63
64         if (pos == macro_table.end()) {
65                 lyxerr << "MathMacroTable::provideTemplate: no template with name '"
66                        << name << "' available." << endl;
67         }
68                 
69         return *pos->second;
70 }
71
72
73 void MathMacroTable::createTemplate
74         (string const & name, int na, string const & text)
75 {
76         MathMacroTemplate * t = new MathMacroTemplate(name, na);
77         t->cell(0) = mathed_parse_cell(text);
78         insertTemplate(t);
79 }
80
81
82 bool MathMacroTable::hasTemplate(string const & name)
83 {
84         builtinMacros();
85         return macro_table.find(name) != macro_table.end();
86 }
87
88
89 MathMacro * MathMacroTable::cloneTemplate(string const & name)
90 {
91         return new MathMacro(provideTemplate(name));
92 }
93
94
95 void MathMacroTable::builtinMacros()
96 {
97         static bool built = false;
98         
99         if (built)
100                 return; 
101
102         built = true;
103     
104         //lyxerr[Debug::MATHED] << "Building macros" << endl;
105    
106 /* 
107         // This macro doesn't have arguments
108         {
109                 MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
110                 MathDecorationInset * p = new MathDecorationInset("not");
111                 p->cell(0).push_back(LM_in, LM_TC_SYMB);
112                 t->push_back(p);
113                 insertTemplate(t);
114         }
115 */
116
117         createTemplate("silentmult", 0, "\\cdot");
118         createTemplate("emptyset",   0, "\\not0");
119         createTemplate("notin",      0, "\\not\\in");
120
121
122
123 /*
124         {
125                 MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0);
126                 MathDecorationInset * p = new MathDecorationInset("not", LM_not);
127                 p->cell(0).push_back('O', LM_TC_VAR);
128                 t->push_back(p);
129                 insertTemplate(t);
130         }
131 */
132
133         {
134
135                 MathMacroTemplate * t = new MathMacroTemplate("land", 0);
136                 t->push_back(LM_wedge, LM_TC_SYMB);
137                 insertTemplate(t);
138         }
139
140         {
141                 MathMacroTemplate * t = new MathMacroTemplate("lor", 0);
142                 t->push_back(LM_vee, LM_TC_SYMB);
143                 insertTemplate(t);
144         }
145
146         {
147                 MathMacroTemplate * t = new MathMacroTemplate("to", 0);
148                 t->push_back(LM_rightarrow, LM_TC_SYMB);
149                 insertTemplate(t);
150         }
151
152         {
153                 MathMacroTemplate * t = new MathMacroTemplate("perp", 0);
154                 t->push_back(LM_bot, LM_TC_BOP);
155                 insertTemplate(t);
156         }
157
158 /*
159         {
160                 MathMacroTemplate & m = createTemplate("lint", 4);
161                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
162                 mathed_parser_file(is, 0);
163                 MathMatrixInset * p = &m;
164         mathed_parse(m.array, p, 0);
165         }
166 */
167 /*
168         {
169                 MathMacroTemplate * t = new MathMacroTemplate("binomii", 2);
170                 istringstream is("\\left(\\frac{#1}{#2}\\right)\0");
171                 mathed_parser_file(is, 0);
172         mathed_parse(t->array, t, 0);
173                 insertTemplate(t);
174         }
175 */
176
177         // binom has two arguments
178         {
179                 MathFracInset * frac = new MathFracInset("atop");
180                 frac->cell(0).push_back(new MathMacroArgument(1));
181                 frac->cell(1).push_back(new MathMacroArgument(2));
182
183                 MathInset * inset = new MathDelimInset('(', ')');
184                 inset->push_back(frac);
185
186                 MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
187                 t->push_back(inset);
188
189                 insertTemplate(t);
190         }
191
192 /*
193         {
194                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
195                 frac->cell(0)->push_back(new MathMacroArgument(1));
196                 frac->cell(1)->push_back(new MathMacroArgument(2));
197
198                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
199                 t->push_back(frac);
200
201                 insertTemplate(t);
202         }
203 */
204 }