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