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