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