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