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