]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
mathed67.diff
[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 "array.h"
13 #include "math_accentinset.h"
14 #include "math_deliminset.h"
15 #include "math_fracinset.h"
16 #include "math_parinset.h"
17 #include "debug.h"
18 #include "support/LAssert.h"
19
20 using std::endl;
21 using std::make_pair;
22
23 MathMacroTable::table_type MathMacroTable::macro_table;
24
25 bool MathMacroTable::built = false;
26
27
28 void MathMacroTable::dump()
29 {
30         using std::cerr;
31
32         cerr << "\n------------------------------------------\n";
33         table_type::const_iterator it;
34         for (it = macro_table.begin(); it != macro_table.end(); ++it)
35                 cerr << it->first << ": " << it->second->GetData() << endl;
36         cerr << "------------------------------------------\n";
37 }
38
39
40 MathMacroTemplate &
41 MathMacroTable::provideTemplate(string const & name, int na)
42 {
43         if (!built)
44                 builtinMacros();
45         
46         if (macro_table.find(name) == macro_table.end())
47                 macro_table.insert(make_pair(name, new MathMacroTemplate(name, na)));
48         
49         return *(macro_table.find(name)->second);
50 }
51
52
53 MathMacroTemplate &
54 MathMacroTable::provideTemplate(string const & name)
55 {
56         if (!built)
57                 builtinMacros();
58         
59         return *macro_table[name];
60 }
61
62
63 bool MathMacroTable::hasTemplate(string const & name)
64 {
65         if (!built)
66                 builtinMacros();
67         
68         return macro_table.find(name) != macro_table.end();
69 }
70
71
72 MathMacro * MathMacroTable::cloneTemplate(string const & name)
73 {
74         return new MathMacro(provideTemplate(name));
75 }
76
77
78 void MathMacroTable::builtinMacros()
79 {
80         built = true;
81     
82         lyxerr[Debug::MATHED] << "Building macros" << endl;
83     
84         // This macro doesn't have arguments
85         {
86                 MathMacroTemplate & m = provideTemplate("notin", 0);
87                 m.push_back(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not), LM_TC_INSET);
88         }
89
90         // This macro doesn't have arguments
91         {
92                 MathMacroTemplate & m = provideTemplate("silentmult", 0);
93                 istringstream is("\\cdot\0");
94                 mathed_parser_file(is, 0);
95                 MathParInset * p = &m;
96         mathed_parse(m.array, p, 0);
97         }
98
99         {
100                 MathMacroTemplate & m = provideTemplate("emptyset", 0);
101                 m.push_back(new MathAccentInset('0', LM_TC_RM, LM_not), LM_TC_INSET);
102         }
103
104         {
105                 MathMacroTemplate & m = provideTemplate("perp", 0);
106                 m.GetData().push_back(LM_bot, LM_TC_BOP);
107         }
108
109         {
110                 MathMacroTemplate & m = provideTemplate("lint", 4);
111                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
112                 mathed_parser_file(is, 0);
113                 MathParInset * p = &m;
114         mathed_parse(m.array, p, 0);
115         }
116
117         // binom has two arguments
118         {
119                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
120                 frac->push_back(new MathMacroArgument(1), LM_TC_INSET);
121                 frac->denom()->push_back(new MathMacroArgument(2), LM_TC_INSET);
122
123                 MathParInset * inset = new MathDelimInset('(', ')');
124                 inset->push_back(frac, LM_TC_ACTIVE_INSET);
125
126                 MathMacroTemplate & m = provideTemplate("binom", 2);
127                 m.push_back(inset, LM_TC_ACTIVE_INSET);
128         }
129 }