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