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