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