]> git.lyx.org Git - lyx.git/blob - src/mathed/MathFactory.cpp
478d22adb013371b888d57ad90e4bdb14b0106af
[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_mathmode, bool macro,
364                 bool textmode_macro)
365 {
366         bool brace = os.pendingBrace();
367         os.pendingBrace(false);
368         if (!os.latex())
369                 return brace;
370         if (os.textMode() && needs_mathmode) {
371                 if (brace) {
372                         // close \lyxmathsym
373                         os << '}';
374                         brace = false;
375                 } else {
376                         os << "\\ensuremath{";
377                         brace = true;
378                 }
379                 os.textMode(false);
380         } else if (macro && textmode_macro && !os.textMode()) {
381                 if (brace) {
382                         // close \ensuremath
383                         os << '}';
384                         brace = false;
385                 } else {
386                         os << "\\lyxmathsym{";
387                         brace = true;
388                 }
389                 os.textMode(true);
390         } else if (macro && brace && !needs_mathmode && !textmode_macro) {
391                 // This is a user defined macro, not a MathMacro, so we
392                 // cannot be sure what mode is needed. We leave it in the
393                 // same environment it was entered by closing either \lyxmathsym
394                 // or \ensuremath, whichever was opened.
395                 os << '}';
396                 brace = false;
397                 os.textMode(!os.textMode());
398         }
399         return brace;
400 }
401
402
403 int ensureMode(WriteStream & os, InsetMath::mode_type mode,
404                 bool locked, bool ascii)
405 {
406         bool textmode = mode == InsetMath::TEXT_MODE;
407         if (os.latex() && textmode && os.pendingBrace()) {
408                 os.os() << '}';
409                 os.pendingBrace(false);
410                 os.pendingSpace(false);
411                 os.textMode(true);
412         }
413         int oldmodes = os.textMode() ? 0x01 : 0;
414         os.textMode(textmode);
415         oldmodes |= os.lockedMode() ? 0x02 : 0;
416         os.lockedMode(locked);
417         oldmodes |= os.asciiOnly() ? 0x04 : 0;
418         os.asciiOnly(ascii);
419         return oldmodes;
420 }
421
422
423 latexkeys const * in_word_set(docstring const & str)
424 {
425         MathWordList::iterator it = theMathWordList.find(str);
426         if (it == theMathWordList.end())
427                 return 0;
428         if (it->second.inset == "macro")
429                 return 0;
430         return &(it->second);
431 }
432
433
434 MathAtom createInsetMath(char const * const s, Buffer * buf)
435 {
436         return createInsetMath(from_utf8(s), buf);
437 }
438
439
440 MathAtom createInsetMath(docstring const & s, Buffer * buf)
441 {
442         //lyxerr << "creating inset with name: '" << to_utf8(s) << '\'' << endl;
443         if ((s == "ce" || s == "cf") && buf
444             && buf->params().use_package("mhchem") == BufferParams::package_off)
445                 return MathAtom(new MathMacro(buf, s));
446
447         latexkeys const * l = in_word_set(s);
448         if (l) {
449                 string const & inset = l->inset;
450                 //lyxerr << " found inset: '" << inset << '\'' << endl;
451                 if (inset == "ref")
452                         return MathAtom(new InsetMathRef(buf, l->name));
453                 if (inset == "overset")
454                         return MathAtom(new InsetMathOverset(buf));
455                 if (inset == "underset")
456                         return MathAtom(new InsetMathUnderset(buf));
457                 if (inset == "decoration")
458                         return MathAtom(new InsetMathDecoration(buf, l));
459                 if (inset == "space")
460                         return MathAtom(new InsetMathSpace(to_ascii(l->name), ""));
461                 if (inset == "dots")
462                         return MathAtom(new InsetMathDots(l));
463                 if (inset == "mbox")
464                         return MathAtom(new InsetMathBox(buf, l->name));
465 //              if (inset == "fbox")
466 //                      return MathAtom(new InsetMathFBox(l));
467                 if (inset == "style")
468                         return MathAtom(new InsetMathSize(buf, l));
469                 if (inset == "font")
470                         return MathAtom(new InsetMathFont(buf, l));
471                 if (inset == "oldfont")
472                         return MathAtom(new InsetMathFontOld(buf, l));
473                 if (inset == "matrix")
474                         return MathAtom(new InsetMathAMSArray(buf, s));
475                 if (inset == "split")
476                         return MathAtom(new InsetMathSplit(buf, s));
477                 if (inset == "big")
478                         // we can't create a InsetMathBig, since the argument
479                         // is missing.
480                         return MathAtom(new InsetMathUnknown(s));
481                 return MathAtom(new InsetMathSymbol(l));
482         }
483
484         if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9')
485                 return MathAtom(new MathMacroArgument(s[1] - '0'));
486         if (s.size() == 3 && s[0] == '\\' && s[1] == '#'
487                         && s[2] >= '1' && s[2] <= '9')
488                 return MathAtom(new MathMacroArgument(s[2] - '0'));
489         if (s == "boxed")
490                 return MathAtom(new InsetMathBoxed(buf));
491         if (s == "fbox")
492                 return MathAtom(new InsetMathFBox(buf));
493         if (s == "framebox")
494                 return MathAtom(new InsetMathMakebox(buf, true));
495         if (s == "makebox")
496                 return MathAtom(new InsetMathMakebox(buf, false));
497         if (s == "kern")
498                 return MathAtom(new InsetMathKern);
499         if (s.substr(0, 8) == "xymatrix") {
500                 char spacing_code = '\0';
501                 Length spacing;
502                 bool equal_spacing = false;
503                 size_t const len = s.length();
504                 size_t i = 8;
505                 if (i < len && s[i] == '@') {
506                         ++i;
507                         if (i < len && s[i] == '!') {
508                                 equal_spacing = true;
509                                 ++i;
510                                 if (i < len) {
511                                         switch (s[i]) {
512                                         case '0':
513                                         case 'R':
514                                         case 'C':
515                                                 spacing_code = static_cast<char>(s[i]);
516                                         }
517                                 }
518                         } else if (i < len) {
519                                 switch (s[i]) {
520                                 case 'R':
521                                 case 'C':
522                                 case 'M':
523                                 case 'W':
524                                 case 'H':
525                                 case 'L':
526                                         spacing_code = static_cast<char>(s[i]);
527                                         ++i;
528                                         break;
529                                 }
530                                 if (i < len && s[i] == '=') {
531                                         ++i;
532                                         spacing = Length(to_ascii(s.substr(i)));
533                                 }
534                         }
535                 }
536                 return MathAtom(new InsetMathXYMatrix(buf, spacing, spacing_code,
537                         equal_spacing));
538         }
539
540         if (s == "Diagram")
541                 return MathAtom(new InsetMathDiagram(buf));
542         if (s == "xrightarrow" || s == "xleftarrow" ||
543                 s == "xhookrightarrow" || s == "xhookleftarrow" ||
544                 s == "xRightarrow" || s == "xLeftarrow" ||
545                 s == "xleftrightarrow" || s == "xLeftrightarrow" ||
546                 s == "xrightharpoondown" || s == "xrightharpoonup" ||
547                 s == "xleftharpoondown" || s == "xleftharpoonup" ||
548                 s == "xleftrightharpoons" || s == "xrightleftharpoons" ||
549                 s == "xmapsto")
550                 return MathAtom(new InsetMathXArrow(buf, s));
551         if (s == "split" || s == "alignedat")
552                 return MathAtom(new InsetMathSplit(buf, s));
553         if (s == "cases")
554                 return MathAtom(new InsetMathCases(buf));
555         if (s == "substack")
556                 return MathAtom(new InsetMathSubstack(buf));
557         if (s == "subarray" || s == "array")
558                 return MathAtom(new InsetMathArray(buf, s, 1, 1));
559         if (s == "sqrt")
560                 return MathAtom(new InsetMathSqrt(buf));
561         if (s == "root")
562                 return MathAtom(new InsetMathRoot(buf));
563         if (s == "tabular")
564                 return MathAtom(new InsetMathTabular(buf, s, 1, 1));
565         if (s == "stackrel")
566                 return MathAtom(new InsetMathStackrel(buf, false));
567         // This string value is only for math toolbar use, no LaTeX name
568         if (s == "stackrelthree")
569                 return MathAtom(new InsetMathStackrel(buf, true));
570         if (s == "binom")
571                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
572         if (s == "dbinom")
573                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::DBINOM));
574         if (s == "tbinom")
575                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::TBINOM));
576         if (s == "choose")
577                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::CHOOSE));
578         if (s == "brace")
579                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACE));
580         if (s == "brack")
581                 return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BRACK));
582         if (s == "frac")
583                 return MathAtom(new InsetMathFrac(buf));
584         if (s == "cfrac")
585                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC));
586         if (s == "dfrac")
587                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::DFRAC));
588         if (s == "tfrac")
589                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::TFRAC));
590         if (s == "over")
591                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::OVER));
592         if (s == "nicefrac")
593                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::NICEFRAC));
594         if (s == "unitfrac")
595                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC));
596         // These string values are only for math toolbar use, no LaTeX names
597         if (s == "unitfracthree")
598                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3));
599         if (s == "unitone")
600                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1));
601         if (s == "unittwo")
602                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT));
603         if (s == "cfracleft")
604                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT));
605         if (s == "cfracright")
606                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT));
607         //if (s == "infer")
608         //      return MathAtom(new MathInferInset);
609         if (s == "atop")
610                 return MathAtom(new InsetMathFrac(buf, InsetMathFrac::ATOP));
611         if (s == "lefteqn")
612                 return MathAtom(new InsetMathLefteqn(buf));
613         if (s == "boldsymbol")
614                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::AMS_BOLD));
615         if (s == "bm")
616                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_BOLD));
617         if (s == "heavysymbol" || s == "hm")
618                 return MathAtom(new InsetMathBoldSymbol(buf, InsetMathBoldSymbol::BM_HEAVY));
619         if (s == "color" || s == "normalcolor")
620                 return MathAtom(new InsetMathColor(buf, true));
621         if (s == "textcolor")
622                 return MathAtom(new InsetMathColor(buf, false));
623         if (s == "hphantom")
624                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::hphantom));
625         if (s == "phantom")
626                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::phantom));
627         if (s == "vphantom")
628                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::vphantom));
629         if (s == "cancel")
630                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::cancel));
631         if (s == "bcancel")
632                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::bcancel));
633         if (s == "xcancel")
634                 return MathAtom(new InsetMathCancel(buf, InsetMathCancel::xcancel));
635         if (s == "cancelto")
636                 return MathAtom(new InsetMathCancelto(buf));
637         if (s == "smash")
638                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
639         // The following 2 string values are only for math toolbar use, no LaTeX names
640         if (s == "smashb")
641                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
642         if (s == "smasht")
643                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
644         if (s == "mathclap")
645                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
646         if (s == "mathllap")
647                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathllap));
648         if (s == "mathrlap")
649                 return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
650         if (s == "ensuremath")
651                 return MathAtom(new InsetMathEnsureMath(buf));
652         if (s == "sideset")
653                 return MathAtom(new InsetMathSideset(buf, true, true));
654         // The following 3 string values are only for math toolbar use, no LaTeX names
655         if (s == "sidesetr")
656                 return MathAtom(new InsetMathSideset(buf, false, true));
657         if (s == "sidesetl")
658                 return MathAtom(new InsetMathSideset(buf, true, false));
659         if (s == "sidesetn")
660                 return MathAtom(new InsetMathSideset(buf, false, false));
661         if (isSpecialChar(s))
662                 return MathAtom(new InsetMathSpecialChar(s));
663         if (s == " ")
664                 return MathAtom(new InsetMathSpace(" ", ""));
665
666         if (s == "regexp")
667                 return MathAtom(new InsetMathHull(buf, hullRegexp));
668
669         return MathAtom(new MathMacro(buf, s));
670 }
671
672
673 bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
674 {
675         // An example str:
676         // "ref LatexCommand ref\nreference \"sec:Title\"\n\\end_inset\n\n";
677         docstring name;
678         docstring body = split(str, name, ' ');
679
680         if (name == "ref") {
681                 InsetCommandParams icp(REF_CODE);
682                 // FIXME UNICODE
683                 InsetCommand::string2params(to_utf8(str), icp);
684                 Encoding const * const utf8 = encodings.fromLyXName("utf8");
685                 OutputParams op(utf8);
686                 mathed_parse_cell(ar, icp.getCommand(op));
687         } else if (name == "mathspace") {
688                 InsetSpaceParams isp(true);
689                 InsetSpace::string2params(to_utf8(str), isp);
690                 InsetSpace is(isp);
691                 odocstringstream ods;
692                 otexstream os(ods, false);
693                 Encoding const * const ascii = encodings.fromLyXName("ascii");
694                 OutputParams op(ascii);
695                 is.latex(os, op);
696                 mathed_parse_cell(ar, ods.str());
697                 if (ar.size() == 2) {
698                         // remove "{}"
699                         if (ar[1].nucleus()->asBraceInset())
700                                 ar.pop_back();
701                 }
702         } else
703                 return false;
704
705         if (ar.size() != 1)
706                 return false;
707
708         return ar[0].nucleus();
709 }
710
711
712 bool isAsciiOrMathAlpha(char_type c)
713 {
714         return isASCII(c) || Encodings::isMathAlpha(c);
715 }
716
717
718 } // namespace lyx