]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
Implement variable size bigops
[lyx.git] / src / mathed / MathFactory.cpp
1 /**
2  * \file MathFactory.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathFactory.h"
14
15 #include "InsetMathAMSArray.h"
16 #include "InsetMathArray.h"
17 #include "InsetMathBoldSymbol.h"
18 #include "InsetMathBox.h"
19 #include "InsetMathCancel.h"
20 #include "InsetMathCancelto.h"
21 #include "InsetMathCases.h"
22 #include "InsetMathClass.h"
23 #include "InsetMathColor.h"
24 #include "InsetMathDecoration.h"
25 #include "InsetMathDots.h"
26 #include "InsetMathEnsureMath.h"
27 #include "InsetMathFont.h"
28 #include "InsetMathFontOld.h"
29 #include "InsetMathFrac.h"
30 #include "InsetMathKern.h"
31 #include "InsetMathLefteqn.h"
32 #include "InsetMathOverset.h"
33 #include "InsetMathPhantom.h"
34 #include "InsetMathRef.h"
35 #include "InsetMathRoot.h"
36 #include "InsetMathSideset.h"
37 #include "InsetMathSize.h"
38 #include "InsetMathSpace.h"
39 #include "InsetMathSpecialChar.h"
40 #include "InsetMathSplit.h"
41 #include "InsetMathSqrt.h"
42 #include "InsetMathStackrel.h"
43 #include "InsetMathSubstack.h"
44 #include "InsetMathSymbol.h"
45 #include "InsetMathTabular.h"
46 #include "InsetMathUnderset.h"
47 #include "InsetMathUnknown.h"
48 #include "InsetMathHull.h"
49 #include "InsetMathXArrow.h"
50 #include "InsetMathXYMatrix.h"
51 #include "InsetMathDiagram.h"
52 #include "MacroTable.h"
53 #include "InsetMathMacro.h"
54 #include "InsetMathMacroArgument.h"
55 #include "MathParser.h"
56 #include "MathStream.h"
57 #include "MathSupport.h"
58
59 #include "insets/InsetCommand.h"
60 #include "insets/InsetSpace.h"
61
62 #include "support/debug.h"
63 #include "support/docstream.h"
64 #include "support/FileName.h"
65 #include "support/filetools.h" // LibFileSearch
66 #include "support/lstrings.h"
67 #include "support/textutils.h"
68
69 #include "frontends/FontLoader.h"
70
71 #include "Buffer.h"
72 #include "BufferParams.h"
73 #include "Encoding.h"
74 #include "LyX.h" // use_gui
75 #include "OutputParams.h"
76
77 #include <iomanip>
78
79 using namespace std;
80 using namespace lyx::support;
81
82 namespace lyx {
83
84 bool has_math_fonts;
85
86
87 namespace {
88
89 MathWordList theMathWordList;
90
91
92 bool isMathFontAvailable(string & name)
93 {
94         if (!use_gui)
95                 return false;
96
97         FontInfo f;
98         augmentFont(f, name);
99
100         // Do we have the font proper?
101         if (theFontLoader().available(f))
102                 return true;
103
104         // can we fake it?
105         if (name == "eufrak") {
106                 name = "lyxfakefrak";
107                 return true;
108         }
109
110         LYXERR(Debug::MATHED,
111                 "font " << name << " not available and I can't fake it");
112         return false;
113 }
114
115
116 bool canBeDisplayed(char_type c)
117 {
118         if (!use_gui)
119                 return true;
120         return theFontLoader().canBeDisplayed(c);
121 }
122
123
124 bool isUnicodeSymbolAvailable(docstring const & name, char_type & c)
125 {
126         docstring cmd(from_ascii("\\") + name);
127         bool is_combining;
128         bool termination;
129         c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
130                                         is_combining, termination);
131         if (c == 0 && name == "varOmega") {
132                 // fallback for bug 7954, unicodesymbols does not list
133                 // \\varOmega because of requirements, but this might change
134                 cmd = from_ascii("\\mathit{\\Omega}");
135                 c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
136                                                 is_combining, termination);
137         }
138         return c != 0 && !is_combining;
139 }
140
141
142 void initSymbols()
143 {
144         FileName const filename = libFileSearch(string(), "symbols");
145         LYXERR(Debug::MATHED, "read symbols from " << filename);
146         if (filename.empty()) {
147                 lyxerr << "Could not find symbols file" << endl;
148                 return;
149         }
150
151         ifstream fs(filename.toFilesystemEncoding().c_str());
152         // limit the size of strings we read to avoid memory problems
153         fs >> setw(65636);
154         string line;
155         bool skip = false;
156         while (getline(fs, line)) {
157                 int charid     = 0;
158                 int dsp_charid = 0;
159                 int fallbackid = 0;
160                 if (line.empty() || line[0] == '#')
161                         continue;
162
163                 // special case of iffont/else/endif
164                 if (line.size() >= 7 && line.substr(0, 6) == "iffont") {
165                         istringstream is(line);
166                         // limit the size of strings we read to avoid memory problems
167                         is >> setw(65636);
168                         string tmp;
169                         is >> tmp;
170                         is >> tmp;
171                         skip = !isMathFontAvailable(tmp);
172                         continue;
173                 } else if (line.size() >= 4 && line.substr(0, 4) == "else") {
174                         skip = !skip;
175                         continue;
176                 } else if (line.size() >= 5 && line.substr(0, 5) == "endif") {
177                         skip = false;
178                         continue;
179                 } else if (skip)
180                         continue;
181
182                 // special case of pre-defined macros
183                 if (line.size() > 8 && line.substr(0, 5) == "\\def\\") {
184                         //lyxerr << "macro definition: '" << line << '\'' << endl;
185                         // syntax: Either
186                         // \def\macroname{definition}
187                         // or
188                         // \def\macroname{definition} requires
189                         // or
190                         // \def\macroname{definition} extra htmlname xmlname requires
191                         istringstream is(line);
192                         string macro;
193                         string required;
194                         string extra;
195                         string htmlname;
196                         string xmlname;
197                         bool hidden = false;
198                         is >> setw(65536) >> macro >> required;
199                         if ((is >> htmlname >> xmlname)) {
200                                 extra = required;
201                                 if (!(is >> required))
202                                         required = "";
203                         } else
204                                 htmlname = xmlname = "";
205                         MacroTable::iterator it = MacroTable::globalMacros().insert(
206                                         0, from_utf8(macro));
207                         if (!extra.empty() || !htmlname.empty() || !xmlname.empty() || !required.empty()) {
208                                 MathWordList::iterator wit = theMathWordList.find(it->first);
209                                 if (wit != theMathWordList.end())
210                                         LYXERR(Debug::MATHED, "readSymbols: inset "
211                                                 << to_utf8(it->first) << " already exists.");
212                                 else {
213                                         latexkeys tmp;
214                                         tmp.inset = "macro";
215                                         tmp.name = it->first;
216                                         tmp.extra = from_utf8(extra);
217                                         tmp.htmlname = from_utf8(htmlname);
218                                         tmp.xmlname = from_utf8(xmlname);
219                                         if (required == "hiddensymbol") {
220                                                 required = "";
221                                                 tmp.hidden = hidden = true;
222                                         } else
223                                                 tmp.required = required;
224                                         theMathWordList[it->first] = tmp;
225                                         wit = theMathWordList.find(it->first);
226                                         it->second.setSymbol(&(wit->second));
227                                 }
228                         }
229                         // If you change the following output, please adjust
230                         // development/tools/generate_symbols_images.py.
231                         LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
232                             << "  inset: macro"
233                             << "  draw: 0"
234                             << "  extra: " << extra
235                             << "  html: " << htmlname
236                             << "  xml: " << xmlname
237                                 << "  requires: " << required
238                                 << "  hidden: " << hidden << '\'');
239                         continue;
240                 }
241
242                 idocstringstream is(from_utf8(line));
243                 latexkeys tmp;
244                 docstring help;
245                 is >> tmp.name >> help;
246                 tmp.inset = to_ascii(help);
247                 if (isFontName(tmp.inset)) {
248                         is >> help >> fallbackid >> tmp.extra >> tmp.htmlname >> tmp.xmlname;
249                         docstring cid, dsp_cid;
250                         idocstringstream is2(subst(help, '|', ' '));
251                         is2 >> charid >> dsp_charid;
252                 } else
253                         is >> tmp.extra;
254                 // requires is optional
255                 if (is) {
256                         if ((is >> help)) {
257                                 // backward compatibility
258                                 if (help == "esintoramsmath")
259                                         tmp.required = "esint|amsmath";
260                                 else
261                                         tmp.required = to_ascii(help);
262                         }
263                 } else {
264                         LYXERR(Debug::MATHED, "skipping line '" << line << "'\n"
265                                 << to_utf8(tmp.name) << ' ' << tmp.inset << ' '
266                                 << to_utf8(tmp.extra));
267                         continue;
268                 }
269
270                 if (isFontName(tmp.inset)) {
271                         // tmp.inset _is_ the fontname here.
272                         // create fallbacks if necessary
273
274                         // store requirements as long as we can
275                         if (tmp.required.empty()) {
276                                 if (tmp.inset == "msa" || tmp.inset == "msb")
277                                         tmp.required = "amssymb";
278                                 else if (tmp.inset == "wasy")
279                                         tmp.required = "wasysym";
280                                 else if (tmp.inset == "mathscr")
281                                         tmp.required = "mathrsfs";
282                                 else if (tmp.inset == "mathds")
283                                         tmp.required = "dsfont";
284                         }
285
286                         // symbol font is not available sometimes
287                         string symbol_font = "lyxsymbol";
288                         char_type unicodesymbol = 0;
289
290                         if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
291                                 LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
292                                 tmp.draw = tmp.name;
293                         } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
294                                 LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
295                                 tmp.draw.push_back(char_type(charid));
296                                 if (dsp_charid && canBeDisplayed(dsp_charid)) {
297                                         LYXERR(Debug::MATHED, "large symbol available for " << to_utf8(tmp.name));
298                                         tmp.dsp_draw.push_back(char_type(dsp_charid));
299                                 }
300                         } else if (fallbackid && isMathFontAvailable(symbol_font) &&
301                                    canBeDisplayed(fallbackid)) {
302                                 if (tmp.inset == "cmex")
303                                         tmp.inset = "lyxsymbol";
304                                 else
305                                         tmp.inset = "lyxboldsymbol";
306                                 LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
307                                 tmp.draw.push_back(char_type(fallbackid));
308                         } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
309                                 LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
310                                 tmp.inset = "mathnormal";
311                                 tmp.draw.push_back(unicodesymbol);
312                         } else {
313                                 LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
314                                 tmp.draw = tmp.name;
315                                 tmp.inset = "lyxtex";
316                         }
317                 } else {
318                         // it's a proper inset
319                         LYXERR(Debug::MATHED, "inset " << tmp.inset
320                                               << " used for " << to_utf8(tmp.name));
321                 }
322
323                 if (tmp.required == "hiddensymbol")
324                 {
325                         tmp.required = "";
326                         tmp.hidden = true;
327                 }
328
329                 if (theMathWordList.find(tmp.name) != theMathWordList.end())
330                         LYXERR(Debug::MATHED, "readSymbols: inset " << to_utf8(tmp.name)
331                                 << " already exists.");
332                 else
333                         theMathWordList[tmp.name] = tmp;
334
335                 // If you change the following output, please adjust
336                 // development/tools/generate_symbols_images.py.
337                 LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
338                         << "  inset: " << tmp.inset
339                         << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
340                         << "  extra: " << to_utf8(tmp.extra)
341                         << "  html: " << to_utf8(tmp.htmlname)
342                         << "  xml: " << to_utf8(tmp.xmlname)
343                         << "  requires: " << tmp.required
344                         << "  hidden: " << tmp.hidden << '\'');
345         }
346         string tmp = "cmm";
347         string tmp2 = "cmsy";
348         has_math_fonts = isMathFontAvailable(tmp) && isMathFontAvailable(tmp2);
349 }
350
351
352 bool isSpecialChar(docstring const & name)
353 {
354         if (name.size() != 1)
355                 return  name == "textasciicircum" || name == "mathcircumflex" ||
356                         name == "textasciitilde"  || name == "textbackslash";
357
358         char_type const c = name.at(0);
359         return  c == '{' || c == '}' || c == '&' || c == '$' ||
360                 c == '#' || c == '%' || c == '_';
361 }
362
363
364 } // namespace
365
366 MathWordList const & mathedWordList()
367 {
368         return theMathWordList;
369 }
370
371
372 void initMath()
373 {
374         static bool initialized = false;
375         if (!initialized) {
376                 initialized = true;
377                 initParser();
378                 initSymbols();
379         }
380 }
381
382
383 bool ensureMath(WriteStream & os, bool needs_mathmode, bool macro,
384                 bool textmode_macro)
385 {
386         bool brace = os.pendingBrace();
387         os.pendingBrace(false);
388         if (!os.latex())
389                 return brace;
390         if (os.textMode() && needs_mathmode) {
391                 if (brace) {
392                         // close \lyxmathsym
393                         os << '}';
394                         brace = false;
395                 } else {
396                         os << "\\ensuremath{";
397                         brace = true;
398                 }
399                 os.textMode(false);
400         } else if (macro && textmode_macro && !os.textMode()) {
401                 if (brace) {
402                         // close \ensuremath
403                         os << '}';
404                         brace = false;
405                 } else {
406                         os << "\\lyxmathsym{";
407                         brace = true;
408                 }
409                 os.textMode(true);
410         } else if (macro && brace && !needs_mathmode && !textmode_macro) {
411                 // This is a user defined macro, not a InsetMathMacro, so we
412                 // cannot be sure what mode is needed. We leave it in the
413                 // same environment it was entered by closing either \lyxmathsym
414                 // or \ensuremath, whichever was opened.
415                 os << '}';
416                 brace = false;
417                 os.textMode(!os.textMode());
418         }
419         return brace;
420 }
421
422
423 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
424                 bool locked, bool ascii)
425 {
426         bool textmode = mode == InsetMath::TEXT_MODE;
427         if (os.latex() && textmode && os.pendingBrace()) {
428                 os.os() << '}';
429                 os.pendingBrace(false);
430                 os.pendingSpace(false);
431                 os.textMode(true);
432         }
433         int oldmodes = os.textMode() ? 0x01 : 0;
434         os.textMode(textmode);
435         oldmodes |= os.lockedMode() ? 0x02 : 0;
436         os.lockedMode(locked);
437         oldmodes |= os.asciiOnly() ? 0x04 : 0;
438         os.asciiOnly(ascii);
439         return oldmodes;
440 }
441
442
443 latexkeys const * in_word_set(docstring const & str)
444 {
445         MathWordList::iterator it = theMathWordList.find(str);
446         if (it == theMathWordList.end())
447                 return 0;
448         if (it->second.inset == "macro")
449                 return 0;
450         return &(it->second);
451 }
452
453
454 MathAtom createInsetMath(char const * const s, Buffer * buf)
455 {
456         return createInsetMath(from_utf8(s), buf);
457 }
458
459
460 MathAtom createInsetMath(docstring const & s, Buffer * buf)
461 {
462         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
463         if ((s == "ce" || s == "cf") && buf
464             && buf->params().use_package("mhchem") == BufferParams::package_off)
465                 return MathAtom(new InsetMathMacro(buf, s));
466
467         latexkeys const * l = in_word_set(s);
468         if (l) {
469                 string const & inset = l->inset;
470                 //lyxerr << " found inset: '" << inset << '\'' << endl;
471                 if (inset == "ref")
472                         return MathAtom(new InsetMathRef(buf, l->name));
473                 if (inset == "overset")
474                         return MathAtom(new InsetMathOverset(buf));
475                 if (inset == "underset")
476                         return MathAtom(new InsetMathUnderset(buf));
477                 if (inset == "decoration")
478                         return MathAtom(new InsetMathDecoration(buf, l));
479                 if (inset == "space")
480                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
481                 if (inset == "class")
482                         return MathAtom(new InsetMathClass(buf, string_to_class(s)));
483                 if (inset == "dots")
484                         return MathAtom(new InsetMathDots(l));
485                 if (inset == "mbox")
486                         return MathAtom(new InsetMathBox(buf, l->name));
487 //              if (inset == "fbox")
488 //                      return MathAtom(new InsetMathFBox(l));
489                 if (inset == "style")
490                         return MathAtom(new InsetMathSize(buf, l));
491                 if (inset == "font")
492                         return MathAtom(new InsetMathFont(buf, l));
493                 if (inset == "oldfont")
494                         return MathAtom(new InsetMathFontOld(buf, l));
495                 if (inset == "matrix")
496                         return MathAtom(new InsetMathAMSArray(buf, s));
497                 if (inset == "split")
498                         return MathAtom(new InsetMathSplit(buf, s));
499                 if (inset == "big")
500                         // we can't create a InsetMathBig, since the argument
501                         // is missing.
502                         return MathAtom(new InsetMathUnknown(s));
503                 return MathAtom(new InsetMathSymbol(l));
504         }
505
506         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
507                 return MathAtom(new InsetMathMacroArgument(s[1] - '0'));
508         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
509                         && s[2] >= '1' && s[2] <= '9')
510                 return MathAtom(new InsetMathMacroArgument(s[2] - '0'));
511         if (s == "boxed")
512                 return MathAtom(new InsetMathBoxed(buf));
513         if (s == "fbox")
514                 return MathAtom(new InsetMathFBox(buf));
515         if (s == "framebox")
516                 return MathAtom(new InsetMathMakebox(buf, true));
517         if (s == "makebox")
518                 return MathAtom(new InsetMathMakebox(buf, false));
519         if (s.substr(0, 8) == "xymatrix") {
520                 char spacing_code = '\0';
521                 Length spacing;
522                 bool equal_spacing = false;
523                 size_t const len = s.length();
524                 size_t i = 8;
525                 if (i < len && s[i] == '@') {
526                         ++i;
527                         if (i < len && s[i] == '!') {
528                                 equal_spacing = true;
529                                 ++i;
530                                 if (i < len) {
531                                         switch (s[i]) {
532                                         case '0':
533                                         case 'R':
534                                         case 'C':
535                                                 spacing_code = static_cast<char>(s[i]);
536                                         }
537                                 }
538                         } else if (i < len) {
539                                 switch (s[i]) {
540                                 case 'R':
541                                 case 'C':
542                                 case 'M':
543                                 case 'W':
544                                 case 'H':
545                                 case 'L':
546                                         spacing_code = static_cast<char>(s[i]);
547                                         ++i;
548                                         break;
549                                 }
550                                 if (i < len && s[i] == '=') {
551                                         ++i;
552                                         spacing = Length(to_ascii(s.substr(i)));
553                                 }
554                         }
555                 }
556                 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
557                         equal_spacing));
558         }
559
560         if (s == "Diagram")
561                 return MathAtom(new InsetMathDiagram(buf));
562         if (s == "xrightarrow" || s == "xleftarrow" ||
563                 s == "xhookrightarrow" || s == "xhookleftarrow" ||
564                 s == "xRightarrow" || s == "xLeftarrow" ||
565                 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
566                 s == "xrightharpoondown" || s == "xrightharpoonup" ||
567                 s == "xleftharpoondown" || s == "xleftharpoonup" ||
568                 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
569                 s == "xmapsto")
570                 return MathAtom(new InsetMathXArrow(buf, s));
571         if (s == "split" || s == "alignedat")
572                 return MathAtom(new InsetMathSplit(buf, s));
573         if (s == "cases")
574                 return MathAtom(new InsetMathCases(buf));
575         if (s == "substack")
576                 return MathAtom(new InsetMathSubstack(buf));
577         if (s == "subarray" || s == "array")
578                 return MathAtom(new InsetMathArray(buf, s, 1, 1));
579         if (s == "sqrt")
580                 return MathAtom(new InsetMathSqrt(buf));
581         if (s == "root")
582                 return MathAtom(new InsetMathRoot(buf));
583         if (s == "tabular")
584                 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
585         if (s == "stackrel")
586                 return MathAtom(new InsetMathStackrel(buf, false));
587         // This string value is only for math toolbar use, no LaTeX name
588         if (s == "stackrelthree")
589                 return MathAtom(new InsetMathStackrel(buf, true));
590         if (s == "binom")
591                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
592         if (s == "dbinom")
593                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
594         if (s == "tbinom")
595                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
596         if (s == "choose")
597                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
598         if (s == "brace")
599                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
600         if (s == "brack")
601                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
602         if (s == "frac")
603                 return MathAtom(new InsetMathFrac(buf));
604         if (s == "cfrac")
605                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
606         if (s == "dfrac")
607                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
608         if (s == "tfrac")
609                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
610         if (s == "over")
611                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
612         if (s == "nicefrac")
613                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
614         if (s == "unitfrac")
615                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
616         // These string values are only for math toolbar use, no LaTeX names
617         if (s == "unitfracthree")
618                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
619         if (s == "unitone")
620                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
621         if (s == "unittwo")
622                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
623         if (s == "cfracleft")
624                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
625         if (s == "cfracright")
626                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
627         //if (s == "infer")
628         //      return MathAtom(new MathInferInset);
629         if (s == "atop")
630                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
631         if (s == "lefteqn")
632                 return MathAtom(new InsetMathLefteqn(buf));
633         if (s == "boldsymbol")
634                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
635         if (s == "bm")
636                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
637         if (s == "heavysymbol" || s == "hm")
638                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
639         if (s == "color" || s == "normalcolor")
640                 return MathAtom(new InsetMathColor(buf, true));
641         if (s == "textcolor")
642                 return MathAtom(new InsetMathColor(buf, false));
643         if (s == "hphantom")
644                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
645         if (s == "phantom")
646                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
647         if (s == "vphantom")
648                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
649         if (s == "cancel")
650                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
651         if (s == "bcancel")
652                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
653         if (s == "xcancel")
654                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
655         if (s == "cancelto")
656                 return MathAtom(new InsetMathCancelto(buf));
657         if (s == "smash")
658                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
659         // The following 2 string values are only for math toolbar use, no LaTeX names
660         if (s == "smashb")
661                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
662         if (s == "smasht")
663                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
664         if (s == "mathclap")
665                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
666         if (s == "mathllap")
667                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
668         if (s == "mathrlap")
669                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
670         if (s == "ensuremath")
671                 return MathAtom(new InsetMathEnsureMath(buf));
672         if (s == "sideset")
673                 return MathAtom(new InsetMathSideset(buf, true, true));
674         // The following 3 string values are only for math toolbar use, no LaTeX names
675         if (s == "sidesetr")
676                 return MathAtom(new InsetMathSideset(buf, false, true));
677         if (s == "sidesetl")
678                 return MathAtom(new InsetMathSideset(buf, true, false));
679         if (s == "sidesetn")
680                 return MathAtom(new InsetMathSideset(buf, false, false));
681         if (isSpecialChar(s))
682                 return MathAtom(new InsetMathSpecialChar(s));
683         if (s == " ")
684                 return MathAtom(new InsetMathSpace(" ", ""));
685         if (s == "regexp")
686                 return MathAtom(new InsetMathHull(buf, hullRegexp));
687
688         return MathAtom(new InsetMathMacro(buf, s));
689 }
690
691
692 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
693 {
694         // An example str:
695         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
696         docstring name;
697         docstring body = split(str, name, ' ');
698
699         if (name == "ref") {
700                 InsetCommandParams icp(REF_CODE);
701                 // FIXME UNICODE
702                 InsetCommand::string2params(to_utf8(str), icp);
703                 Encoding const * const utf8 = encodings.fromLyXName("utf8");
704                 OutputParams op(utf8);
705                 mathed_parse_cell(ar, icp.getCommand(op));
706         } else if (name == "mathspace") {
707                 InsetSpaceParams isp(true);
708                 InsetSpace::string2params(to_utf8(str), isp);
709                 InsetSpace is(isp);
710                 odocstringstream ods;
711                 otexstream os(ods);
712                 Encoding const * const ascii = encodings.fromLyXName("ascii");
713                 OutputParams op(ascii);
714                 is.latex(os, op);
715                 mathed_parse_cell(ar, ods.str());
716                 if (ar.size() == 2) {
717                         // remove "{}"
718                         if (ar[1].nucleus()->asBraceInset())
719                                 ar.pop_back();
720                 }
721         } else
722                 return false;
723
724         if (ar.size() != 1)
725                 return false;
726
727         return ar[0].nucleus();
728 }
729
730
731 } // namespace lyx