]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
LyX Drinkers Union: patch 1
[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("perp", 0);
122                 t->push_back(LM_bot, LM_TC_BOP);
123                 insertTemplate(t);
124         }
125 /*
126         {
127                 MathMacroTemplate & m = createTemplate("lint", 4);
128                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
129                 mathed_parser_file(is, 0);
130                 MathMatrixInset * p = &m;
131         mathed_parse(m.array, p, 0);
132         }
133 */
134 /*
135         {
136                 MathMacroTemplate * t = new MathMacroTemplate("binomii", 2);
137                 istringstream is("\\left(\\frac{#1}{#2}\\right)\0");
138                 mathed_parser_file(is, 0);
139         mathed_parse(t->array, t, 0);
140                 insertTemplate(t);
141         }
142 */
143
144         // binom has two arguments
145         {
146                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
147                 frac->cell(0).push_back(new MathMacroArgument(1));
148                 frac->cell(1).push_back(new MathMacroArgument(2));
149
150                 MathInset * inset = new MathDelimInset('(', ')');
151                 inset->push_back(frac);
152
153                 MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
154                 t->push_back(inset);
155
156                 insertTemplate(t);
157         }
158
159 /*
160         {
161                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
162                 frac->cell(0)->push_back(new MathMacroArgument(1));
163                 frac->cell(1)->push_back(new MathMacroArgument(2));
164
165                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
166                 t->push_back(frac);
167
168                 insertTemplate(t);
169         }
170 */
171 }