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