]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
allow \frak as synomnym to \mathfrak
[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         {"frak",  LM_TK_FONT, LM_TC_EUFRAK},
74         {"gcd",  LM_TK_FUNCLIM, 0},
75         {"grave",  LM_TK_DECORATION, 0},
76         {"hat",  LM_TK_DECORATION, 0},
77         {"hom",  LM_TK_FUNC, 0},
78         {"inf",  LM_TK_FUNCLIM, 0},
79         {"it",  LM_TK_OLDFONT, LM_TC_IT},
80         {"ker",  LM_TK_FUNC, 0},
81         {"kern",  LM_TK_KERN, 0},
82         {"label",  LM_TK_LABEL, 0},
83         {"lefteqn",  LM_TK_LEFTEQN, 1},
84         {"ldots",  LM_TK_DOTS, 0},
85         {"left",  LM_TK_LEFT, 0},
86         {"lg",  LM_TK_FUNC, 0},
87         {"lim",  LM_TK_FUNCLIM, 0},
88         {"liminf",  LM_TK_FUNCLIM, 0},
89         {"limits",  LM_TK_LIMIT, 1 },
90         {"limsup",  LM_TK_FUNCLIM, 0},
91         {"ln",  LM_TK_FUNC, 0},
92         {"log",  LM_TK_FUNC, 0},
93         {"mathbb",  LM_TK_FONT, LM_TC_BB},
94         {"mathbf",  LM_TK_FONT, LM_TC_BF},
95         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
96         {"mathfrak",  LM_TK_FONT, LM_TC_EUFRAK},
97         {"mathit",  LM_TK_FONT, LM_TC_IT},
98         {"mathnormal",  LM_TK_FONT, LM_TC_VAR},
99         {"mathrm",  LM_TK_FONT, LM_TC_RM},
100         {"mathsf",  LM_TK_FONT, LM_TC_SF},
101         {"mathtt",  LM_TK_FONT, LM_TC_TT},
102         {"max",  LM_TK_FUNCLIM, 0},
103         //{"mbox",  LM_TK_BOX, 0},
104         {"min",  LM_TK_FUNCLIM, 0},
105         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
106         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
107         {"nonumber",  LM_TK_NONUM, 0},
108         {"not",  LM_TK_NOT, 0},
109         {"over",  LM_TK_OVER, 0},
110         {"overbrace",  LM_TK_DECORATION, 0},
111         {"overleftarrow",  LM_TK_DECORATION, 0},
112         {"overline",  LM_TK_DECORATION, 0},
113         {"overrightarrow",  LM_TK_DECORATION, 0},
114         {"protect",  LM_TK_PROTECT, 0},
115         {"qquad",  LM_TK_SPACE, 5},
116         {"quad",  LM_TK_SPACE, 4},
117         {"right",  LM_TK_RIGHT, 0},
118         {"rm",  LM_TK_OLDFONT, LM_TC_RM},
119         {"root",  LM_TK_ROOT, 0},
120         //{"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
121         //{"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
122         {"sec",  LM_TK_FUNC, 0},
123         {"sin",  LM_TK_FUNC, 0},
124         {"sinh",  LM_TK_FUNC, 0},
125         {"sqrt",  LM_TK_SQRT, 0},
126         {"stackrel",  LM_TK_STACK, 0},
127         {"sup",  LM_TK_FUNCLIM, 0},
128         {"tan",  LM_TK_FUNC, 0},
129         {"tanh",  LM_TK_FUNC, 0},
130         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
131         //{"textstyle",  LM_TK_STY, LM_ST_TEXT},
132         {"tilde",  LM_TK_DECORATION, 0},
133         {"tt",  LM_TK_OLDFONT, LM_TC_TT},
134         {"underbrace",  LM_TK_DECORATION, 0},
135         {"underline",  LM_TK_DECORATION, 0},
136         {"vdots",  LM_TK_DOTS, 0},
137         {"vec",  LM_TK_DECORATION, 0},
138         {"widehat",  LM_TK_DECORATION, 0},
139         {"widetilde",  LM_TK_DECORATION, 0},
140         {"{",  LM_TK_SPECIAL, '{'},
141         {"}",  LM_TK_SPECIAL, '}'}
142 };
143
144
145
146 MathTokenEnum tokenEnum(const string & font)
147 {
148         if (font == "cmr")
149                 return LM_TK_CMR;
150         if (font == "cmsy")
151                 return LM_TK_CMSY;
152         if (font == "cmm")
153                 return LM_TK_CMM;
154         if (font == "cmex")
155                 return LM_TK_CMEX;
156         if (font == "msa")
157                 return LM_TK_MSA;
158         if (font == "msb")
159                 return LM_TK_MSB;
160         return LM_TK_SYM;
161 }
162
163
164 void readSymbols(string const & filename)
165 {
166         LyXLex lex(0, 0);
167         lex.setFile(filename);
168         while (lex.isOK() && lex.next()) {
169                 latexkeys tmp;
170                 tmp.name = lex.getString();
171                 if (lex.next())
172                         tmp.token = tokenEnum(lex.getString());
173                 if (lex.next())
174                         tmp.latex_font_id = lex.getInteger();
175                 if (lex.next())
176                         tmp.id = lex.getInteger();
177                 if (lex.next())
178                         tmp.type = lex.getString();
179                 if (lex.next())
180                         tmp.xmlname = lex.getString();
181                 if (theWordList.find(tmp.name) != theWordList.end())
182                         lyxerr << "readSymbols: token " << tmp.name
183                                << " already exists.\n";
184                 else
185                         theWordList[tmp.name] = tmp;
186         }
187 }
188
189 void initSymbols()
190 {
191         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
192         for (key_type * p = wordlist_array; p != wordlist_array + n; ++p) {
193                 latexkeys tmp;
194                 tmp.name          = p->name;
195                 tmp.token         = p->token;
196                 tmp.id            = p->id;
197                 tmp.latex_font_id = 0;
198                 theWordList[p->name] = tmp;
199         }
200
201         lyxerr[Debug::MATHED] << "reading symbols file\n";
202         string const file = LibFileSearch(string(), "symbols");
203         if (file.empty())
204                 lyxerr << "Could not find symbols file\n";
205         else
206                 readSymbols(file);
207 }
208
209
210 } // namespace anon
211
212
213 latexkeys const * in_word_set(string const & str)
214 {
215         static bool initialized = false;
216
217         if (!initialized) {
218                 initSymbols();
219                 initialized = true;
220         }
221
222         std::map<string, latexkeys>::iterator it = theWordList.find(str);
223         return (it != theWordList.end()) ? &(it->second) : 0;
224 }