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