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