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