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