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