]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
disable some of the speedup thingies until I found aproper solution for the
[lyx.git] / src / mathed / math_hash.C
1 #include <config.h>
2
3 #include "math_parser.h"
4 #include "math_metricsinfo.h"
5 #include "lyxlex.h"
6 #include "debug.h"
7 #include "support/filetools.h" // LibFileSearch
8
9 #include <map>
10
11 using std::map;
12
13 namespace {
14
15 // file scope
16 typedef map<string, latexkeys> WordList;
17 WordList theWordList;
18
19
20 struct key_type {
21         ///
22         char const * name;
23         ///
24         MathTokenEnum token;
25         ///
26         unsigned int id;
27 };
28
29
30 key_type wordlist_array[] = 
31 {
32         {"!",  LM_TK_SPACE, 0},
33         {"#",  LM_TK_SPECIAL, '#'},
34         {"$",  LM_TK_SPECIAL, '$'},
35         {"%",  LM_TK_SPECIAL, '%'},
36         {"&",  LM_TK_SPECIAL, '&'},
37         {"(",  LM_TK_BEGIN, LM_OT_SIMPLE},
38         {")",  LM_TK_END, LM_OT_SIMPLE},
39         {",",  LM_TK_SPACE, 1},
40         {".",  LM_TK_SPECIAL, '.'},
41         {":",  LM_TK_SPACE, 2},
42         {";",  LM_TK_SPACE, 3},
43         {"Pr",  LM_TK_FUNCLIM, 0},
44         {"[",  LM_TK_BEGIN, LM_OT_EQUATION},
45         {"]",  LM_TK_END, LM_OT_EQUATION},
46         {"_",  LM_TK_SPECIAL, '_'},
47         {"acute",  LM_TK_DECORATION, 0},
48         {"arccos",  LM_TK_FUNC, 0},
49         {"arcsin",  LM_TK_FUNC, 0},
50         {"arctan",  LM_TK_FUNC, 0},
51         {"arg",  LM_TK_FUNC, 0},
52         {"atop",  LM_TK_ATOP, 0},
53         {"bar",  LM_TK_DECORATION, 0},
54         {"begin",  LM_TK_BEGIN, 0},
55         {"bf",  LM_TK_OLDFONT, LM_TC_BF},
56         {"binom",  LM_TK_BINOM, 0},
57         {"bmod",  LM_TK_FUNC, 0},
58         {"breve",  LM_TK_DECORATION, 0},
59         {"cal",  LM_TK_OLDFONT, LM_TC_CAL},
60         {"cdots",  LM_TK_DOTS, 0},
61         {"check",  LM_TK_DECORATION, 0},
62         {"choose",  LM_TK_CHOOSE, 0},
63         {"cos",  LM_TK_FUNC, 0},
64         {"cosh",  LM_TK_FUNC, 0},
65         {"cot",  LM_TK_FUNC, 0},
66         {"coth",  LM_TK_FUNC, 0},
67         {"csc",  LM_TK_FUNC, 0},
68         {"ddot",  LM_TK_DECORATION, 0},
69         {"dddot",  LM_TK_DECORATION, 0},
70         {"ddots",  LM_TK_DOTS, 0},
71         {"deg",  LM_TK_FUNC, 0},
72         {"det",  LM_TK_FUNCLIM, 0},
73         {"dim",  LM_TK_FUNC, 0},
74         {"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
75         {"dot",  LM_TK_DECORATION, 0},
76         {"end",  LM_TK_END, 0},
77         {"exp",  LM_TK_FUNC, 0},
78         {"frac",  LM_TK_FRAC, 0},
79         {"frak",  LM_TK_FONT, LM_TC_EUFRAK},
80         {"gcd",  LM_TK_FUNCLIM, 0},
81         {"grave",  LM_TK_DECORATION, 0},
82         {"hat",  LM_TK_DECORATION, 0},
83         {"hom",  LM_TK_FUNC, 0},
84         {"inf",  LM_TK_FUNCLIM, 0},
85         {"it",  LM_TK_OLDFONT, LM_TC_IT},
86         {"ker",  LM_TK_FUNC, 0},
87         {"kern",  LM_TK_KERN, 0},
88         {"label",  LM_TK_LABEL, 0},
89         {"lefteqn",  LM_TK_LEFTEQN, 1},
90         {"ldots",  LM_TK_DOTS, 0},
91         {"left",  LM_TK_LEFT, 0},
92         {"lg",  LM_TK_FUNC, 0},
93         {"lim",  LM_TK_FUNCLIM, 0},
94         {"liminf",  LM_TK_FUNCLIM, 0},
95         {"limits",  LM_TK_LIMIT, 1 },
96         {"limsup",  LM_TK_FUNCLIM, 0},
97         {"ln",  LM_TK_FUNC, 0},
98         {"log",  LM_TK_FUNC, 0},
99         {"lyxbox",  LM_TK_BOX, 0},
100         {"mathbb",  LM_TK_FONT, LM_TC_BB},
101         {"mathbf",  LM_TK_FONT, LM_TC_BF},
102         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
103         {"mathfrak",  LM_TK_FONT, LM_TC_EUFRAK},
104         {"mathit",  LM_TK_FONT, LM_TC_IT},
105         {"mathnormal",  LM_TK_FONT, LM_TC_VAR},
106         {"mathrm",  LM_TK_FONT, LM_TC_RM},
107         {"mathsf",  LM_TK_FONT, LM_TC_SF},
108         {"mathtt",  LM_TK_FONT, LM_TC_TT},
109         {"max",  LM_TK_FUNCLIM, 0},
110         //{"mbox",  LM_TK_BOX, 0},
111         {"min",  LM_TK_FUNCLIM, 0},
112         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
113         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
114         {"nonumber",  LM_TK_NONUM, 0},
115         {"not",  LM_TK_NOT, 0},
116         {"over",  LM_TK_OVER, 0},
117         {"overbrace",  LM_TK_DECORATION, 0},
118         {"overleftarrow",  LM_TK_DECORATION, 0},
119         {"overline",  LM_TK_DECORATION, 0},
120         {"overrightarrow",  LM_TK_DECORATION, 0},
121         {"overleftrightarrow", LM_TK_DECORATION, 0},
122         {"protect",  LM_TK_PROTECT, 0},
123         {"qquad",  LM_TK_SPACE, 5},
124         {"quad",  LM_TK_SPACE, 4},
125         {"right",  LM_TK_RIGHT, 0},
126         {"rm",  LM_TK_OLDFONT, LM_TC_RM},
127         {"root",  LM_TK_ROOT, 0},
128         {"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
129         {"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
130         {"sec",  LM_TK_FUNC, 0},
131         {"sin",  LM_TK_FUNC, 0},
132         {"sinh",  LM_TK_FUNC, 0},
133         {"sqrt",  LM_TK_SQRT, 0},
134         {"stackrel",  LM_TK_STACK, 0},
135         {"sup",  LM_TK_FUNCLIM, 0},
136         {"tan",  LM_TK_FUNC, 0},
137         {"tanh",  LM_TK_FUNC, 0},
138         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
139         {"textstyle",  LM_TK_STY, LM_ST_TEXT},
140         {"tilde",  LM_TK_DECORATION, 0},
141         {"tt",  LM_TK_OLDFONT, LM_TC_TT},
142         {"underbar",  LM_TK_DECORATION, 0},
143         {"underbrace",  LM_TK_DECORATION, 0},
144         {"underleftarrow", LM_TK_DECORATION, 0},
145         {"underline",  LM_TK_DECORATION, 0},
146         {"underrightarrow", LM_TK_DECORATION, 0},
147         {"underleftrightarrow", LM_TK_DECORATION, 0},
148         {"underset",  LM_TK_UNDERSET, 0},
149         {"vdots",  LM_TK_DOTS, 0},
150         {"vec",  LM_TK_DECORATION, 0},
151         {"widehat",  LM_TK_DECORATION, 0},
152         {"widetilde",  LM_TK_DECORATION, 0},
153         {"{",  LM_TK_SPECIAL, '{'},
154         {"}",  LM_TK_SPECIAL, '}'}
155 };
156
157
158
159 MathTokenEnum tokenEnum(const string & font)
160 {
161         if (font == "cmr")
162                 return LM_TK_CMR;
163         if (font == "cmsy")
164                 return LM_TK_CMSY;
165         if (font == "cmm")
166                 return LM_TK_CMM;
167         if (font == "cmex")
168                 return LM_TK_CMEX;
169         if (font == "msa")
170                 return LM_TK_MSA;
171         if (font == "msb")
172                 return LM_TK_MSB;
173         return LM_TK_SYM;
174 }
175
176
177 void readSymbols(string const & filename)
178 {
179         LyXLex lex(0, 0);
180         lex.setFile(filename);
181         while (lex.isOK() && lex.next()) {
182                 latexkeys tmp;
183                 tmp.name = lex.getString();
184                 if (lex.next())
185                         tmp.token = tokenEnum(lex.getString());
186                 if (lex.next())
187                         tmp.latex_font_id = lex.getInteger();
188                 if (lex.next())
189                         tmp.id = lex.getInteger();
190                 if (lex.next())
191                         tmp.type = lex.getString();
192                 if (lex.next())
193                         tmp.xmlname = lex.getString();
194                 if (theWordList.find(tmp.name) != theWordList.end())
195                         lyxerr << "readSymbols: token " << tmp.name
196                                << " already exists.\n";
197                 else
198                         theWordList[tmp.name] = tmp;
199         }
200 }
201
202
203 void initSymbols()
204 {
205         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
206         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
207                 latexkeys tmp;
208                 tmp.name          = p->name;
209                 tmp.token         = p->token;
210                 tmp.id            = p->id;
211                 tmp.latex_font_id = 0;
212                 theWordList[p->name] = tmp;
213         }
214
215         lyxerr[Debug::MATHED] << "reading symbols file\n";
216         string const file = LibFileSearch(string(), "symbols");
217         if (file.empty())
218                 lyxerr << "Could not find symbols file\n";
219         else
220                 readSymbols(file);
221 }
222
223
224 } // namespace anon
225
226
227 latexkeys const * in_word_set(string const & str)
228 {
229         static bool initialized = false;
230
231         if (!initialized) {
232                 initSymbols();
233                 initialized = true;
234         }
235
236         WordList::iterator it = theWordList.find(str);
237         return (it != theWordList.end()) ? &(it->second) : 0;
238 }