]> git.lyx.org Git - lyx.git/blob - src/mathed/MathParser.cpp
Fix #10778 (issue with CJK and language nesting)
[lyx.git] / src / mathed / MathParser.cpp
1 /**
2  * \file MathParser.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 /*
12
13 If someone desperately needs partial "structures" (such as a few
14 cells of an array inset or similar) (s)he could uses the
15 following hack as starting point to write some macros:
16
17   \newif\ifcomment
18   \commentfalse
19   \ifcomment
20           \def\makeamptab{\catcode`\&=4\relax}
21           \def\makeampletter{\catcode`\&=11\relax}
22     \def\b{\makeampletter\expandafter\makeamptab\bi}
23     \long\def\bi#1\e{}
24   \else
25     \def\b{}\def\e{}
26   \fi
27   ...
28
29   \[\begin{array}{ccc}
30 1
31 &
32
33   \end{array}\]
34
35 */
36
37
38 #include <config.h>
39
40 #include "MathParser.h"
41
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathCancelto.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnsureMath.h"
51 #include "InsetMathEnv.h"
52 #include "InsetMathFrac.h"
53 #include "InsetMathKern.h"
54 #include "MathMacro.h"
55 #include "InsetMathPar.h"
56 #include "InsetMathRef.h"
57 #include "InsetMathRoot.h"
58 #include "InsetMathScript.h"
59 #include "InsetMathSideset.h"
60 #include "InsetMathSpace.h"
61 #include "InsetMathSplit.h"
62 #include "InsetMathSqrt.h"
63 #include "InsetMathStackrel.h"
64 #include "InsetMathString.h"
65 #include "InsetMathTabular.h"
66 #include "MathMacroTemplate.h"
67 #include "MathExtern.h"
68 #include "MathFactory.h"
69 #include "MathMacroArgument.h"
70 #include "MathSupport.h"
71
72 #include "Buffer.h"
73 #include "BufferParams.h"
74 #include "Encoding.h"
75 #include "Lexer.h"
76
77 #include "support/debug.h"
78 #include "support/convert.h"
79 #include "support/docstream.h"
80
81 #include <sstream>
82
83 //#define FILEDEBUG
84
85 using namespace std;
86
87 namespace lyx {
88
89 namespace {
90
91 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
92 {
93         //lyxerr << "handling mode: '" << str << "'" << endl;
94         if (str == "mathmode")
95                 return InsetMath::MATH_MODE;
96         if (str == "textmode" || str == "forcetext")
97                 return InsetMath::TEXT_MODE;
98         return oldmode;
99 }
100
101
102 bool stared(docstring const & s)
103 {
104         size_t const n = s.size();
105         return n && s[n - 1] == '*';
106 }
107
108
109 docstring const repl(docstring const & oldstr, char_type const c,
110                      docstring const & macro, bool textmode = false)
111 {
112         docstring newstr;
113         size_t i;
114         size_t j;
115
116         for (i = 0, j = 0; i < oldstr.size(); ++i) {
117                 if (c == oldstr[i]) {
118                         newstr.append(oldstr, j, i - j);
119                         newstr.append(macro);
120                         j = i + 1;
121                         if (macro.size() > 2 && j < oldstr.size())
122                                 newstr += (textmode && oldstr[j] == ' ' ? '\\' : ' ');
123                 }
124         }
125
126         // Any substitution?
127         if (j == 0)
128                 return oldstr;
129
130         newstr.append(oldstr, j, i - j);
131         return newstr;
132 }
133
134
135 docstring escapeSpecialChars(docstring const & str, bool textmode)
136 {
137         docstring const backslash = textmode ? from_ascii("\\textbackslash")
138                                              : from_ascii("\\backslash");
139         docstring const caret = textmode ? from_ascii("\\textasciicircum")
140                                          : from_ascii("\\mathcircumflex");
141         docstring const tilde = textmode ? from_ascii("\\textasciitilde")
142                                          : from_ascii("\\sim");
143
144         return repl(repl(repl(repl(repl(repl(repl(repl(repl(repl(str,
145                         '\\', backslash, textmode),
146                         '^', caret, textmode),
147                         '~', tilde, textmode),
148                         '_', from_ascii("\\_")),
149                         '$', from_ascii("\\$")),
150                         '#', from_ascii("\\#")),
151                         '&', from_ascii("\\&")),
152                         '%', from_ascii("\\%")),
153                         '{', from_ascii("\\{")),
154                         '}', from_ascii("\\}"));
155 }
156
157
158 /*!
159  * Add the row \p cellrow to \p grid.
160  * \returns wether the row could be added. Adding a row can fail for
161  * environments like "equation" that have a fixed number of rows.
162  */
163 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
164             docstring const & vskip, bool allow_newpage_ = true)
165 {
166         ++cellrow;
167         if (cellrow == grid.nrows()) {
168                 //lyxerr << "adding row " << cellrow << endl;
169                 grid.addRow(cellrow - 1);
170                 if (cellrow == grid.nrows()) {
171                         // We can't add a row to this grid, so let's
172                         // append the content of this cell to the previous
173                         // one.
174                         // This does not happen in well formed .lyx files,
175                         // but LyX versions 1.3.x and older could create
176                         // such files and tex2lyx can still do that.
177                         --cellrow;
178                         lyxerr << "ignoring extra row";
179                         if (!vskip.empty())
180                                 lyxerr << " with extra space " << to_utf8(vskip);
181                         if (!allow_newpage_)
182                                 lyxerr << " with no page break allowed";
183                         lyxerr << '.' << endl;
184                         return false;
185                 }
186         }
187         grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
188         grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
189         return true;
190 }
191
192
193 /*!
194  * Add the column \p cellcol to \p grid.
195  * \returns wether the column could be added. Adding a column can fail for
196  * environments like "eqnarray" that have a fixed number of columns.
197  */
198 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
199 {
200         ++cellcol;
201         if (cellcol == grid.ncols()) {
202                 //lyxerr << "adding column " << cellcol << endl;
203                 grid.addCol(cellcol);
204                 if (cellcol == grid.ncols()) {
205                         // We can't add a column to this grid, so let's
206                         // append the content of this cell to the previous
207                         // one.
208                         // This does not happen in well formed .lyx files,
209                         // but LyX versions 1.3.x and older could create
210                         // such files and tex2lyx can still do that.
211                         --cellcol;
212                         lyxerr << "ignoring extra column." << endl;
213                         return false;
214                 }
215         }
216         return true;
217 }
218
219
220 /*!
221  * Check whether the last row is empty and remove it if yes.
222  * Otherwise the following code
223  * \verbatim
224 \begin{array}{|c|c|}
225 \hline
226 1 & 2 \\ \hline
227 3 & 4 \\ \hline
228 \end{array}
229  * \endverbatim
230  * will result in a grid with 3 rows (+ the dummy row that is always present),
231  * because the last '\\' opens a new row.
232  * Do never delete a row that contains a multicolumn, even if all cells empty,
233  * since the multicolumn information would get lost otherwise.
234  * Note that this is only needed for inner-hull grid types, such as array
235  * or aligned, but not for outer-hull grid types, such as eqnarray or align.
236  */
237 void delEmptyLastRow(InsetMathGrid & grid)
238 {
239         InsetMathGrid::row_type const row = grid.nrows() - 1;
240         for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
241                 InsetMathGrid::idx_type const idx = grid.index(row, col);
242                 if (!grid.cell(idx).empty() ||
243                     grid.cellinfo(idx).multi_ != InsetMathGrid::CELL_NORMAL)
244                         return;
245         }
246         // Copy the row information of the empty row (which would contain the
247         // last hline in the example above) to the dummy row and delete the
248         // empty row.
249         grid.rowinfo(row + 1) = grid.rowinfo(row);
250         grid.delRow(row);
251 }
252
253
254 /*!
255  * Tell whether the environment name corresponds to an inner-hull grid type.
256  */
257 bool innerHull(docstring const & name)
258 {
259         // For [bB]matrix, [vV]matrix, and pmatrix we can check the suffix only
260         return name == "array" || name == "cases" || name == "aligned"
261                 || name == "alignedat" || name == "gathered" || name == "split"
262                 || name == "subarray" || name == "tabular" || name == "matrix"
263                 || name == "smallmatrix" || name.substr(1) == "matrix";
264 }
265
266
267 // These are TeX's catcodes
268 enum CatCode {
269         catEscape,     // 0    backslash
270         catBegin,      // 1    {
271         catEnd,        // 2    }
272         catMath,       // 3    $
273         catAlign,      // 4    &
274         catNewline,    // 5    ^^M
275         catParameter,  // 6    #
276         catSuper,      // 7    ^
277         catSub,        // 8    _
278         catIgnore,     // 9
279         catSpace,      // 10   space
280         catLetter,     // 11   a-zA-Z
281         catOther,      // 12   none of the above
282         catActive,     // 13   ~
283         catComment,    // 14   %
284         catInvalid     // 15   <delete>
285 };
286
287 CatCode theCatcode[128];
288
289
290 inline CatCode catcode(char_type c)
291 {
292         /* The only characters that are not catOther lie in the pure ASCII
293          * range. Therefore theCatcode has only 128 entries.
294          * TeX itself deals with 8bit characters, so if needed this table
295          * could be enlarged to 256 entries.
296          * Any larger value does not make sense, since the fact that we use
297          * unicode internally does not change Knuth's TeX engine.
298          * Apart from that a table for the full 21bit UCS4 range would waste
299          * too much memory. */
300         if (c >= 128)
301                 return catOther;
302
303         return theCatcode[c];
304 }
305
306
307 enum {
308         FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
309         FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
310         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
311         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
312         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
313         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
314         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced) token
315         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
316         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
317         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
318         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
319         FLAG_OPTION     = 1 << 11, //  read [...] style option
320         FLAG_BRACED     = 1 << 12  //  read {...} style argument
321 };
322
323
324 //
325 // Helper class for parsing
326 //
327
328 class Token {
329 public:
330         ///
331         Token() : cs_(), char_(0), cat_(catIgnore) {}
332         ///
333         Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
334         ///
335         explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
336
337         ///
338         docstring const & cs() const { return cs_; }
339         ///
340         CatCode cat() const { return cat_; }
341         ///
342         char_type character() const { return char_; }
343         ///
344         docstring asString() const { return !cs_.empty() ? cs_ : docstring(1, char_); }
345         ///
346         docstring asInput() const { return !cs_.empty() ? '\\' + cs_ : docstring(1, char_); }
347
348 private:
349         ///
350         docstring cs_;
351         ///
352         char_type char_;
353         ///
354         CatCode cat_;
355 };
356
357
358 ostream & operator<<(ostream & os, Token const & t)
359 {
360         if (!t.cs().empty()) {
361                 docstring const & cs = t.cs();
362                 // FIXME: For some strange reason, the stream operator instanciate
363                 // a new Token before outputting the contents of t.cs().
364                 // Because of this the line
365                 //     os << '\\' << cs;
366                 // below becomes recursive.
367                 // In order to avoid that we return early:
368                 if (cs == "\\")
369                         return os;
370                 os << '\\' << to_utf8(cs);
371         }
372         else if (t.cat() == catLetter)
373                 os << t.character();
374         else
375                 os << '[' << t.character() << ',' << t.cat() << ']';
376         return os;
377 }
378
379
380 class Parser {
381 public:
382         ///
383         typedef  InsetMath::mode_type mode_type;
384         ///
385         typedef  Parse::flags parse_mode;
386
387         ///
388         Parser(Lexer & lex, parse_mode mode, Buffer * buf);
389         /// Only use this for reading from .lyx file format, for the reason
390         /// see Parser::tokenize(istream &).
391         Parser(istream & is, parse_mode mode, Buffer * buf);
392         ///
393         Parser(docstring const & str, parse_mode mode, Buffer * buf);
394
395         ///
396         bool parse(MathAtom & at);
397         ///
398         bool parse(MathData & array, unsigned flags, mode_type mode);
399         ///
400         bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
401                 bool numbered);
402         ///
403         int lineno() const { return lineno_; }
404         ///
405         void putback();
406         /// store current position
407         void pushPosition();
408         /// restore previous position
409         void popPosition();
410         /// forget last saved position
411         void dropPosition();
412
413 private:
414         ///
415         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
416         /// get arg delimited by 'left' and 'right'
417         docstring getArg(char_type left, char_type right);
418         ///
419         char_type getChar();
420         ///
421         void error(string const & msg);
422         void error(docstring const & msg) { error(to_utf8(msg)); }
423         /// dump contents to screen
424         void dump() const;
425         /// Only use this for reading from .lyx file format (see
426         /// implementation for reason)
427         void tokenize(istream & is);
428         ///
429         void tokenize(docstring const & s);
430         ///
431         void skipSpaceTokens(idocstream & is, char_type c);
432         ///
433         void push_back(Token const & t);
434         ///
435         Token const & prevToken() const;
436         ///
437         Token const & nextToken() const;
438         ///
439         Token const & getToken();
440         /// skips spaces if any
441         void skipSpaces();
442         ///
443         void lex(docstring const & s);
444         ///
445         bool good() const;
446         ///
447         docstring parse_verbatim_item();
448         ///
449         docstring parse_verbatim_option();
450
451         ///
452         int lineno_;
453         ///
454         vector<Token> tokens_;
455         ///
456         unsigned pos_;
457         ///
458         std::vector<unsigned> positions_;
459         /// Stack of active environments
460         vector<docstring> environments_;
461         ///
462         parse_mode mode_;
463         ///
464         bool success_;
465         ///
466         Buffer * buffer_;
467 };
468
469
470 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
471         : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
472           buffer_(buf)
473 {
474         tokenize(lexer.getStream());
475         lexer.eatLine();
476 }
477
478
479 Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
480         : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
481 {
482         tokenize(is);
483 }
484
485
486 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
487         : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
488 {
489         tokenize(str);
490 }
491
492
493 void Parser::push_back(Token const & t)
494 {
495         tokens_.push_back(t);
496 }
497
498
499 Token const & Parser::prevToken() const
500 {
501         static const Token dummy;
502         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
503 }
504
505
506 Token const & Parser::nextToken() const
507 {
508         static const Token dummy;
509         return good() ? tokens_[pos_] : dummy;
510 }
511
512
513 Token const & Parser::getToken()
514 {
515         static const Token dummy;
516         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
517         return good() ? tokens_[pos_++] : dummy;
518 }
519
520
521 void Parser::skipSpaces()
522 {
523         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
524                 getToken();
525 }
526
527
528 void Parser::putback()
529 {
530         --pos_;
531 }
532
533
534 void Parser::pushPosition()
535 {
536         positions_.push_back(pos_);
537 }
538
539
540 void Parser::popPosition()
541 {
542         pos_ = positions_.back();
543         positions_.pop_back();
544 }
545
546
547 void Parser::dropPosition()
548 {
549         positions_.pop_back();
550 }
551
552
553 bool Parser::good() const
554 {
555         return pos_ < tokens_.size();
556 }
557
558
559 char_type Parser::getChar()
560 {
561         if (!good()) {
562                 error("The input stream is not well...");
563                 return 0;
564         }
565         return tokens_[pos_++].character();
566 }
567
568
569 docstring Parser::getArg(char_type left, char_type right)
570 {
571         docstring result;
572         skipSpaces();
573
574         if (!good())
575                 return result;
576
577         char_type c = getChar();
578
579         if (c != left)
580                 putback();
581         else
582                 while ((c = getChar()) != right && good())
583                         result += c;
584
585         return result;
586 }
587
588
589 void Parser::skipSpaceTokens(idocstream & is, char_type c)
590 {
591         // skip trailing spaces
592         while (catcode(c) == catSpace || catcode(c) == catNewline)
593                 if (!is.get(c))
594                         break;
595         //lyxerr << "putting back: " << c << endl;
596         is.putback(c);
597 }
598
599
600 void Parser::tokenize(istream & is)
601 {
602         // eat everything up to the next \end_inset or end of stream
603         // and store it in s for further tokenization
604         string s;
605         char c;
606         while (is.get(c)) {
607                 s += c;
608                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
609                         s = s.substr(0, s.size() - 10);
610                         break;
611                 }
612         }
613         // Remove the space after \end_inset
614         if (is.get(c) && c != ' ')
615                 is.unget();
616
617         // tokenize buffer
618         tokenize(from_utf8(s));
619 }
620
621
622 void Parser::tokenize(docstring const & buffer)
623 {
624         idocstringstream is(mode_ & Parse::VERBATIM
625                         ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
626                         : buffer, ios::in | ios::binary);
627
628         char_type c;
629         while (is.get(c)) {
630                 //lyxerr << "reading c: " << c << endl;
631
632                 switch (catcode(c)) {
633                         case catNewline: {
634                                 ++lineno_;
635                                 is.get(c);
636                                 if (catcode(c) == catNewline)
637                                         ; //push_back(Token("par"));
638                                 else {
639                                         push_back(Token('\n', catNewline));
640                                         is.putback(c);
641                                 }
642                                 break;
643                         }
644
645 /*
646                         case catComment: {
647                                 while (is.get(c) && catcode(c) != catNewline)
648                                         ;
649                                 ++lineno_;
650                                 break;
651                         }
652 */
653
654                         case catEscape: {
655                                 is.get(c);
656                                 if (!is) {
657                                         error("unexpected end of input");
658                                 } else {
659                                         if (c == '\n')
660                                                 c = ' ';
661                                         docstring s(1, c);
662                                         if (catcode(c) == catLetter) {
663                                                 // collect letters
664                                                 while (is.get(c) && catcode(c) == catLetter)
665                                                         s += c;
666                                                 skipSpaceTokens(is, c);
667                                         }
668                                         push_back(Token(s));
669                                 }
670                                 break;
671                         }
672
673                         case catSuper:
674                         case catSub: {
675                                 push_back(Token(c, catcode(c)));
676                                 is.get(c);
677                                 skipSpaceTokens(is, c);
678                                 break;
679                         }
680
681                         case catIgnore: {
682                                 if (!(mode_ & Parse::QUIET))
683                                         lyxerr << "ignoring a char: " << int(c) << endl;
684                                 break;
685                         }
686
687                         default:
688                                 push_back(Token(c, catcode(c)));
689                 }
690         }
691
692 #ifdef FILEDEBUG
693         dump();
694 #endif
695 }
696
697
698 void Parser::dump() const
699 {
700         lyxerr << "\nTokens: ";
701         for (unsigned i = 0; i < tokens_.size(); ++i) {
702                 if (i == pos_)
703                         lyxerr << " <#> ";
704                 lyxerr << tokens_[i];
705         }
706         lyxerr << " pos: " << pos_ << endl;
707 }
708
709
710 void Parser::error(string const & msg)
711 {
712         success_ = false;
713         if (!(mode_ & Parse::QUIET)) {
714                 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
715                        << msg << endl;
716                 dump();
717         }
718 }
719
720
721 bool Parser::parse(MathAtom & at)
722 {
723         skipSpaces();
724         MathData ar(buffer_);
725         parse(ar, false, InsetMath::UNDECIDED_MODE);
726         if (ar.size() != 1 || ar.front()->getType() == hullNone) {
727                 if (!(mode_ & Parse::QUIET))
728                         lyxerr << "unusual contents found: " << ar << endl;
729                 at = MathAtom(new InsetMathPar(buffer_, ar));
730                 //if (at->nargs() > 0)
731                 //      at.nucleus()->cell(0) = ar;
732                 //else
733                 //      lyxerr << "unusual contents found: " << ar << endl;
734                 success_ = false;
735         } else
736                 at = ar[0];
737         return success_;
738 }
739
740
741 docstring Parser::parse_verbatim_option()
742 {
743         skipSpaces();
744         docstring res;
745         if (nextToken().character() == '[') {
746                 Token t = getToken();
747                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
748                         if (t.cat() == catBegin) {
749                                 putback();
750                                 res += '{' + parse_verbatim_item() + '}';
751                         } else
752                                 res += t.asInput();
753                 }
754         }
755         return res;
756 }
757
758
759 docstring Parser::parse_verbatim_item()
760 {
761         skipSpaces();
762         docstring res;
763         if (nextToken().cat() == catBegin) {
764                 Token t = getToken();
765                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
766                         if (t.cat() == catBegin) {
767                                 putback();
768                                 res += '{' + parse_verbatim_item() + '}';
769                         }
770                         else
771                                 res += t.asInput();
772                 }
773         }
774         return res;
775 }
776
777
778 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
779 {
780         InsetMathGrid grid(buffer_, 1, 1);
781         parse1(grid, flags, mode, false);
782         array = grid.cell(0);
783         return success_;
784 }
785
786
787 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
788         const bool numbered)
789 {
790         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
791 }
792
793
794 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
795         const mode_type mode, const bool numbered)
796 {
797         int limits = 0;
798         InsetMathGrid::row_type cellrow = 0;
799         InsetMathGrid::col_type cellcol = 0;
800         MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
801         Buffer * buf = buffer_;
802
803         if (grid.asHullInset())
804                 grid.asHullInset()->numbered(cellrow, numbered);
805
806         //dump();
807         //lyxerr << " flags: " << flags << endl;
808         //lyxerr << " mode: " << mode  << endl;
809         //lyxerr << "grid: " << grid << endl;
810
811         while (good()) {
812                 Token const & t = getToken();
813
814 #ifdef FILEDEBUG
815                 lyxerr << "t: " << t << " flags: " << flags << endl;
816                 lyxerr << "mode: " << mode  << endl;
817                 cell->dump();
818                 lyxerr << endl;
819 #endif
820
821                 if (flags & FLAG_ITEM) {
822
823                         if (t.cat() == catBegin) {
824                                 // skip the brace and collect everything to the next matching
825                                 // closing brace
826                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
827                                 return success_;
828                         }
829
830                         // handle only this single token, leave the loop if done
831                         flags = FLAG_LEAVE;
832                 }
833
834
835                 if (flags & FLAG_BRACED) {
836                         if (t.cat() == catSpace)
837                                 continue;
838
839                         if (t.cat() != catBegin) {
840                                 error("opening brace expected");
841                                 return success_;
842                         }
843
844                         // skip the brace and collect everything to the next matching
845                         // closing brace
846                         flags = FLAG_BRACE_LAST;
847                 }
848
849
850                 if (flags & FLAG_OPTION) {
851                         if (t.cat() == catOther && t.character() == '[') {
852                                 MathData ar;
853                                 parse(ar, FLAG_BRACK_LAST, mode);
854                                 cell->append(ar);
855                         } else {
856                                 // no option found, put back token and we are done
857                                 putback();
858                         }
859                         return success_;
860                 }
861
862                 //
863                 // cat codes
864                 //
865                 if (t.cat() == catMath) {
866                         if (mode != InsetMath::MATH_MODE) {
867                                 // we are inside some text mode thingy, so opening new math is allowed
868                                 Token const & n = getToken();
869                                 if (n.cat() == catMath) {
870                                         // TeX's $$...$$ syntax for displayed math
871                                         if (mode == InsetMath::UNDECIDED_MODE) {
872                                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
873                                                 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
874                                                 getToken(); // skip the second '$' token
875                                         } else {
876                                                 // This is not an outer hull and display math is
877                                                 // not allowed inside text mode environments.
878                                                 error("bad math environment $$");
879                                                 break;
880                                         }
881                                 } else {
882                                         // simple $...$  stuff
883                                         putback();
884                                         if (mode == InsetMath::UNDECIDED_MODE) {
885                                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
886                                                 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
887                                         } else {
888                                                 // Don't create nested math hulls (bug #5392)
889                                                 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
890                                                 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
891                                         }
892                                 }
893                         }
894
895                         else if (flags & FLAG_SIMPLE) {
896                                 // this is the end of the formula
897                                 return success_;
898                         }
899
900                         else {
901                                 Token const & n = getToken();
902                                 if (n.cat() == catMath) {
903                                         error("something strange in the parser");
904                                         break;
905                                 } else {
906                                         // This is inline math ($...$), but the parser thinks we are
907                                         // already in math mode and latex would issue an error, unless we
908                                         // are inside a text mode user macro. We have no way to tell, so
909                                         // let's play safe by using \ensuremath, as it will work in any case.
910                                         putback();
911                                         cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
912                                         parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
913                                 }
914                         }
915                 }
916
917                 else if (t.cat() == catLetter)
918                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
919
920                 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
921                         if (cell->empty() || cell->back()->getChar() != ' ')
922                                 cell->push_back(MathAtom(new InsetMathChar(t.character())));
923                 }
924
925                 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
926                         if (cell->empty() || cell->back()->getChar() != ' ')
927                                 cell->push_back(MathAtom(new InsetMathChar(' ')));
928                 }
929
930                 else if (t.cat() == catParameter) {
931                         Token const & n = getToken();
932                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
933                 }
934
935                 else if (t.cat() == catActive)
936                         cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), "")));
937
938                 else if (t.cat() == catBegin) {
939                         MathData ar;
940                         parse(ar, FLAG_BRACE_LAST, mode);
941                         // do not create a BraceInset if they were written by LyX
942                         // this helps to keep the annoyance of  "a choose b"  to a minimum
943                         if (ar.size() == 1 && ar[0]->extraBraces())
944                                 cell->append(ar);
945                         else
946                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
947                 }
948
949                 else if (t.cat() == catEnd) {
950                         if (flags & FLAG_BRACE_LAST)
951                                 return success_;
952                         error("found '}' unexpectedly");
953                         //LASSERT(false, /**/);
954                         //add(cell, '}', LM_TC_TEX);
955                 }
956
957                 else if (t.cat() == catAlign) {
958                         //lyxerr << " column now " << (cellcol + 1)
959                         //       << " max: " << grid.ncols() << endl;
960                         if (flags & FLAG_ALIGN)
961                                 return success_;
962                         if (addCol(grid, cellcol))
963                                 cell = &grid.cell(grid.index(cellrow, cellcol));
964                 }
965
966                 else if (t.cat() == catSuper || t.cat() == catSub) {
967                         bool up = (t.cat() == catSuper);
968                         // we need no new script inset if the last thing was a scriptinset,
969                         // which has that script already not the same script already
970                         if (cell->empty())
971                                 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
972                         else if (cell->back()->asScriptInset() &&
973                                         !cell->back()->asScriptInset()->has(up))
974                                 cell->back().nucleus()->asScriptInset()->ensure(up);
975                         else if (cell->back()->asScriptInset())
976                                 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
977                         else
978                                 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
979                         InsetMathScript * p = cell->back().nucleus()->asScriptInset();
980                         // special handling of {}-bases
981                         // Here we could remove the brace inset for things
982                         // like {a'}^2 and add the braces back in
983                         // InsetMathScript::write().
984                         // We do not do it, since it is not possible to detect
985                         // reliably whether the braces are needed because the
986                         // nucleus contains more than one symbol, or whether
987                         // they are needed for unknown commands like \xx{a}_0
988                         // or \yy{a}{b}_0. This was done in revision 14819
989                         // in an unreliable way. See this thread
990                         // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
991                         // for more details.
992                         // However, we remove empty braces because they look
993                         // ugly on screen and we are sure that they were added
994                         // by the write() method (and will be re-added on save).
995                         if (p->nuc().size() == 1 &&
996                             p->nuc().back()->asBraceInset() &&
997                             p->nuc().back()->asBraceInset()->cell(0).empty())
998                                 p->nuc().erase(0);
999
1000                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
1001                         if (limits) {
1002                                 p->limits(limits);
1003                                 limits = 0;
1004                         }
1005                 }
1006
1007                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
1008                         //lyxerr << "finished reading option" << endl;
1009                         return success_;
1010                 }
1011
1012                 else if (t.cat() == catOther) {
1013                         char_type c = t.character();
1014                         if (isAsciiOrMathAlpha(c)
1015                             || mode_ & Parse::VERBATIM
1016                             || !(mode_ & Parse::USETEXT)
1017                             || mode == InsetMath::TEXT_MODE) {
1018                                 cell->push_back(MathAtom(new InsetMathChar(c)));
1019                         } else {
1020                                 MathAtom at = createInsetMath("text", buf);
1021                                 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1022                                 while (nextToken().cat() == catOther
1023                                        && !isAsciiOrMathAlpha(nextToken().character())) {
1024                                         c = getToken().character();
1025                                         at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1026                                 }
1027                                 cell->push_back(at);
1028                         }
1029                 }
1030
1031                 else if (t.cat() == catComment) {
1032                         docstring s;
1033                         while (good()) {
1034                                 Token const & t = getToken();
1035                                 if (t.cat() == catNewline)
1036                                         break;
1037                                 s += t.asInput();
1038                         }
1039                         cell->push_back(MathAtom(new InsetMathComment(buf, s)));
1040                         skipSpaces();
1041                 }
1042
1043                 //
1044                 // control sequences
1045                 //
1046
1047                 else if (t.cs() == "lyxlock") {
1048                         if (!cell->empty())
1049                                 cell->back().nucleus()->lock(true);
1050                 }
1051
1052                 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
1053                          t.cs() == "def") {
1054                         if (t.cs() == "global")
1055                                 getToken();
1056
1057                         // get name
1058                         docstring name = getToken().cs();
1059
1060                         // read parameters
1061                         int nargs = 0;
1062                         docstring pars;
1063                         while (good() && nextToken().cat() != catBegin) {
1064                                 pars += getToken().cs();
1065                                 ++nargs;
1066                         }
1067                         nargs /= 2;
1068
1069                         // read definition
1070                         MathData def;
1071                         parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1072
1073                         // is a version for display attached?
1074                         skipSpaces();
1075                         MathData display;
1076                         if (nextToken().cat() == catBegin)
1077                                 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1078
1079                         cell->push_back(MathAtom(new MathMacroTemplate(buf,
1080                                 name, nargs, 0, MacroTypeDef,
1081                                 vector<MathData>(), def, display)));
1082
1083                         if (buf && (mode_ & Parse::TRACKMACRO))
1084                                 buf->usermacros.insert(name);
1085                 }
1086
1087                 else if (t.cs() == "newcommand" ||
1088                          t.cs() == "renewcommand" ||
1089                          t.cs() == "newlyxcommand") {
1090                         // get name
1091                         if (getToken().cat() != catBegin) {
1092                                 error("'{' in \\newcommand expected (1) ");
1093                                 return success_;
1094                         }
1095                         docstring name = getToken().cs();
1096                         if (getToken().cat() != catEnd) {
1097                                 error("'}' in \\newcommand expected");
1098                                 return success_;
1099                         }
1100
1101                         // get arity
1102                         docstring const arg = getArg('[', ']');
1103                         int nargs = 0;
1104                         if (!arg.empty())
1105                                 nargs = convert<int>(arg);
1106
1107                         // optional argument given?
1108                         skipSpaces();
1109                         int optionals = 0;
1110                         vector<MathData> optionalValues;
1111                         while (nextToken().character() == '[') {
1112                                 getToken();
1113                                 optionalValues.push_back(MathData());
1114                                 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1115                                 ++optionals;
1116                         }
1117
1118                         MathData def;
1119                         parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1120
1121                         // is a version for display attached?
1122                         skipSpaces();
1123                         MathData display;
1124                         if (nextToken().cat() == catBegin)
1125                                 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1126
1127                         cell->push_back(MathAtom(new MathMacroTemplate(buf,
1128                                 name, nargs, optionals, MacroTypeNewcommand,
1129                                 optionalValues, def, display)));
1130
1131                         if (buf && (mode_ & Parse::TRACKMACRO))
1132                                 buf->usermacros.insert(name);
1133                 }
1134
1135                 else if (t.cs() == "newcommandx" ||
1136                          t.cs() == "renewcommandx") {
1137                         // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1138                         // get name
1139                         docstring name;
1140                         if (nextToken().cat() == catBegin) {
1141                                 getToken();
1142                                 name = getToken().cs();
1143                                 if (getToken().cat() != catEnd) {
1144                                         error("'}' in \\newcommandx expected");
1145                                         return success_;
1146                                 }
1147                         } else
1148                                 name = getToken().cs();
1149
1150                         // get arity
1151                         docstring const arg = getArg('[', ']');
1152                         if (arg.empty()) {
1153                                 error("[num] in \\newcommandx expected");
1154                                 return success_;
1155                         }
1156                         int nargs = convert<int>(arg);
1157
1158                         // get options
1159                         int optionals = 0;
1160                         vector<MathData> optionalValues;
1161                         if (nextToken().character() == '[') {
1162                                 // skip '['
1163                                 getToken();
1164
1165                                 // handle 'opt=value' options, separated by ','.
1166                                 skipSpaces();
1167                                 while (nextToken().character() != ']' && good()) {
1168                                         if (nextToken().character() >= '1'
1169                                             && nextToken().character() <= '9') {
1170                                                 // optional value -> get parameter number
1171                                                 int n = getChar() - '0';
1172                                                 if (n > nargs) {
1173                                                         error("Arity of \\newcommandx too low "
1174                                                               "for given optional parameter.");
1175                                                         return success_;
1176                                                 }
1177
1178                                                 // skip '='
1179                                                 if (getToken().character() != '=') {
1180                                                         error("'=' and optional parameter value "
1181                                                               "expected for \\newcommandx");
1182                                                         return success_;
1183                                                 }
1184
1185                                                 // get value
1186                                                 int optNum = max(size_t(n), optionalValues.size());
1187                                                 optionalValues.resize(optNum);
1188                                                 optionalValues[n - 1].clear();
1189                                                 while (nextToken().character() != ']'
1190                                                        && nextToken().character() != ',') {
1191                                                         MathData data;
1192                                                         parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1193                                                         optionalValues[n - 1].append(data);
1194                                                 }
1195                                                 optionals = max(n, optionals);
1196                                         } else if (nextToken().cat() == catLetter) {
1197                                                 // we in fact ignore every non-optional
1198                                                 // parameter
1199
1200                                                 // get option name
1201                                                 docstring opt;
1202                                                 while (nextToken().cat() == catLetter)
1203                                                         opt += getChar();
1204
1205                                                 // value?
1206                                                 skipSpaces();
1207                                                 MathData value;
1208                                                 if (nextToken().character() == '=') {
1209                                                         getToken();
1210                                                         while (nextToken().character() != ']'
1211                                                                 && nextToken().character() != ',')
1212                                                                 parse(value, FLAG_ITEM,
1213                                                                       InsetMath::UNDECIDED_MODE);
1214                                                 }
1215                                         } else {
1216                                                 error("option for \\newcommandx expected");
1217                                                 return success_;
1218                                         }
1219
1220                                         // skip komma
1221                                         skipSpaces();
1222                                         if (nextToken().character() == ',') {
1223                                                 getChar();
1224                                                 skipSpaces();
1225                                         } else if (nextToken().character() != ']') {
1226                                                 error("Expecting ',' or ']' in options "
1227                                                       "of \\newcommandx");
1228                                                 return success_;
1229                                         }
1230                                 }
1231
1232                                 // skip ']'
1233                                 if (!good())
1234                                         return success_;
1235                                 getToken();
1236                         }
1237
1238                         // get definition
1239                         MathData def;
1240                         parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1241
1242                         // is a version for display attached?
1243                         skipSpaces();
1244                         MathData display;
1245                         if (nextToken().cat() == catBegin)
1246                                 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1247
1248                         cell->push_back(MathAtom(new MathMacroTemplate(buf,
1249                                 name, nargs, optionals, MacroTypeNewcommandx,
1250                                 optionalValues, def, display)));
1251
1252                         if (buf && (mode_ & Parse::TRACKMACRO))
1253                                 buf->usermacros.insert(name);
1254                 }
1255
1256                 else if (t.cs() == "(") {
1257                         if (mode == InsetMath::UNDECIDED_MODE) {
1258                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1259                                 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1260                         } else {
1261                                 // Don't create nested math hulls (bug #5392)
1262                                 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1263                                 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE2, InsetMath::MATH_MODE);
1264                         }
1265                 }
1266
1267                 else if (t.cs() == "[") {
1268                         if (mode != InsetMath::UNDECIDED_MODE) {
1269                                 error("bad math environment [");
1270                                 break;
1271                         }
1272                         cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1273                         parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1274                 }
1275
1276                 else if (t.cs() == "protect")
1277                         // ignore \\protect, will hopefully be re-added during output
1278                         ;
1279
1280                 else if (t.cs() == "end") {
1281                         if (flags & FLAG_END) {
1282                                 // eat environment name
1283                                 docstring const name = getArg('{', '}');
1284                                 if (environments_.empty())
1285                                         error("'found \\end{" + name +
1286                                               "}' without matching '\\begin{" +
1287                                               name + "}'");
1288                                 else if (name != environments_.back())
1289                                         error("'\\end{" + name +
1290                                               "}' does not match '\\begin{" +
1291                                               environments_.back() + "}'");
1292                                 else {
1293                                         environments_.pop_back();
1294                                         // Delete empty last row in matrix
1295                                         // like insets.
1296                                         // If you abuse InsetMathGrid for
1297                                         // non-matrix like structures you
1298                                         // probably need to refine this test.
1299                                         // Right now we only have to test for
1300                                         // single line hull insets.
1301                                         if (grid.nrows() > 1 && innerHull(name))
1302                                                 delEmptyLastRow(grid);
1303                                         return success_;
1304                                 }
1305                         } else
1306                                 error("found 'end' unexpectedly");
1307                 }
1308
1309                 else if (t.cs() == ")") {
1310                         if (flags & FLAG_SIMPLE2)
1311                                 return success_;
1312                         error("found '\\)' unexpectedly");
1313                 }
1314
1315                 else if (t.cs() == "]") {
1316                         if (flags & FLAG_EQUATION)
1317                                 return success_;
1318                         error("found '\\]' unexpectedly");
1319                 }
1320
1321                 else if (t.cs() == "\\") {
1322                         if (flags & FLAG_ALIGN)
1323                                 return success_;
1324                         bool starred = false;
1325                         docstring arg;
1326                         if (nextToken().asInput() == "*") {
1327                                 getToken();
1328                                 starred = true;
1329                         } else if (nextToken().asInput() == "[")
1330                                 arg = getArg('[', ']');
1331                         else if (!good())
1332                                 error("missing token after \\\\");
1333                         // skip "{}" added in front of "[" (the
1334                         // counterpart is in InsetMathGrid::eolString())
1335                         // skip spaces because formula could come from tex2lyx
1336                         bool skipBraces = false;
1337                         pushPosition();
1338                         if (nextToken().cat() == catBegin) {
1339                                 getToken();
1340                                 if (nextToken().cat() == catEnd) {
1341                                         getToken();
1342                                         pushPosition();
1343                                         skipSpaces();
1344                                         if (nextToken().asInput() == "[")
1345                                                 skipBraces = true;
1346                                         popPosition();
1347                                 }
1348                         }
1349                         if (skipBraces)
1350                                 dropPosition();
1351                         else
1352                                 popPosition();
1353                         bool const added = addRow(grid, cellrow, arg, !starred);
1354                         if (added) {
1355                                 cellcol = 0;
1356                                 if (grid.asHullInset())
1357                                         grid.asHullInset()->numbered(
1358                                                         cellrow, numbered);
1359                                 cell = &grid.cell(grid.index(cellrow,
1360                                                              cellcol));
1361                         }
1362                 }
1363
1364                 else if (t.cs() == "multicolumn" && grid.handlesMulticolumn()) {
1365                         // if the columns are specified numerically,
1366                         // extract column count and insert dummy cells,
1367                         // otherwise parse it as an user macro
1368                         MathData count;
1369                         parse(count, FLAG_ITEM, mode);
1370                         int cols;
1371                         if (extractNumber(count, cols)) {
1372                                 // resize the table if necessary
1373                                 size_t first = grid.index(cellrow, cellcol);
1374                                 for (int i = 1; i < cols; ++i) {
1375                                         if (addCol(grid, cellcol)) {
1376                                                 size_t const idx = grid.index(cellrow, cellcol);
1377                                                 grid.cellinfo(idx).multi_ = 
1378                                                         InsetMathGrid::CELL_PART_OF_MULTICOLUMN;
1379                                         }
1380                                 }
1381
1382                                 // the first cell is the real thing, not a dummy 
1383                                 cell = &grid.cell(first); 
1384                                 grid.cellinfo(first).multi_ =
1385                                         InsetMathGrid::CELL_BEGIN_OF_MULTICOLUMN; 
1386                                 // read special alignment 
1387                                 MathData align; 
1388                                 parse(align, FLAG_ITEM, mode); 
1389                                 grid.cellinfo(first).align_ = asString(align); 
1390                                 // parse the remaining contents into the "real" cell
1391                                 parse(*cell, FLAG_ITEM, mode); 
1392                         } else {
1393                                 MathAtom at = MathAtom(new MathMacro(buf, t.cs()));
1394                                 cell->push_back(at);
1395                                 cell->push_back(MathAtom(new InsetMathBrace(count)));
1396                         }
1397                 }
1398
1399                 else if (t.cs() == "limits" || t.cs() == "nolimits") {
1400                         CatCode const cat = nextToken().cat();
1401                         if (cat == catSuper || cat == catSub)
1402                                 limits = t.cs() == "limits" ? 1 : -1;
1403                         else {
1404                                 MathAtom at = createInsetMath(t.cs(), buf);
1405                                 cell->push_back(at);
1406                         }
1407                 }
1408
1409                 // \notag is the same as \nonumber if amsmath is used
1410                 else if ((t.cs() == "nonumber" || t.cs() == "notag") &&
1411                          grid.asHullInset())
1412                         grid.asHullInset()->numbered(cellrow, false);
1413
1414                 else if (t.cs() == "number" && grid.asHullInset())
1415                         grid.asHullInset()->numbered(cellrow, true);
1416
1417                 else if (t.cs() == "hline") {
1418                         grid.rowinfo(cellrow).lines_ ++;
1419                 }
1420
1421                 else if (t.cs() == "sqrt") {
1422                         MathData ar;
1423                         parse(ar, FLAG_OPTION, mode);
1424                         if (!ar.empty()) {
1425                                 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1426                                 cell->back().nucleus()->cell(0) = ar;
1427                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1428                         } else {
1429                                 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1430                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1431                         }
1432                 }
1433
1434                 else if (t.cs() == "cancelto") {
1435                         MathData ar;
1436                         parse(ar, FLAG_ITEM, mode);
1437                                 cell->push_back(MathAtom(new InsetMathCancelto(buf)));
1438                                 cell->back().nucleus()->cell(1) = ar;
1439                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1440                 }
1441
1442                 else if (t.cs() == "unit") {
1443                         // Allowed formats \unit[val]{unit}
1444                         MathData ar;
1445                         parse(ar, FLAG_OPTION, mode);
1446                         if (!ar.empty()) {
1447                                 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1448                                 cell->back().nucleus()->cell(0) = ar;
1449                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1450                         } else {
1451                                 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1452                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1453                         }
1454                 }
1455
1456                 else if (t.cs() == "unitfrac") {
1457                         // Here allowed formats are \unitfrac[val]{num}{denom}
1458                         MathData ar;
1459                         parse(ar, FLAG_OPTION, mode);
1460                         if (!ar.empty()) {
1461                                 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1462                                 cell->back().nucleus()->cell(2) = ar;
1463                         } else {
1464                                 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1465                         }
1466                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1467                         parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1468                 }
1469
1470                 else if (t.cs() == "cfrac") {
1471                         // allowed formats are \cfrac[pos]{num}{denom}
1472                         docstring const arg = getArg('[', ']');
1473                         //lyxerr << "got so far: '" << arg << "'" << endl;
1474                                 if (arg == "l")
1475                                         cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1476                                 else if (arg == "r")
1477                                         cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1478                                 else if (arg.empty() || arg == "c")
1479                                         cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1480                                 else {
1481                                         error("found invalid optional argument");
1482                                         break;
1483                                 }
1484                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1485                         parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1486                 }
1487
1488                 else if (t.cs() == "sideset") {
1489                         // Here allowed formats are \sideset{_{bl}^{tl}}{_{br}^{tr}}{operator}
1490                         MathData ar[2];
1491                         InsetMathScript * script[2] = {0, 0};
1492                         for (int i = 0; i < 2; ++i) {
1493                                 parse(ar[i], FLAG_ITEM, mode);
1494                                 if (ar[i].size() == 1)
1495                                         script[i] = ar[i][0].nucleus()->asScriptInset();
1496                         }
1497                         bool const hasscript[2] = {script[0] ? true : false, script[1] ? true : false};
1498                         cell->push_back(MathAtom(new InsetMathSideset(buf, hasscript[0], hasscript[1])));
1499                         if (hasscript[0]) {
1500                                 if (script[0]->hasDown())
1501                                         cell->back().nucleus()->cell(1) = script[0]->down();
1502                                 if (script[0]->hasUp())
1503                                         cell->back().nucleus()->cell(2) = script[0]->up();
1504                         } else
1505                                 cell->back().nucleus()->cell(1) = ar[0];
1506                         if (hasscript[1]) {
1507                                 if (script[1]->hasDown())
1508                                         cell->back().nucleus()->cell(2 + hasscript[0]) = script[1]->down();
1509                                 if (script[1]->hasUp())
1510                                         cell->back().nucleus()->cell(3 + hasscript[0]) = script[1]->up();
1511                         } else
1512                                 cell->back().nucleus()->cell(2 + hasscript[0]) = ar[1];
1513                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1514                 }
1515
1516                 else if (t.cs() == "stackrel") {
1517                         // Here allowed formats are \stackrel[subscript]{superscript}{operator}
1518                         MathData ar;
1519                         parse(ar, FLAG_OPTION, mode);
1520                         cell->push_back(MathAtom(new InsetMathStackrel(buf, !ar.empty())));
1521                         if (!ar.empty())
1522                                 cell->back().nucleus()->cell(2) = ar;
1523                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1524                         parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1525                 }
1526
1527                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1528                         cell->push_back(createInsetMath(t.cs(), buf));
1529                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1530                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1531                 }
1532
1533                 else if (t.cs() == "xhookrightarrow" || t.cs() == "xhookleftarrow" ||
1534                              t.cs() == "xRightarrow" || t.cs() == "xLeftarrow" ||
1535                                  t.cs() == "xleftrightarrow" || t.cs() == "xLeftrightarrow" ||
1536                                  t.cs() == "xrightharpoondown" || t.cs() == "xrightharpoonup" ||
1537                                  t.cs() == "xleftharpoondown" || t.cs() == "xleftharpoonup" ||
1538                                  t.cs() == "xleftrightharpoons" || t.cs() == "xrightleftharpoons" ||
1539                                  t.cs() == "xmapsto") {
1540                         cell->push_back(createInsetMath(t.cs(), buf));
1541                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1542                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1543                 }
1544
1545                 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1546                           || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1547                         cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1548                         docstring const opt = parse_verbatim_option();
1549                         docstring const ref = parse_verbatim_item();
1550                         if (!opt.empty()) {
1551                                 cell->back().nucleus()->cell(1).push_back(
1552                                         MathAtom(new InsetMathString(opt)));
1553                         }
1554                         cell->back().nucleus()->cell(0).push_back(
1555                                         MathAtom(new InsetMathString(ref)));
1556                 }
1557
1558                 else if (t.cs() == "left") {
1559                         skipSpaces();
1560                         Token const & tl = getToken();
1561                         // \| and \Vert are equivalent, and InsetMathDelim
1562                         // can't handle \|
1563                         // FIXME: fix this in InsetMathDelim itself!
1564                         docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1565                         MathData ar;
1566                         parse(ar, FLAG_RIGHT, mode);
1567                         if (!good())
1568                                 break;
1569                         skipSpaces();
1570                         Token const & tr = getToken();
1571                         docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1572                         cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1573                 }
1574
1575                 else if (t.cs() == "right") {
1576                         if (flags & FLAG_RIGHT)
1577                                 return success_;
1578                         //lyxerr << "got so far: '" << cell << "'" << endl;
1579                         error("Unmatched right delimiter");
1580                         return success_;
1581                 }
1582
1583                 else if (t.cs() == "begin") {
1584                         docstring const name = getArg('{', '}');
1585
1586                         if (name.empty()) {
1587                                 success_ = false;
1588                                 error("found invalid environment");
1589                                 return success_;
1590                         }
1591
1592                         environments_.push_back(name);
1593
1594                         if (name == "array" || name == "subarray") {
1595                                 docstring const valign = parse_verbatim_option() + 'c';
1596                                 docstring const halign = parse_verbatim_item();
1597                                 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1598                                         InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1599                                 parse2(cell->back(), FLAG_END, mode, false);
1600                         }
1601
1602                         else if (name == "tabular") {
1603                                 docstring const valign = parse_verbatim_option() + 'c';
1604                                 docstring const halign = parse_verbatim_item();
1605                                 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1606                                         InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1607                                 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1608                         }
1609
1610                         else if (name == "split" || name == "cases") {
1611                                 cell->push_back(createInsetMath(name, buf));
1612                                 parse2(cell->back(), FLAG_END, mode, false);
1613                         }
1614
1615                         else if (name == "alignedat") {
1616                                 docstring const valign = parse_verbatim_option() + 'c';
1617                                 // ignore this for a while
1618                                 getArg('{', '}');
1619                                 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1620                                 parse2(cell->back(), FLAG_END, mode, false);
1621                         }
1622
1623                         else if (name == "math") {
1624                                 if (mode == InsetMath::UNDECIDED_MODE) {
1625                                         cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1626                                         parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, false);
1627                                 } else {
1628                                         // Don't create nested math hulls (bug #5392)
1629                                         cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1630                                         parse(cell->back().nucleus()->cell(0), FLAG_END, InsetMath::MATH_MODE);
1631                                 }
1632                         }
1633
1634                         else if (name == "equation" || name == "equation*"
1635                                         || name == "displaymath") {
1636                                 if (mode != InsetMath::UNDECIDED_MODE) {
1637                                         error("bad math environment " + name);
1638                                         break;
1639                                 }
1640                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1641                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1642                         }
1643
1644                         else if (name == "eqnarray" || name == "eqnarray*") {
1645                                 if (mode != InsetMath::UNDECIDED_MODE) {
1646                                         error("bad math environment " + name);
1647                                         break;
1648                                 }
1649                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1650                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1651                         }
1652
1653                         else if (name == "align" || name == "align*") {
1654                                 if (mode == InsetMath::UNDECIDED_MODE) {
1655                                         cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1656                                         parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1657                                 } else {
1658                                         cell->push_back(MathAtom(new InsetMathSplit(buf, name,
1659                                                         'c', !stared(name))));
1660                                         parse2(cell->back(), FLAG_END, mode, !stared(name));
1661                                 }
1662                         }
1663
1664                         else if (name == "flalign" || name == "flalign*") {
1665                                 if (mode != InsetMath::UNDECIDED_MODE) {
1666                                         error("bad math environment " + name);
1667                                         break;
1668                                 }
1669                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1670                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1671                         }
1672
1673                         else if (name == "alignat" || name == "alignat*") {
1674                                 if (mode != InsetMath::UNDECIDED_MODE) {
1675                                         error("bad math environment " + name);
1676                                         break;
1677                                 }
1678                                 // ignore this for a while
1679                                 getArg('{', '}');
1680                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlignAt)));
1681                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1682                         }
1683
1684                         else if (name == "xalignat" || name == "xalignat*") {
1685                                 if (mode != InsetMath::UNDECIDED_MODE) {
1686                                         error("bad math environment " + name);
1687                                         break;
1688                                 }
1689                                 // ignore this for a while
1690                                 getArg('{', '}');
1691                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullXAlignAt)));
1692                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1693                         }
1694
1695                         else if (name == "xxalignat") {
1696                                 if (mode != InsetMath::UNDECIDED_MODE) {
1697                                         error("bad math environment " + name);
1698                                         break;
1699                                 }
1700                                 // ignore this for a while
1701                                 getArg('{', '}');
1702                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1703                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1704                         }
1705
1706                         else if (name == "multline" || name == "multline*") {
1707                                 if (mode != InsetMath::UNDECIDED_MODE) {
1708                                         error("bad math environment " + name);
1709                                         break;
1710                                 }
1711                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1712                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1713                         }
1714
1715                         else if (name == "gather" || name == "gather*") {
1716                                 if (mode != InsetMath::UNDECIDED_MODE) {
1717                                         error("bad math environment " + name);
1718                                         break;
1719                                 }
1720                                 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1721                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1722                         }
1723
1724                         else if (latexkeys const * l = in_word_set(name)) {
1725                                 if (l->inset == "matrix") {
1726                                         cell->push_back(createInsetMath(name, buf));
1727                                         parse2(cell->back(), FLAG_END, mode, false);
1728                                 } else if (l->inset == "split") {
1729                                         docstring const valign = parse_verbatim_option() + 'c';
1730                                         cell->push_back(MathAtom(
1731                                                 new InsetMathSplit(buf, name, (char)valign[0])));
1732                                         parse2(cell->back(), FLAG_END, mode, false);
1733                                 } else {
1734                                         success_ = false;
1735                                         if (!(mode_ & Parse::QUIET)) {
1736                                                 dump();
1737                                                 lyxerr << "found math environment `"
1738                                                        << to_utf8(name)
1739                                                        << "' in symbols file with unsupported inset `"
1740                                                        << l->inset
1741                                                        << "'." << endl;
1742                                         }
1743                                         // create generic environment inset
1744                                         cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1745                                         parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1746                                 }
1747                         }
1748
1749                         else {
1750                                 success_ = false;
1751                                 if (!(mode_ & Parse::QUIET)) {
1752                                         dump();
1753                                         lyxerr << "found unknown math environment '"
1754                                                << to_utf8(name) << "'" << endl;
1755                                 }
1756                                 // create generic environment inset
1757                                 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1758                                 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1759                         }
1760                 }
1761
1762                 else if (t.cs() == "kern") {
1763                         // FIXME: A hack...
1764                         docstring s;
1765                         int num_tokens = 0;
1766                         while (true) {
1767                                 Token const & t = getToken();
1768                                 ++num_tokens;
1769                                 if (!good()) {
1770                                         s.clear();
1771                                         while (num_tokens--)
1772                                                 putback();
1773                                         break;
1774                                 }
1775                                 s += t.character();
1776                                 if (isValidLength(to_utf8(s)))
1777                                         break;
1778                         }
1779                         if (s.empty())
1780                                 cell->push_back(MathAtom(new InsetMathKern));
1781                         else
1782                                 cell->push_back(MathAtom(new InsetMathKern(s)));
1783                 }
1784
1785                 else if (t.cs() == "label") {
1786                         // FIXME: This is swallowed in inline formulas
1787                         docstring label = parse_verbatim_item();
1788                         MathData ar;
1789                         asArray(label, ar);
1790                         if (grid.asHullInset()) {
1791                                 grid.asHullInset()->label(cellrow, label);
1792                                 grid.asHullInset()->numbered(cellrow, true);
1793                         } else {
1794                                 cell->push_back(createInsetMath(t.cs(), buf));
1795                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1796                         }
1797                 }
1798
1799                 else if (t.cs() == "choose" || t.cs() == "over"
1800                                 || t.cs() == "atop" || t.cs() == "brace"
1801                                 || t.cs() == "brack") {
1802                         MathAtom at = createInsetMath(t.cs(), buf);
1803                         at.nucleus()->cell(0) = *cell;
1804                         cell->clear();
1805                         parse(at.nucleus()->cell(1), flags, mode);
1806                         cell->push_back(at);
1807                         return success_;
1808                 }
1809
1810                 else if (t.cs() == "color") {
1811                         docstring const color = parse_verbatim_item();
1812                         cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1813                         parse(cell->back().nucleus()->cell(0), flags, mode);
1814                         return success_;
1815                 }
1816
1817                 else if (t.cs() == "textcolor") {
1818                         docstring const color = parse_verbatim_item();
1819                         cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1820                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1821                 }
1822
1823                 else if (t.cs() == "normalcolor") {
1824                         cell->push_back(createInsetMath(t.cs(), buf));
1825                         parse(cell->back().nucleus()->cell(0), flags, mode);
1826                         return success_;
1827                 }
1828
1829                 else if (t.cs() == "substack") {
1830                         cell->push_back(createInsetMath(t.cs(), buf));
1831                         parse2(cell->back(), FLAG_ITEM, mode, false);
1832                         // Delete empty last row if present
1833                         InsetMathGrid & subgrid =
1834                                 *(cell->back().nucleus()->asGridInset());
1835                         if (subgrid.nrows() > 1)
1836                                 delEmptyLastRow(subgrid);
1837                 }
1838
1839                 else if (t.cs() == "xymatrix") {
1840                         odocstringstream os;
1841                         while (good() && nextToken().cat() != catBegin)
1842                                 os << getToken().asInput();
1843                         cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1844                         parse2(cell->back(), FLAG_ITEM, mode, false);
1845                         // Delete empty last row if present
1846                         InsetMathGrid & subgrid =
1847                                 *(cell->back().nucleus()->asGridInset());
1848                         if (subgrid.nrows() > 1)
1849                                 delEmptyLastRow(subgrid);
1850                 }
1851
1852                 else if (t.cs() == "Diagram") {
1853                         odocstringstream os;
1854                         while (good() && nextToken().cat() != catBegin)
1855                                 os << getToken().asInput();
1856                         cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1857                         parse2(cell->back(), FLAG_ITEM, mode, false);
1858                 }
1859
1860                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1861                         cell->push_back(createInsetMath(t.cs(), buf));
1862                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1863                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1864                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1865                 }
1866
1867                 else if (t.cs() == "tag") {
1868                         if (nextToken().character() == '*') {
1869                                 getToken();
1870                                 cell->push_back(createInsetMath(t.cs() + '*', buf));
1871                         } else
1872                                 cell->push_back(createInsetMath(t.cs(), buf));
1873                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1874                 }
1875
1876                 else if (t.cs() == "hspace") {
1877                         bool const prot =  nextToken().character() == '*';
1878                         if (prot)
1879                                 getToken();
1880                         docstring const name = t.cs();
1881                         docstring const arg = parse_verbatim_item();
1882                         Length length;
1883                         if (prot && arg == "\\fill")
1884                                 cell->push_back(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
1885                         else if (isValidLength(to_utf8(arg), &length))
1886                                 cell->push_back(MathAtom(new InsetMathSpace(length, prot)));
1887                         else {
1888                                 // Since the Length class cannot use length variables
1889                                 // we must not create an InsetMathSpace.
1890                                 cell->push_back(MathAtom(new MathMacro(buf, name)));
1891                                 MathData ar;
1892                                 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1893                                 cell->append(ar);
1894                         }
1895                 }
1896
1897                 else if (t.cs() == "smash") {
1898                         skipSpaces();
1899                         if (nextToken().asInput() == "[") {
1900                                 // Since the phantom inset cannot handle optional arguments
1901                                 // other than b and t, we must not create an InsetMathPhantom
1902                                 // if opt is different from b and t (bug 8967).
1903                                 docstring const opt = parse_verbatim_option();
1904                                 if (opt == "t" || opt == "b") {
1905                                         cell->push_back(createInsetMath(t.cs() + opt, buf));
1906                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1907                                 } else {
1908                                         docstring const arg = parse_verbatim_item();
1909                                         cell->push_back(MathAtom(new MathMacro(buf, t.cs())));
1910                                         MathData ar;
1911                                         mathed_parse_cell(ar, '[' + opt + ']', mode_);
1912                                         cell->append(ar);
1913                                         ar = MathData();
1914                                         mathed_parse_cell(ar, '{' + arg + '}', mode_);
1915                                         cell->append(ar);
1916                                 }
1917                         }
1918                         else {
1919                                 cell->push_back(createInsetMath(t.cs(), buf));
1920                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1921                         }
1922                 }
1923
1924 #if 0
1925                 else if (t.cs() == "infer") {
1926                         MathData ar;
1927                         parse(ar, FLAG_OPTION, mode);
1928                         cell->push_back(createInsetMath(t.cs(), buf));
1929                         parse2(cell->back(), FLAG_ITEM, mode, false);
1930                 }
1931
1932                 // Disabled
1933                 else if (1 && t.cs() == "ar") {
1934                         auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1935                         // try to read target
1936                         parse(p->cell(0), FLAG_OTPTION, mode);
1937                         // try to read label
1938                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1939                                 p->up_ = nextToken().cat() == catSuper;
1940                                 getToken();
1941                                 parse(p->cell(1), FLAG_ITEM, mode);
1942                                 //lyxerr << "read label: " << p->cell(1) << endl;
1943                         }
1944
1945                         cell->push_back(MathAtom(p.release()));
1946                         //lyxerr << "read cell: " << cell << endl;
1947                 }
1948 #endif
1949
1950                 else if (t.cs() == "lyxmathsym") {
1951                         skipSpaces();
1952                         if (getToken().cat() != catBegin) {
1953                                 error("'{' expected in \\" + t.cs());
1954                                 return success_;
1955                         }
1956                         int count = 0;
1957                         docstring cmd;
1958                         CatCode cat = nextToken().cat();
1959                         while (good() && (count || cat != catEnd)) {
1960                                 if (cat == catBegin)
1961                                         ++count;
1962                                 else if (cat == catEnd)
1963                                         --count;
1964                                 cmd += getToken().asInput();
1965                                 cat = nextToken().cat();
1966                         }
1967                         if (getToken().cat() != catEnd) {
1968                                 error("'}' expected in \\" + t.cs());
1969                                 return success_;
1970                         }
1971                         bool termination;
1972                         docstring rem;
1973                         do {
1974                                 cmd = Encodings::fromLaTeXCommand(cmd,
1975                                         Encodings::MATH_CMD | Encodings::TEXT_CMD,
1976                                         termination, rem);
1977                                 for (size_t i = 0; i < cmd.size(); ++i)
1978                                         cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1979                                 if (!rem.empty()) {
1980                                         char_type c = rem[0];
1981                                         cell->push_back(MathAtom(new InsetMathChar(c)));
1982                                         cmd = rem.substr(1);
1983                                         rem.clear();
1984                                 } else
1985                                         cmd.clear();
1986                         } while (!cmd.empty());
1987                 }
1988
1989                 else if (!t.cs().empty()) {
1990                         bool const no_mhchem =
1991                                 (t.cs() == "ce" || t.cs() == "cf")
1992                                 && buf && buf->params().use_package("mhchem") ==
1993                                                 BufferParams::package_off;
1994
1995                         bool const is_user_macro = no_mhchem ||
1996                                 (buf && (mode_ & Parse::TRACKMACRO
1997                                          ? buf->usermacros.count(t.cs()) != 0
1998                                          : buf->getMacro(t.cs(), false) != 0));
1999
2000                         latexkeys const * l = in_word_set(t.cs());
2001                         if (l && !is_user_macro) {
2002                                 if (l->inset == "big") {
2003                                         skipSpaces();
2004                                         docstring const delim = getToken().asInput();
2005                                         if (InsetMathBig::isBigInsetDelim(delim))
2006                                                 cell->push_back(MathAtom(
2007                                                         new InsetMathBig(t.cs(), delim)));
2008                                         else {
2009                                                 cell->push_back(createInsetMath(t.cs(), buf));
2010                                                 putback();
2011                                         }
2012                                 }
2013
2014                                 else if (l->inset == "font") {
2015                                         cell->push_back(createInsetMath(t.cs(), buf));
2016                                         parse(cell->back().nucleus()->cell(0),
2017                                                 FLAG_ITEM, asMode(mode, l->extra));
2018                                 }
2019
2020                                 else if (l->inset == "oldfont") {
2021                                         cell->push_back(createInsetMath(t.cs(), buf));
2022                                         parse(cell->back().nucleus()->cell(0),
2023                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
2024                                         if (prevToken().cat() != catAlign &&
2025                                             prevToken().cs() != "\\")
2026                                                 return success_;
2027                                         putback();
2028                                 }
2029
2030                                 else if (l->inset == "style") {
2031                                         cell->push_back(createInsetMath(t.cs(), buf));
2032                                         parse(cell->back().nucleus()->cell(0),
2033                                                 flags | FLAG_ALIGN, mode);
2034                                         if (prevToken().cat() != catAlign &&
2035                                             prevToken().cs() != "\\")
2036                                                 return success_;
2037                                         putback();
2038                                 }
2039
2040                                 else {
2041                                         MathAtom at = createInsetMath(t.cs(), buf);
2042                                         for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
2043                                                 parse(at.nucleus()->cell(i),
2044                                                         FLAG_ITEM, asMode(mode, l->extra));
2045                                         cell->push_back(at);
2046                                 }
2047                         }
2048
2049                         else {
2050                                 bool is_unicode_symbol = false;
2051                                 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
2052                                         int num_tokens = 0;
2053                                         docstring cmd = prevToken().asInput();
2054                                         CatCode cat = nextToken().cat();
2055                                         if (cat == catBegin) {
2056                                                 int count = 0;
2057                                                 while (good() && (count || cat != catEnd)) {
2058                                                         cat = nextToken().cat();
2059                                                         cmd += getToken().asInput();
2060                                                         ++num_tokens;
2061                                                         if (cat == catBegin)
2062                                                                 ++count;
2063                                                         else if (cat == catEnd)
2064                                                                 --count;
2065                                                 }
2066                                         }
2067                                         bool is_combining;
2068                                         bool termination;
2069                                         char_type c = Encodings::fromLaTeXCommand(cmd,
2070                                                 Encodings::MATH_CMD | Encodings::TEXT_CMD,
2071                                                 is_combining, termination);
2072                                         if (is_combining) {
2073                                                 if (cat == catLetter)
2074                                                         cmd += '{';
2075                                                 cmd += getToken().asInput();
2076                                                 ++num_tokens;
2077                                                 if (cat == catLetter)
2078                                                         cmd += '}';
2079                                                 c = Encodings::fromLaTeXCommand(cmd,
2080                                                         Encodings::MATH_CMD | Encodings::TEXT_CMD,
2081                                                         is_combining, termination);
2082                                         }
2083                                         if (c) {
2084                                                 if (termination) {
2085                                                         if (nextToken().cat() == catBegin) {
2086                                                                 getToken();
2087                                                                 if (nextToken().cat() == catEnd) {
2088                                                                         getToken();
2089                                                                         num_tokens += 2;
2090                                                                 } else
2091                                                                         putback();
2092                                                         } else {
2093                                                                 while (nextToken().cat() == catSpace) {
2094                                                                         getToken();
2095                                                                         ++num_tokens;
2096                                                                 }
2097                                                         }
2098                                                 }
2099                                                 is_unicode_symbol = true;
2100                                                 cell->push_back(MathAtom(new InsetMathChar(c)));
2101                                         } else {
2102                                                 while (num_tokens--)
2103                                                         putback();
2104                                         }
2105                                 }
2106                                 if (!is_unicode_symbol) {
2107                                         MathAtom at = is_user_macro ?
2108                                                 MathAtom(new MathMacro(buf, t.cs()))
2109                                                 : createInsetMath(t.cs(), buf);
2110                                         InsetMath::mode_type m = mode;
2111                                         //if (m == InsetMath::UNDECIDED_MODE)
2112                                         //lyxerr << "default creation: m1: " << m << endl;
2113                                         if (at->currentMode() != InsetMath::UNDECIDED_MODE)
2114                                                 m = at->currentMode();
2115                                         //lyxerr << "default creation: m2: " << m << endl;
2116                                         InsetMath::idx_type start = 0;
2117                                         // this fails on \bigg[...\bigg]
2118                                         //MathData opt;
2119                                         //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
2120                                         //if (!opt.empty()) {
2121                                         //      start = 1;
2122                                         //      at.nucleus()->cell(0) = opt;
2123                                         //}
2124                                         for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
2125                                                 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
2126                                                 if (mode == InsetMath::MATH_MODE)
2127                                                         skipSpaces();
2128                                         }
2129                                         cell->push_back(at);
2130                                 }
2131                         }
2132                 }
2133
2134
2135                 if (flags & FLAG_LEAVE) {
2136                         flags &= ~FLAG_LEAVE;
2137                         break;
2138                 }
2139         }
2140         return success_;
2141 }
2142
2143
2144
2145 } // anonymous namespace
2146
2147
2148 // FIXME This will likely need some work.
2149 char const * latexkeys::MathMLtype() const
2150 {
2151         if (extra == "mathord")
2152                 return "mi";
2153         return "mo";
2154 }
2155
2156
2157 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
2158 {
2159         return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
2160                                 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
2161 }
2162
2163
2164 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
2165 {
2166         return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
2167                                 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
2168 }
2169
2170
2171 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
2172                          Parse::flags f)
2173 {
2174         return Parser(str, f, buf).parse(t);
2175 }
2176
2177
2178 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
2179                          Parse::flags f)
2180 {
2181         return Parser(lex, f, buf).parse(t);
2182 }
2183
2184
2185 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
2186                          Parse::flags f)
2187 {
2188         return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
2189                         InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
2190 }
2191
2192
2193 void initParser()
2194 {
2195         fill(theCatcode, theCatcode + 128, catOther);
2196         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
2197         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
2198
2199         theCatcode[int('\\')] = catEscape;
2200         theCatcode[int('{')]  = catBegin;
2201         theCatcode[int('}')]  = catEnd;
2202         theCatcode[int('$')]  = catMath;
2203         theCatcode[int('&')]  = catAlign;
2204         theCatcode[int('\n')] = catNewline;
2205         theCatcode[int('#')]  = catParameter;
2206         theCatcode[int('^')]  = catSuper;
2207         theCatcode[int('_')]  = catSub;
2208         theCatcode[int(0x7f)] = catIgnore;
2209         theCatcode[int(' ')]  = catSpace;
2210         theCatcode[int('\t')] = catSpace;
2211         theCatcode[int('\r')] = catNewline;
2212         theCatcode[int('~')]  = catActive;
2213         theCatcode[int('%')]  = catComment;
2214 }
2215
2216
2217 } // namespace lyx