]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
Added a std::endl directive.
[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 using std::endl;
13
14 bool operator<(const latexkeys & a, const latexkeys & b)
15 {
16         return string(a.name) < string(b.name);
17 }
18
19 namespace {
20
21 // This lists needs to remain sorted all the time!
22
23 struct latexkeys_a {
24         ///
25         char const * name;
26         ///
27         short token;
28         ///
29         unsigned int id;
30 };
31
32 latexkeys_a wordlist_array[] = 
33 {
34         {"!",  LM_TK_SPACE, 0},
35         {"#",  LM_TK_SPECIAL, '#'},
36         {"$",  LM_TK_SPECIAL, '$'},
37         {"%",  LM_TK_SPECIAL, '%'},
38         {"&",  LM_TK_SPECIAL, '&'},
39         {"(",  LM_TK_BEGIN, LM_OT_SIMPLE},
40         {")",  LM_TK_END, LM_OT_SIMPLE},
41         {",",  LM_TK_SPACE, 1},
42         {".",  LM_TK_SPECIAL, '.'},
43         {":",  LM_TK_SPACE, 2},
44         {";",  LM_TK_SPACE, 3},
45         {"Pr",  LM_TK_FUNCLIM, 0},
46         {"[",  LM_TK_BEGIN, LM_OT_EQUATION},
47         {"\\",  LM_TK_NEWLINE, static_cast<unsigned>(-1)}, // -1 needed in mathed_parse_lines!
48         {"]",  LM_TK_END, LM_OT_EQUATION},
49         {"_",  LM_TK_SPECIAL, '_'},
50         {"acute",  LM_TK_DECORATION, LM_acute},
51         {"arccos",  LM_TK_FUNC, 0},
52         {"arcsin",  LM_TK_FUNC, 0},
53         {"arctan",  LM_TK_FUNC, 0},
54         {"arg",  LM_TK_FUNC, 0},
55         {"atop",  LM_TK_ATOP, 0},
56         {"backslash",  LM_TK_SPECIAL, '\\'},
57         {"bar",  LM_TK_DECORATION, LM_bar},
58         {"begin",  LM_TK_BEGIN, 0},
59         {"binom",  LM_TK_BINOM, 0},
60         {"bmod",  LM_TK_FUNC, 0},
61         {"breve",  LM_TK_DECORATION, LM_breve},
62         {"cal",  LM_TK_OLDFONT, LM_TC_CAL},
63         {"cdots",  LM_TK_DOTS, LM_cdots},
64         {"check",  LM_TK_DECORATION, LM_check},
65         {"choose",  LM_TK_CHOOSE, 0},
66         {"cos",  LM_TK_FUNC, 0},
67         {"cosh",  LM_TK_FUNC, 0},
68         {"cot",  LM_TK_FUNC, 0},
69         {"coth",  LM_TK_FUNC, 0},
70         {"csc",  LM_TK_FUNC, 0},
71         {"ddot",  LM_TK_DECORATION, LM_ddot},
72         {"ddots",  LM_TK_DOTS, LM_ddots},
73         {"deg",  LM_TK_FUNC, 0},
74         {"det",  LM_TK_FUNCLIM, 0},
75         {"dim",  LM_TK_FUNC, 0},
76         //{"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
77         {"dot",  LM_TK_DECORATION, LM_dot},
78         {"end",  LM_TK_END, 0},
79         {"exp",  LM_TK_FUNC, 0},
80         {"frac",  LM_TK_FRAC, 0},
81         {"gcd",  LM_TK_FUNCLIM, 0},
82         {"grave",  LM_TK_DECORATION, LM_grave},
83         {"hat",  LM_TK_DECORATION, LM_hat},
84         {"hom",  LM_TK_FUNC, 0},
85         {"inf",  LM_TK_FUNCLIM, 0},
86         {"ker",  LM_TK_FUNC, 0},
87         {"kern",  LM_TK_KERN, 0},
88         {"label",  LM_TK_LABEL, 0},
89         {"ldots",  LM_TK_DOTS, LM_ldots},
90         {"left",  LM_TK_LEFT, 0},
91         {"lg",  LM_TK_FUNC, 0},
92         {"lim",  LM_TK_FUNCLIM, 0},
93         {"liminf",  LM_TK_FUNCLIM, 0},
94         {"limits",  LM_TK_LIMIT, 1 },
95         {"limsup",  LM_TK_FUNCLIM, 0},
96         {"ln",  LM_TK_FUNC, 0},
97         {"log",  LM_TK_FUNC, 0},
98         {"mathbb",  LM_TK_FONT, LM_TC_BB},
99         {"mathbf",  LM_TK_FONT, LM_TC_BF},
100         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
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         {"min",  LM_TK_FUNCLIM, 0},
108         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
109         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
110         {"nonumber",  LM_TK_NONUM, 0},
111         {"not",  LM_TK_NOT, LM_not},
112         {"over",  LM_TK_OVER, 0},
113         {"overbrace",  LM_TK_DECORATION, LM_overbrace},
114         {"overleftarrow",  LM_TK_DECORATION, LM_overleftarrow},
115         {"overline",  LM_TK_DECORATION, LM_overline},
116         {"overrightarrow",  LM_TK_DECORATION, LM_overightarrow},
117         {"protect",  LM_TK_PROTECT, 0},
118         {"qquad",  LM_TK_SPACE, 5},
119         {"quad",  LM_TK_SPACE, 4},
120         {"right",  LM_TK_RIGHT, 0},
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, LM_tilde},
135         {"underbrace",  LM_TK_DECORATION, LM_underbrace},
136         {"underline",  LM_TK_DECORATION, LM_underline},
137         {"vdots",  LM_TK_DOTS, LM_vdots},
138         {"vec",  LM_TK_DECORATION, LM_vec},
139         {"widehat",  LM_TK_DECORATION, LM_widehat},
140         {"widetilde",  LM_TK_DECORATION, LM_widetilde},
141         {"{",  LM_TK_SPECIAL, '{'},
142         {"|",  LM_TK_UNDEF, '|'},
143         {"}",  LM_TK_SPECIAL, '}'},
144         {"", LM_TK_SPECIAL, 0}
145 };
146
147
148 std::vector<latexkeys> wordlist;
149
150 bool initialized = false;
151
152 } // namespace anon
153
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.type   = LMB_NONE;
163                 tmp.latex_font_id = 0;
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 == "cmr")
189                         tmp.token = LM_TK_CMR;
190                 else if (font == "cmsy")
191                         tmp.token = LM_TK_CMSY;
192                 else if (font == "cmm")
193                         tmp.token = LM_TK_CMM;
194                 else if (font == "cmex")
195                         tmp.token = LM_TK_CMEX;
196                 else if (font == "msa")
197                         tmp.token = LM_TK_MSA;
198                 else if (font == "msb")
199                         tmp.token = LM_TK_MSB;
200                 else
201                         tmp.token = LM_TK_SYM;
202
203                 if (type == "mathrel")
204                         tmp.type = LMB_RELATION;
205                 else if (type == "mathbin")
206                         tmp.type = LMB_OPERATOR;
207                 else
208                         tmp.type = LMB_NONE;
209
210                 wordlist.push_back(tmp);
211         }
212
213         std::sort(wordlist.begin(), wordlist.end());
214 }
215
216
217 latexkeys const * in_word_set(string const & str)
218 {
219         if (!initialized) {
220                 lyxerr[Debug::MATHED] << "Reading symbols file\n";
221                 string const file = LibFileSearch(string(), "symbols");
222                 if (file.empty())
223                         lyxerr << "Could not find symbols file" << endl;
224                 else
225                         ReadSymbols(file);
226                 initialized = true;
227         }
228
229         std::vector<latexkeys>::iterator it =
230                 std::find_if(wordlist.begin(), wordlist.end(),
231                              lyx::compare_memfun(&latexkeys::Name, str));
232         return  (it != wordlist.end()) ? &(*it) : 0;
233 }