]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
get recently broken \mathcircumflex working again (hackish, proper fix in 1.3)
[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         {"label",  LM_TK_LABEL, 0},
88         {"lefteqn",  LM_TK_LEFTEQN, 1},
89         {"ldots",  LM_TK_DOTS, 0},
90         {"left",  LM_TK_LEFT, 0},
91         {"lg",  LM_TK_FUNC, 0},
92         {"lim",  LM_TK_FUNCLIM, 0},
93         {"liminf",  LM_TK_FUNCLIM, 0},
94         {"limits",  LM_TK_LIMIT, 1 },
95         {"limsup",  LM_TK_FUNCLIM, 0},
96         {"ln",  LM_TK_FUNC, 0},
97         {"log",  LM_TK_FUNC, 0},
98         {"lyxbox",  LM_TK_BOX, 0},
99         {"lyxnegspace",  LM_TK_SPACE, 6},
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         //{"mathring",  LM_TK_DECORATION, 0},
107         {"mathrm",  LM_TK_FONT, LM_TC_RM},
108         {"mathsf",  LM_TK_FONT, LM_TC_SF},
109         {"mathtt",  LM_TK_FONT, LM_TC_TT},
110         {"max",  LM_TK_FUNCLIM, 0},
111         //{"mbox",  LM_TK_BOX, 0},
112         {"min",  LM_TK_FUNCLIM, 0},
113         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
114         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
115         {"nonumber",  LM_TK_NONUM, 0},
116         {"not",  LM_TK_NOT, 0},
117         {"over",  LM_TK_OVER, 0},
118         {"overbrace",  LM_TK_DECORATION, 0},
119         {"overleftarrow",  LM_TK_DECORATION, 0},
120         {"overline",  LM_TK_DECORATION, 0},
121         {"overrightarrow",  LM_TK_DECORATION, 0},
122         {"overleftrightarrow", LM_TK_DECORATION, 0},
123         {"protect",  LM_TK_PROTECT, 0},
124         {"qquad",  LM_TK_SPACE, 5},
125         {"quad",  LM_TK_SPACE, 4},
126         {"right",  LM_TK_RIGHT, 0},
127         {"rm",  LM_TK_OLDFONT, LM_TC_RM},
128         {"root",  LM_TK_ROOT, 0},
129         {"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
130         {"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
131         {"sec",  LM_TK_FUNC, 0},
132         {"sin",  LM_TK_FUNC, 0},
133         {"sinh",  LM_TK_FUNC, 0},
134         {"sqrt",  LM_TK_SQRT, 0},
135         {"stackrel",  LM_TK_STACK, 0},
136         {"sup",  LM_TK_FUNCLIM, 0},
137         {"tan",  LM_TK_FUNC, 0},
138         {"tanh",  LM_TK_FUNC, 0},
139         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
140         {"textstyle",  LM_TK_STY, LM_ST_TEXT},
141         {"tilde",  LM_TK_DECORATION, 0},
142         {"tt",  LM_TK_OLDFONT, LM_TC_TT},
143         {"underbar",  LM_TK_DECORATION, 0},
144         {"underbrace",  LM_TK_DECORATION, 0},
145         {"underleftarrow", LM_TK_DECORATION, 0},
146         {"underline",  LM_TK_DECORATION, 0},
147         {"underrightarrow", LM_TK_DECORATION, 0},
148         {"underleftrightarrow", LM_TK_DECORATION, 0},
149         {"underset",  LM_TK_UNDERSET, 0},
150         {"vdots",  LM_TK_DOTS, 0},
151         {"vec",  LM_TK_DECORATION, 0},
152         {"widehat",  LM_TK_DECORATION, 0},
153         {"widetilde",  LM_TK_DECORATION, 0},
154         {"{",  LM_TK_SPECIAL, '{'},
155         {"}",  LM_TK_SPECIAL, '}'}
156 };
157
158
159
160 MathTokenEnum tokenEnum(const string & font)
161 {
162         if (font == "cmr")
163                 return LM_TK_CMR;
164         if (font == "cmsy")
165                 return LM_TK_CMSY;
166         if (font == "cmm")
167                 return LM_TK_CMM;
168         if (font == "cmex")
169                 return LM_TK_CMEX;
170         if (font == "msa")
171                 return LM_TK_MSA;
172         if (font == "msb")
173                 return LM_TK_MSB;
174         return LM_TK_SYM;
175 }
176
177
178 void readSymbols(string const & filename)
179 {
180         LyXLex lex(0, 0);
181         lex.setFile(filename);
182         while (lex.isOK() && lex.next()) {
183                 latexkeys tmp;
184                 tmp.name = lex.getString();
185                 if (lex.next())
186                         tmp.token = tokenEnum(lex.getString());
187                 if (lex.next())
188                         tmp.latex_font_id = lex.getInteger();
189                 if (lex.next())
190                         tmp.id = lex.getInteger();
191                 if (lex.next())
192                         tmp.type = lex.getString();
193                 if (lex.next())
194                         tmp.xmlname = lex.getString();
195                 if (theWordList.find(tmp.name) != theWordList.end())
196                         lyxerr << "readSymbols: token " << tmp.name
197                                << " already exists.\n";
198                 else
199                         theWordList[tmp.name] = tmp;
200         }
201 }
202
203
204 void initSymbols()
205 {
206         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
207         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
208                 latexkeys tmp;
209                 tmp.name          = p->name;
210                 tmp.token         = p->token;
211                 tmp.id            = p->id;
212                 tmp.latex_font_id = 0;
213                 theWordList[p->name] = tmp;
214         }
215
216         lyxerr[Debug::MATHED] << "reading symbols file\n";
217         string const file = LibFileSearch(string(), "symbols");
218         if (file.empty())
219                 lyxerr << "Could not find symbols file\n";
220         else
221                 readSymbols(file);
222 }
223
224
225 } // namespace anon
226
227
228 latexkeys const * in_word_set(string const & str)
229 {
230         static bool initialized = false;
231
232         if (!initialized) {
233                 initSymbols();
234                 initialized = true;
235         }
236
237         WordList::iterator it = theWordList.find(str);
238         return (it != theWordList.end()) ? &(it->second) : 0;
239 }