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