]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlMath.C
hopefully fix tex2lyx linking.
[lyx.git] / src / frontends / controllers / ControlMath.C
1 /**
2  * \file ControlMath.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlMath.h"
14 #include "debug.h"
15 #include "funcrequest.h"
16
17 #include "support/lyxalgo.h" // sorted
18 #include "support/lstrings.h"
19 #include "support/filetools.h"
20
21 #include <functional>
22
23 using std::string;
24
25 namespace lyx {
26
27 using support::compare;
28 using support::libFileSearch;
29 using support::subst;
30
31 namespace frontend {
32
33 ControlMath::ControlMath(Dialog & dialog)
34         : Dialog::Controller(dialog)
35 {}
36
37
38 void ControlMath::dispatchFunc(kb_action action, string const & arg) const
39 {
40         kernel().dispatch(FuncRequest(action, arg));
41 }
42
43
44 void ControlMath::dispatchInsert(string const & name) const
45 {
46         dispatchFunc(LFUN_MATH_INSERT, '\\' + name);
47 }
48
49
50 void ControlMath::dispatchSubscript() const
51 {
52         dispatchFunc(LFUN_MATH_INSERT, "_");
53 }
54
55
56 void ControlMath::dispatchSuperscript() const
57 {
58         dispatchFunc(LFUN_MATH_INSERT, "^");
59 }
60
61
62 void ControlMath::dispatchCubeRoot() const
63 {
64         dispatchFunc(LFUN_MATH_INSERT, "\\root");
65         dispatchFunc(LFUN_SELF_INSERT, "3");
66         dispatchFunc(LFUN_CHAR_FORWARD);
67 }
68
69
70 void ControlMath::dispatchMatrix(string const & str) const
71 {
72         dispatchFunc(LFUN_MATH_MATRIX, str);
73 }
74
75
76 void ControlMath::dispatchDelim(string const & str) const
77 {
78         dispatchFunc(LFUN_MATH_DELIM, str);
79 }
80
81
82 void ControlMath::dispatchBigDelim(string const & str) const
83 {
84         dispatchFunc(LFUN_MATH_BIGDELIM, str);
85 }
86
87
88 void ControlMath::dispatchToggleDisplay() const
89 {
90         dispatchFunc(LFUN_MATH_DISPLAY);
91 }
92
93
94 void ControlMath::showDialog(string const & name) const
95 {
96         dispatchFunc(LFUN_DIALOG_SHOW, name);
97 }
98
99
100 char const * function_names[] = {
101         "arccos", "arcsin", "arctan", "arg", "bmod",
102         "cos", "cosh", "cot", "coth", "csc", "deg",
103         "det", "dim", "exp", "gcd", "hom", "inf", "ker",
104         "lg", "lim", "liminf", "limsup", "ln", "log",
105         "max", "min", "sec", "sin", "sinh", "sup",
106         "tan", "tanh", "Pr", ""
107 };
108
109 int const nr_function_names = sizeof(function_names) / sizeof(char const *) - 1;
110
111 char const * latex_dots[] = {
112         "ldots", "cdots", "vdots", "ddots", ""
113 };
114
115 int const nr_latex_dots = sizeof(latex_dots) / sizeof(char const *) - 1;
116
117 char const * latex_deco[] = {
118         "widehat", "widetilde", "overbrace", "overleftarrow", "overrightarrow",
119         "overline", "underbrace", "underline", "underleftarrow", "underrightarrow",
120         "underleftrightarrow", "overleftrightarrow",
121         "hat", "acute", "bar", "dot",
122         "check", "grave", "vec", "ddot",
123         "breve", "tilde", "overset", "underset", ""
124 };
125
126 int const nr_latex_deco = sizeof(latex_deco) / sizeof(char const *) - 1;
127
128 char const * latex_arrow[] = {
129         "downarrow", "leftarrow", "Downarrow", "Leftarrow",
130         "hookleftarrow", "rightarrow", "uparrow", "Rightarrow", "Uparrow",
131         "hookrightarrow", "updownarrow", "Leftrightarrow", "leftharpoonup",
132         "rightharpoonup", "rightleftharpoons", "leftrightarrow", "Updownarrow",
133         "leftharpoondown", "rightharpoondown", "mapsto",
134         "Longleftarrow", "Longrightarrow", "Longleftrightarrow",
135         "longleftrightarrow", "longleftarrow", "longrightarrow", "longmapsto",
136         "nwarrow", "nearrow", "swarrow", "searrow",  "",
137 };
138
139 int const nr_latex_arrow = sizeof(latex_arrow) / sizeof(char const *);
140
141 char const * latex_bop[] = {
142         "pm", "cap", "diamond", "oplus",
143         "mp", "cup", "bigtriangleup", "ominus",
144         "times", "uplus", "bigtriangledown", "otimes",
145         "div", "sqcap", "triangleright", "oslash",
146         "cdot", "sqcup", "triangleleft", "odot",
147         "star", "vee", "amalg", "bigcirc",
148         "setminus", "wedge", "dagger", "circ",
149         "bullet", "wr", "ddagger", ""
150 };
151
152 int const nr_latex_bop = sizeof(latex_bop) / sizeof(char const *);
153
154 char const * latex_brel[] = {
155         "leq", "geq", "equiv", "models",
156         "prec", "succ", "sim", "perp",
157         "preceq", "succeq", "simeq", "mid",
158         "ll", "gg", "asymp", "parallel",
159         "subset", "supset", "approx", "smile",
160         "subseteq", "supseteq", "cong", "frown",
161         "sqsubseteq", "sqsupseteq", "doteq", "neq",
162         "in", "ni", "propto", "notin",
163         "vdash", "dashv", "bowtie", ""
164 };
165
166 int const nr_latex_brel = sizeof(latex_brel) / sizeof(char const *);
167
168 char const * latex_greek[] = {
169         "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi",
170         "Sigma", "Upsilon", "Phi", "Psi", "Omega",
171         "alpha", "beta", "gamma", "delta", "epsilon", "varepsilon", "zeta",
172         "eta", "theta", "vartheta", "iota", "kappa", "lambda", "mu",
173         "nu", "xi", "pi", "varpi", "rho", "sigma", "varsigma",
174         "tau", "upsilon", "phi", "varphi", "chi", "psi", "omega", ""
175 };
176
177 int const nr_latex_greek = sizeof(latex_greek) / sizeof(char const *);
178
179 char const * latex_misc[] = {
180         "nabla", "partial", "infty", "prime", "ell",
181         "emptyset", "exists", "forall", "imath",  "jmath",
182         "Re", "Im", "aleph", "wp", "hbar",
183         "angle", "top", "bot", "Vert", "neg",
184         "flat", "natural", "sharp", "surd", "triangle",
185         "diamondsuit", "heartsuit", "clubsuit", "spadesuit",
186         "textrm \\AA", "textrm \\O", "mathcircumflex", "_",
187         "mathrm T",
188         "mathbb N", "mathbb Z", "mathbb Q",
189         "mathbb R", "mathbb C", "mathbb H",
190         "mathcal F", "mathcal L",
191         "mathcal H", "mathcal O",
192         "phantom", "vphantom", "hphantom", ""
193 };
194
195 int const nr_latex_misc = sizeof(latex_misc) / sizeof(char const *);
196
197 char const * latex_varsz[] = {
198         "sum", "int", "intop", "iint", "iintop", "iiint", "iiintop",
199         "iiiint", "iiiintop", "dotsint", "dotsintop",
200         "oint", "ointop", "oiint", "oiintop", "ointctrclockwise",
201         "ointctrclockwiseop", "ointclockwise", "ointclockwiseop",
202         "sqint", "sqintop", "sqiint", "sqiintop",
203         "prod", "coprod", "bigsqcup",
204         "bigotimes", "bigodot", "bigoplus",
205         "bigcap", "bigcup", "biguplus",
206         "bigvee", "bigwedge", ""
207 };
208
209 int const nr_latex_varsz = sizeof(latex_varsz) / sizeof(char const *);
210
211 char const * latex_ams_misc[] = {
212         "digamma", "varkappa", "beth", "daleth", "gimel",
213         "ulcorner", "urcorner", "llcorner", "lrcorner",
214         "hbar", "hslash", "vartriangle",
215         "triangledown", "square", "lozenge",
216         "circledS", "angle", "measuredangle",
217         "nexists", "mho", "Finv",
218         "Game", "Bbbk", "backprime",
219         "varnothing", "blacktriangle", "blacktriangledown",
220         "blacksquare", "blacklozenge", "bigstar",
221         "sphericalangle", "complement", "eth",
222         "diagup", "diagdown", ""
223 };
224
225 int const nr_latex_ams_misc = sizeof(latex_ams_misc) / sizeof(char const *);
226
227 char const * latex_ams_arrows[] = {
228         "dashrightarrow", "dashleftarrow", "leftleftarrows",
229         "leftrightarrows", "Lleftarrow", "twoheadleftarrow",
230         "leftarrowtail", "looparrowleft", "leftrightharpoons",
231         "curvearrowleft", "circlearrowleft", "Lsh",
232         "upuparrows", "upharpoonleft", "downharpoonleft",
233         "multimap", "leftrightsquigarrow", "rightrightarrows",
234         "rightleftarrows", "rightrightarrows", "rightleftarrows",
235         "twoheadrightarrow", "rightarrowtail", "looparrowright",
236         "rightleftharpoons", "curvearrowright", "circlearrowright",
237         "Rsh", "downdownarrows", "upharpoonright",
238         "downharpoonright", "rightsquigarrow",
239         "nleftarrow", "nrightarrow", "nLeftarrow",
240         "nRightarrow", "nleftrightarrow", "nLeftrightarrow", ""
241 };
242
243 int const nr_latex_ams_arrows = sizeof(latex_ams_arrows) / sizeof(char const *);
244
245 char const * latex_ams_rel[] = {
246         "leqq", "leqslant", "eqslantless", "eqslantgtr", "gtrsim", "gtrapprox",
247         "lesssim", "lessapprox", "approxeq", "gtrdot", "ggg", "gtrless",
248         "lessdot", "lll", "lessgtr", "gtreqless", "gtreqqless", "eqcirc",
249         "lesseqgtr", "lesseqqgtr", "doteqdot", "circeq", "triangleq", "thicksim",
250         "risingdotseq", "fallingdotseq", "backsim", "thickapprox", "supseteqq", "Supset",
251         "backsimeq", "subseteqq", "Subset", "sqsupset", "succcurlyeq", "curlyeqsucc",
252         "sqsubset", "preccurlyeq", "curlyeqprec", "succsim", "succapprox", "vartriangleright",
253         "precsim", "precapprox", "vartriangleleft", "trianglerighteq", "Vdash", "shortmid",
254         "trianglelefteq", "vDash", "Vvdash", "shortparallel", "between", "pitchfork",
255         "smallsmile", "smallfrown", "bumpeq", "varpropto", "blacktriangleleft", "therefore",
256         "Bumpeq", "geqq", "geqslant", "backepsilon", "blacktriangleright", "because", ""
257 };
258
259 int const nr_latex_ams_rel = sizeof(latex_ams_rel) / sizeof(char const *);
260
261 char const * latex_ams_nrel[] = {
262         "nless", "nleq", "nleqslant", "ngeqslant", "ngeqq", "gneq",
263         "nleqq", "lneq", "lneqq", "gneqq", "gvertneqq", "gnsim",
264         "lvertneqq", "lnsim", "lnapprox", "gnapprox", "nsucc", "nsucceq",
265         "nprec", "npreceq", "precnsim","succnsim", "succnapprox", "ncong",
266         "precnapprox", "nsim", "nshortmid", "nshortparallel", "nparallel", "nvDash",
267
268         "nmid", "nvdash", "nvDash", "nVDash", "ntriangleright", "ntrianglerighteq",
269         "ntriangleleft", "ntrianglelefteq", "nsubseteq", "nsupseteq", "nsupseteqq", "supsetneq",
270         "subsetneq", "varsubsetneq", "subsetneqq", "varsupsetneq", "supsetneqq", "varsupsetneqq",
271         "varsubsetneqq", "ngtr", "ngeq", "", "", ""
272 };
273
274 int const nr_latex_ams_nrel = sizeof(latex_ams_nrel) / sizeof(char const *);
275
276
277 char const * latex_ams_ops[] = {
278         "dotplus", "smallsetminus", "Cap",
279         "Cup", "barwedge", "veebar",
280         "doublebarwedge", "boxminus", "boxtimes",
281         "boxdot", "boxplus", "divideontimes",
282         "ltimes", "rtimes", "leftthreetimes",
283         "rightthreetimes", "curlywedge", "curlyvee",
284         "circleddash", "circledast", "circledcirc",
285         "centerdot", "intercal", ""
286 };
287
288 int const nr_latex_ams_ops = sizeof(latex_ams_ops) / sizeof(char const *);
289
290
291 namespace {
292
293 struct XPMmap {
294         char const * key;
295         char const * value;
296 };
297
298
299 bool operator<(XPMmap const & lhs, XPMmap const & rhs)
300 {
301                 return compare(lhs.key, rhs.key) < 0;
302 }
303
304
305 class CompareKey : public std::unary_function<XPMmap, bool> {
306 public:
307         CompareKey(string const & name) : name_(name) {}
308         bool operator()(XPMmap const & other) const {
309                 return other.key == name_;
310         }
311 private:
312         string const name_;
313 };
314
315
316 XPMmap sorted_xpm_map[] = {
317         { "Bumpeq", "bumpeq2" },
318         { "Cap", "cap2" },
319         { "Cup", "cup2" },
320         { "Delta", "delta2" },
321         { "Downarrow", "downarrow2" },
322         { "Gamma", "gamma2" },
323         { "Lambda", "lambda2" },
324         { "Leftarrow", "leftarrow2" },
325         { "Leftrightarrow", "leftrightarrow2" },
326         { "Longleftarrow", "longleftarrow2" },
327         { "Longleftrightarrow", "longleftrightarrow2" },
328         { "Longrightarrow", "longrightarrow2" },
329         { "Omega", "omega2" },
330         { "Phi", "phi2" },
331         { "Pi", "pi2" },
332         { "Psi", "psi2" },
333         { "Rightarrow", "rightarrow2" },
334         { "Sigma", "sigma2" },
335         { "Subset", "subset2" },
336         { "Supset", "supset2" },
337         { "Theta", "theta2" },
338         { "Uparrow", "uparrow2" },
339         { "Updownarrow", "updownarrow2" },
340         { "Upsilon", "upsilon2" },
341         { "Vdash", "vdash3" },
342         { "Xi", "xi2" },
343         { "nLeftarrow", "nleftarrow2" },
344         { "nLeftrightarrow", "nleftrightarrow2" },
345         { "nRightarrow", "nrightarrow2" },
346         { "nVDash", "nvdash3" },
347         { "nvDash", "nvdash2" },
348         { "textrm \\AA", "textrm_AA"},
349         { "textrm \\O", "textrm_Oe"},
350         { "vDash", "vdash2" }
351 };
352
353 size_t const nr_sorted_xpm_map = sizeof(sorted_xpm_map) / sizeof(XPMmap);
354
355 } // namespace anon
356
357
358 string const find_xpm(string const & name)
359 {
360         XPMmap const * const begin = sorted_xpm_map;
361         XPMmap const * const end = begin + nr_sorted_xpm_map;
362         BOOST_ASSERT(sorted(begin, end));
363
364         XPMmap const * const it =
365                 std::find_if(begin, end, CompareKey(name));
366
367         string xpm_name;
368         if (it != end)
369                 xpm_name = it->value;
370         else {
371                 xpm_name = subst(name, "_", "underscore");
372                 xpm_name = subst(xpm_name, ' ', '_');
373
374                 // This way we can have "math-delim { }" on the toolbar.
375                 xpm_name = subst(xpm_name, "(", "lparen");
376                 xpm_name = subst(xpm_name, ")", "rparen");
377                 xpm_name = subst(xpm_name, "[", "lbracket");
378                 xpm_name = subst(xpm_name, "]", "rbracket");
379                 xpm_name = subst(xpm_name, "{", "lbrace");
380                 xpm_name = subst(xpm_name, "}", "rbrace");
381                 xpm_name = subst(xpm_name, "|", "bars");
382         }
383
384         lyxerr[Debug::GUI] << "find_xpm(" << name << ")\n"
385                            << "Looking for math XPM called \""
386                            << xpm_name << '"' << std::endl;
387
388         return libFileSearch("images/math/", xpm_name, "xpm");
389 }
390
391 } // namespace frontend
392 } // namespace lyx