]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
move ownership of the templates from the formulamacro
[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 "math_deliminset.h"
14 #include "math_fracinset.h"
15 #include "debug.h"
16
17
18 MathArray mathed_parse_cell(string const &);
19
20
21 MathMacroTable::table_type MathMacroTable::macro_table;
22
23
24 void MathMacroTable::dump()
25 {
26         lyxerr << "\n------------------------------------------\n";
27         table_type::const_iterator it;
28         for (it = macro_table.begin(); it != macro_table.end(); ++it)
29                 lyxerr << it->first << " [" << it->second.nargs() << "] : "
30                         << it->second << "\n";
31         lyxerr << "------------------------------------------\n";
32 }
33
34
35 void MathMacroTable::insertTemplate(MathMacroTemplate const & p)
36 {
37         if (macro_table.find(p.name()) != macro_table.end()) 
38                 lyxerr << "macro '" << p.name() << "' not new\n";
39         macro_table[p.name()] = p;
40 }
41
42
43 MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
44 {
45         builtinMacros();
46         
47         table_type::iterator pos = macro_table.find(name);
48
49         if (pos == macro_table.end()) {
50                 lyxerr << "MathMacroTable::provideTemplate: no template with name '"
51                        << name << "' available.\n";
52         }
53                 
54         return pos->second;
55 }
56
57
58 void MathMacroTable::createTemplate
59         (string const & name, int na, string const & text)
60 {
61         MathMacroTemplate t(name, na);
62         t.cell(0) = mathed_parse_cell(text);
63         insertTemplate(t);
64 }
65
66
67 bool MathMacroTable::hasTemplate(string const & name)
68 {
69         builtinMacros();
70         return macro_table.find(name) != macro_table.end();
71 }
72
73
74 MathMacro * MathMacroTable::cloneTemplate(string const & name)
75 {
76         return new MathMacro(provideTemplate(name));
77 }
78
79
80 void MathMacroTable::builtinMacros()
81 {
82         static bool built = false;
83         
84         if (built)
85                 return; 
86
87         built = true;
88         //lyxerr[Debug::MATHED] << "Building macros\n";
89    
90         createTemplate("emptyset",     0, "\\not0");
91         createTemplate("ge",           0, "\\geq");
92         createTemplate("gets",         0, "\\leftarrow");
93         createTemplate("land",         0, "\\wedge");
94         createTemplate("le",           0, "\\leq");
95         createTemplate("lor",          0, "\\vee");
96         createTemplate("notin",        0, "\\not\\in");
97         createTemplate("perp",         0, "\\bot");
98         createTemplate("to",           0, "\\rightarrow");
99         //createTemplate("lint",       4, "\\int_{#1}^{#2}#3 d#4");
100         //createTemplate("silentmult", 0, "\\cdot");
101         //createTemplate("binomi",     2, "\\left(\\frac{#1}{#2}\\right)");
102
103         // binom has two arguments
104         {
105                 MathFracInset * frac = new MathFracInset("atop");
106                 frac->cell(0).push_back(new MathMacroArgument(1));
107                 frac->cell(1).push_back(new MathMacroArgument(2));
108
109                 MathInset * inset = new MathDelimInset('(', ')');
110                 inset->push_back(frac);
111
112                 MathMacroTemplate t("binom", 2);
113                 t.push_back(inset);
114                 insertTemplate(t);
115         }
116
117 /*
118         {
119                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
120                 frac->cell(0)->push_back(new MathMacroArgument(1));
121                 frac->cell(1)->push_back(new MathMacroArgument(2));
122
123                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
124                 t->push_back(frac);
125
126                 insertTemplate(t);
127         }
128 */
129 }