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