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