]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
fix pullArg when pressing <Delete> at the end of an cell
[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 "array.h"
14 #include "math_decorationinset.h"
15 #include "math_deliminset.h"
16 #include "math_fracinset.h"
17 #include "math_inset.h"
18 #include "debug.h"
19 #include "support/LAssert.h"
20
21 using std::endl;
22
23 MathMacroTable::table_type MathMacroTable::macro_table;
24
25
26 void MathMacroTable::dump()
27 {
28         lyxerr << "\n------------------------------------------\n";
29         table_type::const_iterator it;
30         for (it = macro_table.begin(); it != macro_table.end(); ++it)
31                 lyxerr << it->first << " [" << it->second->nargs() << "] : "
32                         << it->second << endl;
33         lyxerr << "------------------------------------------" << endl;;
34 }
35
36
37 void MathMacroTable::updateTemplate(MathMacroTemplate * par)
38 {
39         table_type::iterator pos = macro_table.find(par->name());
40
41         if (pos == macro_table.end())
42                 lyxerr << "MathMacroTable::updateTemplate: no template with name '"
43                        << par->name() << "' available." << endl;
44         else
45                 pos->second = par;
46 }
47
48
49 void MathMacroTable::insertTemplate(MathMacroTemplate * p)
50 {
51         macro_table[p->name()] = p;
52 }
53
54
55 MathMacroTemplate & MathMacroTable::provideTemplate(string const & name)
56 {
57         builtinMacros();
58         
59         table_type::iterator pos = macro_table.find(name);
60
61         if (pos == macro_table.end()) {
62                 lyxerr << "MathMacroTable::provideTemplate: no template with name '"
63                        << name << "' available." << endl;
64         }
65                 
66         return *pos->second;
67 }
68
69
70 bool MathMacroTable::hasTemplate(string const & name)
71 {
72         builtinMacros();
73         return macro_table.find(name) != macro_table.end();
74 }
75
76
77 MathMacro * MathMacroTable::cloneTemplate(string const & name)
78 {
79         return new MathMacro(provideTemplate(name));
80 }
81
82
83 void MathMacroTable::builtinMacros()
84 {
85         static bool built = false;
86         
87         if (built)
88                 return; 
89
90         built = true;
91     
92         lyxerr[Debug::MATHED] << "Building macros" << endl;
93     
94         // This macro doesn't have arguments
95         {
96                 MathMacroTemplate * t = new MathMacroTemplate("notin", 0);
97                 MathDecorationInset * p = new MathDecorationInset("not", LM_not);
98                 p->cell(0).push_back(LM_in, LM_TC_BOPS);
99                 t->push_back(p);
100                 insertTemplate(t);
101         }
102
103 /*
104         // This macro doesn't have arguments
105         {
106                 MathMacroTemplate & m = createTemplate("silentmult", 0);
107                 istringstream is("\\cdot\0");
108                 mathed_parser_file(is, 0);
109                 MathMatrixInset * p = &m;
110         mathed_parse(m.array, p, 0);
111         }
112 */
113
114         {
115                 MathMacroTemplate * t = new MathMacroTemplate("emptyset", 0);
116                 MathDecorationInset * p = new MathDecorationInset("not", LM_not);
117                 p->cell(0).push_back('O', LM_TC_VAR);
118                 t->push_back(p);
119                 insertTemplate(t);
120         }
121
122         {
123                 MathMacroTemplate * t = new MathMacroTemplate("land", 0);
124                 t->push_back(LM_wedge, LM_TC_SYMB);
125                 insertTemplate(t);
126         }
127
128         {
129                 MathMacroTemplate * t = new MathMacroTemplate("lor", 0);
130                 t->push_back(LM_vee, LM_TC_SYMB);
131                 insertTemplate(t);
132         }
133
134         {
135                 MathMacroTemplate * t = new MathMacroTemplate("to", 0);
136                 t->push_back(LM_rightarrow, LM_TC_SYMB);
137                 insertTemplate(t);
138         }
139
140         {
141                 MathMacroTemplate * t = new MathMacroTemplate("perp", 0);
142                 t->push_back(LM_bot, LM_TC_BOP);
143                 insertTemplate(t);
144         }
145
146 /*
147         {
148                 MathMacroTemplate & m = createTemplate("lint", 4);
149                 istringstream is("\\int_{#1}^{#2}#3 d#4\0");
150                 mathed_parser_file(is, 0);
151                 MathMatrixInset * p = &m;
152         mathed_parse(m.array, p, 0);
153         }
154 */
155 /*
156         {
157                 MathMacroTemplate * t = new MathMacroTemplate("binomii", 2);
158                 istringstream is("\\left(\\frac{#1}{#2}\\right)\0");
159                 mathed_parser_file(is, 0);
160         mathed_parse(t->array, t, 0);
161                 insertTemplate(t);
162         }
163 */
164
165         // binom has two arguments
166         {
167                 MathFracInset * frac = new MathFracInset("atop");
168                 frac->cell(0).push_back(new MathMacroArgument(1));
169                 frac->cell(1).push_back(new MathMacroArgument(2));
170
171                 MathInset * inset = new MathDelimInset('(', ')');
172                 inset->push_back(frac);
173
174                 MathMacroTemplate * t = new MathMacroTemplate("binom", 2);
175                 t->push_back(inset);
176
177                 insertTemplate(t);
178         }
179
180 /*
181         {
182                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
183                 frac->cell(0)->push_back(new MathMacroArgument(1));
184                 frac->cell(1)->push_back(new MathMacroArgument(2));
185
186                 MathMacroTemplate * t = new MathMacroTemplate("choose", 2);
187                 t->push_back(frac);
188
189                 insertTemplate(t);
190         }
191 */
192 }