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