]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
mathed65.diff
[lyx.git] / src / mathed / math_macrotable.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_macrotable.h"
8 #include "math_macro.h"
9 #include "math_macrotemplate.h"
10 #include "math_iter.h"
11 #include "array.h"
12 #include "math_accentinset.h"
13 #include "math_deliminset.h"
14 #include "math_fracinset.h"
15 #include "math_parinset.h"
16 #include "debug.h"
17 #include "support/LAssert.h"
18
19 using std::endl;
20 using std::make_pair;
21
22 MathMacroTable::table_type MathMacroTable::macro_table;
23
24 bool MathMacroTable::built = false;
25
26
27 void MathMacroTable::dump()
28 {
29         using std::cerr;
30
31         cerr << "\n------------------------------------------\n";
32         table_type::const_iterator it;
33         for (it = macro_table.begin(); it != macro_table.end(); ++it)
34                 cerr << it->first << ": " << it->second->GetData() << endl;
35         cerr << "------------------------------------------\n";
36 }
37
38
39 MathMacroTemplate &
40 MathMacroTable::provideTemplate(string const & name, int na)
41 {
42         if (!built)
43                 builtinMacros();
44         
45         if (macro_table.find(name) == macro_table.end())
46                 macro_table.insert(make_pair(name, new MathMacroTemplate(name, na)));
47         
48         return *(macro_table.find(name)->second);
49 }
50
51
52 MathMacroTemplate &
53 MathMacroTable::provideTemplate(string const & name)
54 {
55         if (!built)
56                 builtinMacros();
57         
58         return *macro_table[name];
59 }
60
61
62 bool MathMacroTable::hasTemplate(string const & name)
63 {
64         if (!built)
65                 builtinMacros();
66         
67         return macro_table.find(name) != macro_table.end();
68 }
69
70
71 MathMacro * MathMacroTable::cloneTemplate(string const & name)
72 {
73         return new MathMacro(provideTemplate(name));
74 }
75
76
77 void MathMacroTable::builtinMacros()
78 {
79         built = true;
80     
81         lyxerr[Debug::MATHED] << "Building macros" << endl;
82     
83         // This macro doesn't have arguments
84         {
85                 MathMacroTemplate & m = provideTemplate("notin", 0);
86                 MathedIter iter(&m.GetData());
87                 iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not),
88                                  LM_TC_INSET);
89         }
90
91         // This macro doesn't have arguments
92         {
93                 MathMacroTemplate & m = provideTemplate("silentmult", 0);
94                 istringstream is("\\cdot\0");
95                 mathed_parser_file(is, 0);
96                 MathParInset * p = &m;
97         mathed_parse(m.array, p, 0);
98         }
99
100         // These two are only while we are still with LyX 2.x
101         {
102                 MathMacroTemplate & m = provideTemplate("emptyset", 0);
103                 MathedIter iter(&m.GetData());
104                 iter.insertInset(new MathAccentInset('0', LM_TC_RM, LM_not),
105                                  LM_TC_INSET);
106         }
107
108         {
109                 MathMacroTemplate & m = provideTemplate("perp", 0);
110                 MathedIter iter(&m.GetData());
111                 iter.insert(LM_bot, LM_TC_BOP);
112         }
113
114         {
115                 MathMacroTemplate & m = provideTemplate("lint", 4);
116                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
117                 mathed_parser_file(is, 0);
118                 MathParInset * p = &m;
119         mathed_parse(m.array, p, 0);
120         }
121
122         {
123                 MathMacroTemplate & m = provideTemplate("binom", 2);
124                 istringstream is("\\choose{#1}{#2}");
125                 mathed_parser_file(is, 0);
126                 MathParInset * p = &m;
127         mathed_parse(m.array, p, 0);
128         }
129
130         // binom has two arguments
131         {
132                 MathMacroTemplate & m = provideTemplate("binom1", 2);
133                 MathedIter iter(&m.GetData());
134
135                 MathParInset * inset = new MathDelimInset('(', ')');
136                 iter.insertInset(inset, LM_TC_ACTIVE_INSET);
137
138                 MathedArray array2;
139                 MathedIter iter2(&array2);
140                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
141                 iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
142                 frac->setData(array2);
143
144                 MathedArray array3;
145                 MathedIter iter3(&array3);
146                 iter3.insertInset(new MathMacroArgument(1), LM_TC_INSET);
147
148                 MathedArray array4;
149                 MathedIter iter4(&array4);
150                 iter4.insertInset(new MathMacroArgument(2), LM_TC_INSET);
151
152                 frac->SetData(array3, array4);
153         }
154
155 /*    
156         {
157                 boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("perp", 0));
158                 addTemplate(m);
159                 MathedArray array;
160                 MathedIter iter(&array);
161                 iter.insert(LM_bot, LM_TC_BOP);
162                 m->setData(array);
163         }
164
165         // binom has two arguments
166         {
167                 boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("binom", 2));
168                 addTemplate(m);
169                 MathedArray array;
170                 m->setData(array);
171                 MathedIter iter(&array);
172                 MathParInset * inset = new MathDelimInset('(', ')');
173                 iter.insertInset(inset, LM_TC_ACTIVE_INSET);
174                 array = MathedArray();
175                 MathedIter iter2(&array);
176                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
177                 iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
178                 inset->setData(array);
179                 array = MathedArray();
180                 MathedArray array2;
181                 MathedIter iter3(&array);
182                 iter3.insertInset(m->getMacroPar(0), LM_TC_INSET);
183                 MathedIter iter4(&array2);
184                 iter4.insertInset(m->getMacroPar(1), LM_TC_INSET);
185                 frac->SetData(array, array2);
186         }
187 */
188 }