]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
enable direct input of #1...#9; some whitespace changes
[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         {"underbar",  LM_TK_DECORATION, 0},
139         {"underbrace",  LM_TK_DECORATION, 0},
140         {"underline",  LM_TK_DECORATION, 0},
141         {"vdots",  LM_TK_DOTS, 0},
142         {"vec",  LM_TK_DECORATION, 0},
143         {"widehat",  LM_TK_DECORATION, 0},
144         {"widetilde",  LM_TK_DECORATION, 0},
145         {"{",  LM_TK_SPECIAL, '{'},
146         {"}",  LM_TK_SPECIAL, '}'}
147 };
148
149
150
151 MathTokenEnum tokenEnum(const string & font)
152 {
153         if (font == "cmr")
154                 return LM_TK_CMR;
155         if (font == "cmsy")
156                 return LM_TK_CMSY;
157         if (font == "cmm")
158                 return LM_TK_CMM;
159         if (font == "cmex")
160                 return LM_TK_CMEX;
161         if (font == "msa")
162                 return LM_TK_MSA;
163         if (font == "msb")
164                 return LM_TK_MSB;
165         return LM_TK_SYM;
166 }
167
168
169 void readSymbols(string const & filename)
170 {
171         LyXLex lex(0, 0);
172         lex.setFile(filename);
173         while (lex.isOK() && lex.next()) {
174                 latexkeys tmp;
175                 tmp.name = lex.getString();
176                 if (lex.next())
177                         tmp.token = tokenEnum(lex.getString());
178                 if (lex.next())
179                         tmp.latex_font_id = lex.getInteger();
180                 if (lex.next())
181                         tmp.id = lex.getInteger();
182                 if (lex.next())
183                         tmp.type = lex.getString();
184                 if (lex.next())
185                         tmp.xmlname = lex.getString();
186                 if (theWordList.find(tmp.name) != theWordList.end())
187                         lyxerr << "readSymbols: token " << tmp.name
188                                << " already exists.\n";
189                 else
190                         theWordList[tmp.name] = tmp;
191         }
192 }
193
194 void initSymbols()
195 {
196         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
197         for (key_type * 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.latex_font_id = 0;
203                 theWordList[p->name] = tmp;
204         }
205
206         lyxerr[Debug::MATHED] << "reading symbols file\n";
207         string const file = LibFileSearch(string(), "symbols");
208         if (file.empty())
209                 lyxerr << "Could not find symbols file\n";
210         else
211                 readSymbols(file);
212 }
213
214
215 } // namespace anon
216
217
218 latexkeys const * in_word_set(string const & str)
219 {
220         static bool initialized = false;
221
222         if (!initialized) {
223                 initSymbols();
224                 initialized = true;
225         }
226
227         std::map<string, latexkeys>::iterator it = theWordList.find(str);
228         return (it != theWordList.end()) ? &(it->second) : 0;
229 }