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