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