]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
LyX Drinkers United: patch 2
[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_accentinset.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
22 using namespace std;
23
24
25 MathMacroTable::table_type MathMacroTable::macro_table;
26
27
28 void MathMacroTable::dump()
29 {
30         cerr << "\n------------------------------------------\n";
31         table_type::const_iterator it;
32         for (it = macro_table.begin(); it != macro_table.end(); ++it)
33                 cerr << it->first << " [" << it->second->nargs() << "] : "
34                         << it->second << endl;
35         cerr << "------------------------------------------\n";
36 }
37
38
39 void MathMacroTable::updateTemplate(MathMacroTemplate * par)
40 {
41         table_type::iterator pos = macro_table.find(par->name());
42
43         if (pos == macro_table.end())
44                 lyxerr << "MathMacroTable::updateTemplate: no template with name '"
45                                 << par->name() << "' available.\n";
46         else
47                 pos->second = par;
48 }
49
50
51 void MathMacroTable::insertTemplate(MathMacroTemplate * p)
52 {
53         macro_table[p->name()] = p;
54 }
55
56
57 MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
58 {
59         builtinMacros();
60         
61         table_type::iterator pos = macro_table.find(name);
62
63         if (pos == macro_table.end()) {
64                 lyxerr << "MathMacroTable::provideTemplate: no template with name '"
65                                 << name << "' available.\n";
66         }
67                 
68         return *pos->second;
69 }
70
71
72 bool MathMacroTable::hasTemplate(string const & name)
73 {
74         builtinMacros();
75         return macro_table.find(name) != macro_table.end();
76 }
77
78
79 MathMacro * MathMacroTable::cloneTemplate(string const & name)
80 {
81         return new MathMacro(provideTemplate(name));
82 }
83
84
85 void MathMacroTable::builtinMacros()
86 {
87         static bool built = false;
88         
89         if (built)
90                 return; 
91
92         built = true;
93     
94         lyxerr[Debug::MATHED] << "Building macros\n";
95     
96         // This macro doesn't have arguments
97         {
98                 MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
99                 t->push_back(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not));
100                 insertTemplate(t);
101         }
102
103 /*
104         // This macro doesn't have arguments
105         {
106                 MathMacroTemplate & m = createTemplate("silentmult", 0);
107                 istringstream is("\\cdot\0");
108                 mathed_parser_file(is, 0);
109                 MathMatrixInset * p = &m;
110         mathed_parse(m.array, p, 0);
111         }
112 */
113
114         {
115                 MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0);
116                 t->push_back(new MathAccentInset('0', LM_TC_RM, LM_not));
117                 insertTemplate(t);
118         }
119
120         {
121                 MathMacroTemplate * t = new MathMacroTemplate("to", 0);
122                 t->push_back(LM_rightarrow, LM_TC_SYMB);
123                 insertTemplate(t);
124         }
125
126         {
127                 MathMacroTemplate * t = new MathMacroTemplate("perp", 0);
128                 t->push_back(LM_bot, LM_TC_BOP);
129                 insertTemplate(t);
130         }
131 /*
132         {
133                 MathMacroTemplate & m = createTemplate("lint", 4);
134                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
135                 mathed_parser_file(is, 0);
136                 MathMatrixInset * p = &m;
137         mathed_parse(m.array, p, 0);
138         }
139 */
140 /*
141         {
142                 MathMacroTemplate * t = new MathMacroTemplate("binomii", 2);
143                 istringstream is("\\left(\\frac{#1}{#2}\\right)\0");
144                 mathed_parser_file(is, 0);
145         mathed_parse(t->array, t, 0);
146                 insertTemplate(t);
147         }
148 */
149
150         // binom has two arguments
151         {
152                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
153                 frac->cell(0).push_back(new MathMacroArgument(1));
154                 frac->cell(1).push_back(new MathMacroArgument(2));
155
156                 MathInset * inset = new MathDelimInset('(', ')');
157                 inset->push_back(frac);
158
159                 MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
160                 t->push_back(inset);
161
162                 insertTemplate(t);
163         }
164
165 /*
166         {
167                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
168                 frac->cell(0)->push_back(new MathMacroArgument(1));
169                 frac->cell(1)->push_back(new MathMacroArgument(2));
170
171                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
172                 t->push_back(frac);
173
174                 insertTemplate(t);
175         }
176 */
177 }