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