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