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