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