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