]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
8004a2680e6b3190c933ec49a59489d146ad4cc7
[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 #include "support/lyxfunctional.h"
8
9 #include <map>
10 #include <algorithm>
11
12
13 namespace {
14
15 struct latexkeys_a {
16         ///
17         char const * name;
18         ///
19         short token;
20         ///
21         unsigned int id;
22 };
23
24 latexkeys_a wordlist_array[] = 
25 {
26         {"!",  LM_TK_SPACE, 0},
27         {"#",  LM_TK_SPECIAL, '#'},
28         {"$",  LM_TK_SPECIAL, '$'},
29         {"%",  LM_TK_SPECIAL, '%'},
30         {"&",  LM_TK_SPECIAL, '&'},
31         {"(",  LM_TK_BEGIN, LM_OT_SIMPLE},
32         {")",  LM_TK_END, LM_OT_SIMPLE},
33         {",",  LM_TK_SPACE, 1},
34         {".",  LM_TK_SPECIAL, '.'},
35         {":",  LM_TK_SPACE, 2},
36         {";",  LM_TK_SPACE, 3},
37         {"Pr",  LM_TK_FUNCLIM, 0},
38         {"[",  LM_TK_BEGIN, LM_OT_EQUATION},
39         {"\\",  LM_TK_NEWLINE, static_cast<unsigned>(-1)}, // -1 needed in mathed_parse_lines!
40         {"]",  LM_TK_END, LM_OT_EQUATION},
41         {"_",  LM_TK_SPECIAL, '_'},
42         {"acute",  LM_TK_DECORATION, LM_acute},
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         {"backslash",  LM_TK_SPECIAL, '\\'},
49         {"bar",  LM_TK_DECORATION, LM_bar},
50         {"begin",  LM_TK_BEGIN, 0},
51         {"binom",  LM_TK_BINOM, 0},
52         {"bmod",  LM_TK_FUNC, 0},
53         {"breve",  LM_TK_DECORATION, LM_breve},
54         {"cal",  LM_TK_OLDFONT, LM_TC_CAL},
55         {"cdots",  LM_TK_DOTS, LM_cdots},
56         {"check",  LM_TK_DECORATION, LM_check},
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, LM_ddot},
64         {"ddots",  LM_TK_DOTS, LM_ddots},
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, LM_dot},
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, LM_grave},
75         {"hat",  LM_TK_DECORATION, LM_hat},
76         {"hom",  LM_TK_FUNC, 0},
77         {"inf",  LM_TK_FUNCLIM, 0},
78         {"ker",  LM_TK_FUNC, 0},
79         {"kern",  LM_TK_KERN, 0},
80         {"label",  LM_TK_LABEL, 0},
81         {"ldots",  LM_TK_DOTS, LM_ldots},
82         {"left",  LM_TK_LEFT, 0},
83         {"lg",  LM_TK_FUNC, 0},
84         {"lim",  LM_TK_FUNCLIM, 0},
85         {"liminf",  LM_TK_FUNCLIM, 0},
86         {"limits",  LM_TK_LIMIT, 1 },
87         {"limsup",  LM_TK_FUNCLIM, 0},
88         {"ln",  LM_TK_FUNC, 0},
89         {"log",  LM_TK_FUNC, 0},
90         {"mathbb",  LM_TK_FONT, LM_TC_BB},
91         {"mathbf",  LM_TK_FONT, LM_TC_BF},
92         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
93         {"mathit",  LM_TK_FONT, LM_TC_IT},
94         {"mathnormal",  LM_TK_FONT, LM_TC_VAR},
95         {"mathrm",  LM_TK_FONT, LM_TC_RM},
96         {"mathsf",  LM_TK_FONT, LM_TC_SF},
97         {"mathtt",  LM_TK_FONT, LM_TC_TT},
98         {"max",  LM_TK_FUNCLIM, 0},
99         {"min",  LM_TK_FUNCLIM, 0},
100         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
101         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
102         {"nonumber",  LM_TK_NONUM, 0},
103         {"not",  LM_TK_NOT, LM_not},
104         {"over",  LM_TK_OVER, 0},
105         {"overbrace",  LM_TK_DECORATION, LM_overbrace},
106         {"overleftarrow",  LM_TK_DECORATION, LM_overleftarrow},
107         {"overline",  LM_TK_DECORATION, LM_overline},
108         {"overrightarrow",  LM_TK_DECORATION, LM_overightarrow},
109         {"protect",  LM_TK_PROTECT, 0},
110         {"qquad",  LM_TK_SPACE, 5},
111         {"quad",  LM_TK_SPACE, 4},
112         {"right",  LM_TK_RIGHT, 0},
113         {"root",  LM_TK_ROOT, 0},
114         //{"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
115         //{"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
116         {"sec",  LM_TK_FUNC, 0},
117         {"sin",  LM_TK_FUNC, 0},
118         {"sinh",  LM_TK_FUNC, 0},
119         {"sqrt",  LM_TK_SQRT, 0},
120         {"stackrel",  LM_TK_STACK, 0},
121         {"sup",  LM_TK_FUNCLIM, 0},
122         {"tan",  LM_TK_FUNC, 0},
123         {"tanh",  LM_TK_FUNC, 0},
124         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
125         //{"textstyle",  LM_TK_STY, LM_ST_TEXT},
126         {"tilde",  LM_TK_DECORATION, LM_tilde},
127         {"underbrace",  LM_TK_DECORATION, LM_underbrace},
128         {"underline",  LM_TK_DECORATION, LM_underline},
129         {"vdots",  LM_TK_DOTS, LM_vdots},
130         {"vec",  LM_TK_DECORATION, LM_vec},
131         {"widehat",  LM_TK_DECORATION, LM_widehat},
132         {"widetilde",  LM_TK_DECORATION, LM_widetilde},
133         {"{",  LM_TK_SPECIAL, '{'},
134         {"|",  LM_TK_UNDEF, '|'},
135         {"}",  LM_TK_SPECIAL, '}'},
136 };
137
138
139 std::map<string, latexkeys> wordlist;
140
141
142
143 MathTokenEnum tokenEnum(const string & font)
144 {
145         if (font == "cmr")
146                 return LM_TK_CMR;
147         if (font == "cmsy")
148                 return LM_TK_CMSY;
149         if (font == "cmm")
150                 return LM_TK_CMM;
151         if (font == "cmex")
152                 return LM_TK_CMEX;
153         if (font == "msa")
154                 return LM_TK_MSA;
155         if (font == "msb")
156                 return LM_TK_MSB;
157         return LM_TK_SYM;
158 }
159
160 MathSymbolTypes symbolType(const string & type)
161 {
162         if (type == "mathrel")
163                 return LMB_RELATION;
164         if (type == "mathbin")
165                 return LMB_OPERATOR;    
166         return LMB_NONE;
167 }
168
169
170 } // namespace anon
171
172
173 void ReadSymbols(string const & filename)
174 {
175         LyXLex lex(0, 0);
176         lex.setFile(filename);
177         while (lex.isOK() && lex.next()) {
178                 latexkeys tmp;
179                 tmp.name = lex.getString();
180                 if (lex.next())
181                         tmp.token = tokenEnum(lex.getString());
182                 if (lex.next())
183                         tmp.latex_font_id = lex.getInteger();
184                 if (lex.next())
185                         tmp.id = lex.getInteger();
186                 if (lex.next())
187                         tmp.type = symbolType(lex.getString());
188                 wordlist[tmp.name] = tmp;
189         }
190 }
191
192
193 void initSymbols()
194 {
195         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
196         for (latexkeys_a * p = wordlist_array; p != wordlist_array + n; ++p) {
197                 latexkeys tmp;
198                 tmp.name          = p->name;
199                 tmp.token         = p->token;
200                 tmp.id            = p->id;
201                 tmp.type          = LMB_NONE;
202                 tmp.latex_font_id = 0;
203                 wordlist[p->name] = tmp;
204         }
205
206         lyxerr[Debug::MATHED] << "Reading symbols file\n";
207         string const file = LibFileSearch(string(), "symbols");
208         if (file.empty())
209                 lyxerr << "Could not find symbols file\n";
210         else
211                 ReadSymbols(file);
212 }
213
214
215 latexkeys const * in_word_set(string const & str)
216 {
217         static bool initialized = false;
218
219         if (!initialized) {
220                 initSymbols();
221                 initialized = true;
222         }
223
224         std::map<string, latexkeys>::iterator it = wordlist.find(str);
225         return (it != wordlist.end()) ? &(it->second) : 0;
226 }