]> git.lyx.org Git - lyx.git/blob - src/mathed/MathParser.C
cursor is no more damaging the background. L-shaped cursor is broken right now....
[lyx.git] / src / mathed / MathParser.C
1 /**
2  * \file MathParser.C
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
30   \[\begin{array}{ccc}
31 1
32 &
33
34   \end{array}\]
35
36 */
37
38
39 #include <config.h>
40
41 #include "MathParser.h"
42
43 #include "InsetMathArray.h"
44 #include "InsetMathBig.h"
45 #include "InsetMathBrace.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathKern.h"
52 #include "InsetMathMacro.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 "lyxlex.h"
66 #include "debug.h"
67
68 #include "support/convert.h"
69
70 #include <sstream>
71
72
73 namespace lyx {
74
75 using std::endl;
76 using std::fill;
77
78 using std::string;
79 using std::ios;
80 using std::istream;
81 using std::istringstream;
82 using std::ostream;
83 using std::vector;
84
85
86 //#define FILEDEBUG
87
88
89 namespace {
90
91 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
92 {
93         //lyxerr << "handling mode: '" << str << "'" << endl;
94         if (str == "mathmode")
95                 return InsetMath::MATH_MODE;
96         if (str == "textmode" || str == "forcetext")
97                 return InsetMath::TEXT_MODE;
98         return oldmode;
99 }
100
101
102 bool stared(docstring const & s)
103 {
104         size_t const n = s.size();
105         return n && s[n - 1] == '*';
106 }
107
108
109 /*!
110  * Add the row \p cellrow to \p grid.
111  * \returns wether the row could be added. Adding a row can fail for
112  * environments like "equation" that have a fixed number of rows.
113  */
114 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
115             docstring const & vskip)
116 {
117         ++cellrow;
118         if (cellrow == grid.nrows()) {
119                 //lyxerr << "adding row " << cellrow << endl;
120                 grid.addRow(cellrow - 1);
121                 if (cellrow == grid.nrows()) {
122                         // We can't add a row to this grid, so let's
123                         // append the content of this cell to the previous
124                         // one.
125                         // This does not happen in well formed .lyx files,
126                         // but LyX versions 1.3.x and older could create
127                         // such files and tex2lyx can still do that.
128                         --cellrow;
129                         lyxerr << "ignoring extra row";
130                         if (!vskip.empty())
131                                 lyxerr << " with extra space " << to_utf8(vskip);
132                         lyxerr << '.' << endl;
133                         return false;
134                 }
135         }
136         grid.vcrskip(LyXLength(to_utf8(vskip)), cellrow - 1);
137         return true;
138 }
139
140
141 /*!
142  * Add the column \p cellcol to \p grid.
143  * \returns wether the column could be added. Adding a column can fail for
144  * environments like "eqnarray" that have a fixed number of columns.
145  */
146 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
147 {
148         ++cellcol;
149         if (cellcol == grid.ncols()) {
150                 //lyxerr << "adding column " << cellcol << endl;
151                 grid.addCol(cellcol - 1);
152                 if (cellcol == grid.ncols()) {
153                         // We can't add a column to this grid, so let's
154                         // append the content of this cell to the previous
155                         // one.
156                         // This does not happen in well formed .lyx files,
157                         // but LyX versions 1.3.x and older could create
158                         // such files and tex2lyx can still do that.
159                         --cellcol;
160                         lyxerr << "ignoring extra column." << endl;
161                         return false;
162                 }
163         }
164         return true;
165 }
166
167
168 /*!
169  * Check wether the last row is empty and remove it if yes.
170  * Otherwise the following code
171  * \verbatim
172 \begin{array}{|c|c|}
173 \hline
174 1 & 2 \\ \hline
175 3 & 4 \\ \hline
176 \end{array}
177  * \endverbatim
178  * will result in a grid with 3 rows (+ the dummy row that is always present),
179  * because the last '\\' opens a new row.
180  */
181 void delEmptyLastRow(InsetMathGrid & grid)
182 {
183         InsetMathGrid::row_type const row = grid.nrows() - 1;
184         for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
185                 if (!grid.cell(grid.index(row, col)).empty())
186                         return;
187         }
188         // Copy the row information of the empty row (which would contain the
189         // last hline in the example above) to the dummy row and delete the
190         // empty row.
191         grid.rowinfo(row + 1) = grid.rowinfo(row);
192         grid.delRow(row);
193 }
194
195
196 // These are TeX's catcodes
197 enum CatCode {
198         catEscape,     // 0    backslash
199         catBegin,      // 1    {
200         catEnd,        // 2    }
201         catMath,       // 3    $
202         catAlign,      // 4    &
203         catNewline,    // 5    ^^M
204         catParameter,  // 6    #
205         catSuper,      // 7    ^
206         catSub,        // 8    _
207         catIgnore,     // 9
208         catSpace,      // 10   space
209         catLetter,     // 11   a-zA-Z
210         catOther,      // 12   none of the above
211         catActive,     // 13   ~
212         catComment,    // 14   %
213         catInvalid     // 15   <delete>
214 };
215
216 CatCode theCatcode[256];
217
218
219 inline CatCode catcode(unsigned char c)
220 {
221         return theCatcode[c];
222 }
223
224
225 enum {
226         FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
227         FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
228         FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
229         FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
230         FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
231         FLAG_TEXTMODE   = 1 << 5,  //  we are in a box
232         FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
233         FLAG_LEAVE      = 1 << 7,  //  leave the loop at the end
234         FLAG_SIMPLE     = 1 << 8,  //  next $ leaves the loop
235         FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
236         FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
237         FLAG_OPTION     = 1 << 11, //  read [...] style option
238         FLAG_BRACED     = 1 << 12  //  read {...} style argument
239 };
240
241
242 //
243 // Helper class for parsing
244 //
245
246 class Token {
247 public:
248         ///
249         Token() : cs_(), char_(0), cat_(catIgnore) {}
250         ///
251         Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
252         ///
253         Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
254
255         ///
256         docstring const & cs() const { return cs_; }
257         ///
258         CatCode cat() const { return cat_; }
259         ///
260         char_type character() const { return char_; }
261         ///
262         docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
263         ///
264         docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
265
266 private:
267         ///
268         docstring cs_;
269         ///
270         char_type char_;
271         ///
272         CatCode cat_;
273 };
274
275 ostream & operator<<(ostream & os, Token const & t)
276 {
277         if (t.cs().size())
278                 os << '\\' << t.cs();
279         else if (t.cat() == catLetter)
280                 os << t.character();
281         else
282                 os << '[' << t.character() << ',' << t.cat() << ']';
283         return os;
284 }
285
286
287 class Parser {
288 public:
289         ///
290         typedef  InsetMath::mode_type mode_type;
291
292         ///
293         Parser(LyXLex & lex);
294         ///
295         Parser(istream & is);
296
297         ///
298         bool parse(MathAtom & at);
299         ///
300         void parse(MathArray & array, unsigned flags, mode_type mode);
301         ///
302         void parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
303                 bool numbered);
304         ///
305         MathArray parse(unsigned flags, mode_type mode);
306         ///
307         int lineno() const { return lineno_; }
308         ///
309         void putback();
310
311 private:
312         ///
313         void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
314         /// get arg delimited by 'left' and 'right'
315         docstring getArg(char_type left, char_type right);
316         ///
317         char_type getChar();
318         ///
319         void error(string const & msg);
320         void error(docstring const & msg) { error(to_utf8(msg)); }
321         /// dump contents to screen
322         void dump() const;
323         ///
324         void tokenize(istream & is);
325         ///
326         void tokenize(docstring const & s);
327         ///
328         void skipSpaceTokens(idocstream & is, char_type c);
329         ///
330         void push_back(Token const & t);
331         ///
332         void pop_back();
333         ///
334         Token const & prevToken() const;
335         ///
336         Token const & nextToken() const;
337         ///
338         Token const & getToken();
339         /// skips spaces if any
340         void skipSpaces();
341         ///
342         void lex(docstring const & s);
343         ///
344         bool good() const;
345         ///
346         docstring parse_verbatim_item();
347         ///
348         docstring parse_verbatim_option();
349
350         ///
351         int lineno_;
352         ///
353         vector<Token> tokens_;
354         ///
355         unsigned pos_;
356         /// Stack of active environments
357         vector<docstring> environments_;
358 };
359
360
361 Parser::Parser(LyXLex & lexer)
362         : lineno_(lexer.getLineNo()), pos_(0)
363 {
364         tokenize(lexer.getStream());
365         lexer.eatLine();
366 }
367
368
369 Parser::Parser(istream & is)
370         : lineno_(0), pos_(0)
371 {
372         tokenize(is);
373 }
374
375
376 void Parser::push_back(Token const & t)
377 {
378         tokens_.push_back(t);
379 }
380
381
382 void Parser::pop_back()
383 {
384         tokens_.pop_back();
385 }
386
387
388 Token const & Parser::prevToken() const
389 {
390         static const Token dummy;
391         return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
392 }
393
394
395 Token const & Parser::nextToken() const
396 {
397         static const Token dummy;
398         return good() ? tokens_[pos_] : dummy;
399 }
400
401
402 Token const & Parser::getToken()
403 {
404         static const Token dummy;
405         //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
406         return good() ? tokens_[pos_++] : dummy;
407 }
408
409
410 void Parser::skipSpaces()
411 {
412         while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
413                 getToken();
414 }
415
416
417 void Parser::putback()
418 {
419         --pos_;
420 }
421
422
423 bool Parser::good() const
424 {
425         return pos_ < tokens_.size();
426 }
427
428
429 char_type Parser::getChar()
430 {
431         if (!good())
432                 error("The input stream is not well...");
433         return tokens_[pos_++].character();
434 }
435
436
437 docstring Parser::getArg(char_type left, char_type right)
438 {
439         skipSpaces();
440
441         docstring result;
442         char_type c = getChar();
443
444         if (c != left)
445                 putback();
446         else
447                 while ((c = getChar()) != right && good())
448                         result += c;
449
450         return result;
451 }
452
453
454 void Parser::skipSpaceTokens(idocstream & is, char_type c)
455 {
456         // skip trailing spaces
457         while (catcode(c) == catSpace || catcode(c) == catNewline)
458                 if (!is.get(c))
459                         break;
460         //lyxerr << "putting back: " << c << endl;
461         is.putback(c);
462 }
463
464
465 void Parser::tokenize(istream & is)
466 {
467         // eat everything up to the next \end_inset or end of stream
468         // and store it in s for further tokenization
469         std::string s;
470         char c;
471         while (is.get(c)) {
472                 s += c;
473                 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
474                         s = s.substr(0, s.size() - 10);
475                         break;
476                 }
477         }
478         // Remove the space after \end_inset
479         if (is.get(c) && c != ' ')
480                 is.unget();
481
482         // tokenize buffer
483         tokenize(from_utf8(s));
484 }
485
486
487 void Parser::tokenize(docstring const & buffer)
488 {
489         idocstringstream is(buffer, ios::in | ios::binary);
490
491         char_type c;
492         while (is.get(c)) {
493                 //lyxerr << "reading c: " << c << endl;
494
495                 switch (catcode(c)) {
496                         case catNewline: {
497                                 ++lineno_;
498                                 is.get(c);
499                                 if (catcode(c) == catNewline)
500                                         ; //push_back(Token("par"));
501                                 else {
502                                         push_back(Token('\n', catNewline));
503                                         is.putback(c);
504                                 }
505                                 break;
506                         }
507
508 /*
509                         case catComment: {
510                                 while (is.get(c) && catcode(c) != catNewline)
511                                         ;
512                                 ++lineno_;
513                                 break;
514                         }
515 */
516
517                         case catEscape: {
518                                 is.get(c);
519                                 if (!is) {
520                                         error("unexpected end of input");
521                                 } else {
522                                         docstring s(1, c);
523                                         if (catcode(c) == catLetter) {
524                                                 // collect letters
525                                                 while (is.get(c) && catcode(c) == catLetter)
526                                                         s += c;
527                                                 skipSpaceTokens(is, c);
528                                         }
529                                         push_back(Token(s));
530                                 }
531                                 break;
532                         }
533
534                         case catSuper:
535                         case catSub: {
536                                 push_back(Token(c, catcode(c)));
537                                 is.get(c);
538                                 skipSpaceTokens(is, c);
539                                 break;
540                         }
541
542                         case catIgnore: {
543                                 lyxerr << "ignoring a char: " << int(c) << endl;
544                                 break;
545                         }
546
547                         default:
548                                 push_back(Token(c, catcode(c)));
549                 }
550         }
551
552 #ifdef FILEDEBUG
553         dump();
554 #endif
555 }
556
557
558 void Parser::dump() const
559 {
560         lyxerr << "\nTokens: ";
561         for (unsigned i = 0; i < tokens_.size(); ++i) {
562                 if (i == pos_)
563                         lyxerr << " <#> ";
564                 lyxerr << tokens_[i];
565         }
566         lyxerr << " pos: " << pos_ << endl;
567 }
568
569
570 void Parser::error(string const & msg)
571 {
572         lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
573         dump();
574         //exit(1);
575 }
576
577
578 bool Parser::parse(MathAtom & at)
579 {
580         skipSpaces();
581         MathArray ar;
582         parse(ar, false, InsetMath::UNDECIDED_MODE);
583         if (ar.size() != 1 || ar.front()->getType() == hullNone) {
584                 lyxerr << "unusual contents found: " << ar << endl;
585                 at = MathAtom(new InsetMathPar(ar));
586                 //if (at->nargs() > 0)
587                 //      at.nucleus()->cell(0) = ar;
588                 //else
589                 //      lyxerr << "unusual contents found: " << ar << endl;
590                 return true;
591         }
592         at = ar[0];
593         return true;
594 }
595
596
597 docstring Parser::parse_verbatim_option()
598 {
599         skipSpaces();
600         docstring res;
601         if (nextToken().character() == '[') {
602                 Token t = getToken();
603                 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
604                         if (t.cat() == catBegin) {
605                                 putback();
606                                 res += '{' + parse_verbatim_item() + '}';
607                         } else
608                                 res += t.asString();
609                 }
610         }
611         return res;
612 }
613
614
615 docstring Parser::parse_verbatim_item()
616 {
617         skipSpaces();
618         docstring res;
619         if (nextToken().cat() == catBegin) {
620                 Token t = getToken();
621                 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
622                         if (t.cat() == catBegin) {
623                                 putback();
624                                 res += '{' + parse_verbatim_item() + '}';
625                         }
626                         else
627                                 res += t.asString();
628                 }
629         }
630         return res;
631 }
632
633
634 MathArray Parser::parse(unsigned flags, mode_type mode)
635 {
636         MathArray ar;
637         parse(ar, flags, mode);
638         return ar;
639 }
640
641
642 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
643 {
644         InsetMathGrid grid(1, 1);
645         parse1(grid, flags, mode, false);
646         array = grid.cell(0);
647 }
648
649
650 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
651         const bool numbered)
652 {
653         parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
654 }
655
656
657 void Parser::parse1(InsetMathGrid & grid, unsigned flags,
658         const mode_type mode, const bool numbered)
659 {
660         int limits = 0;
661         InsetMathGrid::row_type cellrow = 0;
662         InsetMathGrid::col_type cellcol = 0;
663         MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
664
665         if (grid.asHullInset())
666                 grid.asHullInset()->numbered(cellrow, numbered);
667
668         //dump();
669         //lyxerr << " flags: " << flags << endl;
670         //lyxerr << " mode: " << mode  << endl;
671         //lyxerr << "grid: " << grid << endl;
672
673         while (good()) {
674                 Token const & t = getToken();
675
676 #ifdef FILEDEBUG
677                 lyxerr << "t: " << t << " flags: " << flags << endl;
678                 lyxerr << "mode: " << mode  << endl;
679                 cell->dump();
680                 lyxerr << endl;
681 #endif
682
683                 if (flags & FLAG_ITEM) {
684
685                         if (t.cat() == catBegin) {
686                                 // skip the brace and collect everything to the next matching
687                                 // closing brace
688                                 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
689                                 return;
690                         }
691
692                         // handle only this single token, leave the loop if done
693                         flags = FLAG_LEAVE;
694                 }
695
696
697                 if (flags & FLAG_BRACED) {
698                         if (t.cat() == catSpace)
699                                 continue;
700
701                         if (t.cat() != catBegin) {
702                                 error("opening brace expected");
703                                 return;
704                         }
705
706                         // skip the brace and collect everything to the next matching
707                         // closing brace
708                         flags = FLAG_BRACE_LAST;
709                 }
710
711
712                 if (flags & FLAG_OPTION) {
713                         if (t.cat() == catOther && t.character() == '[') {
714                                 MathArray ar;
715                                 parse(ar, FLAG_BRACK_LAST, mode);
716                                 cell->append(ar);
717                         } else {
718                                 // no option found, put back token and we are done
719                                 putback();
720                         }
721                         return;
722                 }
723
724                 //
725                 // cat codes
726                 //
727                 if (t.cat() == catMath) {
728                         if (mode != InsetMath::MATH_MODE) {
729                                 // we are inside some text mode thingy, so opening new math is allowed
730                                 Token const & n = getToken();
731                                 if (n.cat() == catMath) {
732                                         // TeX's $$...$$ syntax for displayed math
733                                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
734                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
735                                         getToken(); // skip the second '$' token
736                                 } else {
737                                         // simple $...$  stuff
738                                         putback();
739                                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
740                                         parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
741                                 }
742                         }
743
744                         else if (flags & FLAG_SIMPLE) {
745                                 // this is the end of the formula
746                                 return;
747                         }
748
749                         else {
750                                 error("something strange in the parser");
751                                 break;
752                         }
753                 }
754
755                 else if (t.cat() == catLetter)
756                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
757
758                 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
759                         if (cell->empty() || cell->back()->getChar() != ' ')
760                                 cell->push_back(MathAtom(new InsetMathChar(t.character())));
761                 }
762
763                 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
764                         if (cell->empty() || cell->back()->getChar() != ' ')
765                                 cell->push_back(MathAtom(new InsetMathChar(' ')));
766                 }
767
768                 else if (t.cat() == catParameter) {
769                         Token const & n = getToken();
770                         cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
771                 }
772
773                 else if (t.cat() == catActive)
774                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
775
776                 else if (t.cat() == catBegin) {
777                         MathArray ar;
778                         parse(ar, FLAG_BRACE_LAST, mode);
779                         // do not create a BraceInset if they were written by LyX
780                         // this helps to keep the annoyance of  "a choose b"  to a minimum
781                         if (ar.size() == 1 && ar[0]->extraBraces())
782                                 cell->append(ar);
783                         else
784                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
785                 }
786
787                 else if (t.cat() == catEnd) {
788                         if (flags & FLAG_BRACE_LAST)
789                                 return;
790                         error("found '}' unexpectedly");
791                         //BOOST_ASSERT(false);
792                         //add(cell, '}', LM_TC_TEX);
793                 }
794
795                 else if (t.cat() == catAlign) {
796                         //lyxerr << " column now " << (cellcol + 1)
797                         //       << " max: " << grid.ncols() << endl;
798                         if (flags & FLAG_ALIGN)
799                                 return;
800                         if (addCol(grid, cellcol))
801                                 cell = &grid.cell(grid.index(cellrow, cellcol));
802                 }
803
804                 else if (t.cat() == catSuper || t.cat() == catSub) {
805                         bool up = (t.cat() == catSuper);
806                         // we need no new script inset if the last thing was a scriptinset,
807                         // which has that script already not the same script already
808                         if (!cell->size())
809                                 cell->push_back(MathAtom(new InsetMathScript(up)));
810                         else if (cell->back()->asScriptInset() &&
811                                         !cell->back()->asScriptInset()->has(up))
812                                 cell->back().nucleus()->asScriptInset()->ensure(up);
813                         else if (cell->back()->asScriptInset())
814                                 cell->push_back(MathAtom(new InsetMathScript(up)));
815                         else
816                                 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
817                         InsetMathScript * p = cell->back().nucleus()->asScriptInset();
818                         // special handling of {}-bases
819                         // is this always correct?
820                         if (p->nuc().size() == 1 
821                             && p->nuc().back()->asBraceInset())
822                                 p->nuc() = p->nuc().back()->asNestInset()->cell(0);
823                         parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
824                         if (limits) {
825                                 p->limits(limits);
826                                 limits = 0;
827                         }
828                 }
829
830                 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
831                         //lyxerr << "finished reading option" << endl;
832                         return;
833                 }
834
835                 else if (t.cat() == catOther)
836                         cell->push_back(MathAtom(new InsetMathChar(t.character())));
837
838                 else if (t.cat() == catComment) {
839                         docstring s;
840                         while (good()) {
841                                 Token const & t = getToken();
842                                 if (t.cat() == catNewline)
843                                         break;
844                                 s += t.asString();
845                         }
846                         cell->push_back(MathAtom(new InsetMathComment(s)));
847                         skipSpaces();
848                 }
849
850                 //
851                 // control sequences
852                 //
853
854                 else if (t.cs() == "lyxlock") {
855                         if (cell->size())
856                                 cell->back().nucleus()->lock(true);
857                 }
858
859                 else if (t.cs() == "def" ||
860                         t.cs() == "newcommand" ||
861                         t.cs() == "renewcommand")
862                 {
863                         docstring const type = t.cs();
864                         docstring name;
865                         int nargs = 0;
866                         if (t.cs() == "def") {
867                                 // get name
868                                 name = getToken().cs();
869
870                                 // read parameter
871                                 docstring pars;
872                                 while (good() && nextToken().cat() != catBegin) {
873                                         pars += getToken().cs();
874                                         ++nargs;
875                                 }
876                                 nargs /= 2;
877                                 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
878
879                         } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
880
881                                 if (getToken().cat() != catBegin) {
882                                         error("'{' in \\newcommand expected (1) ");
883                                         return;
884                                 }
885
886                                 name = getToken().cs();
887
888                                 if (getToken().cat() != catEnd) {
889                                         error("'}' in \\newcommand expected");
890                                         return;
891                                 }
892
893                                 docstring const arg  = getArg('[', ']');
894                                 if (!arg.empty())
895                                         nargs = convert<int>(arg);
896
897                         }
898
899                         MathArray ar1;
900                         parse(ar1, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
901
902                         // we cannot handle recursive stuff at all
903                         //MathArray test;
904                         //test.push_back(createInsetMath(name));
905                         //if (ar1.contains(test)) {
906                         //      error("we cannot handle recursive macros at all.");
907                         //      return;
908                         //}
909
910                         // is a version for display attached?
911                         skipSpaces();
912                         MathArray ar2;
913                         if (nextToken().cat() == catBegin)
914                                 parse(ar2, FLAG_ITEM, InsetMath::MATH_MODE);
915
916                         cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
917                                 ar1, ar2)));
918                 }
919
920                 else if (t.cs() == "(") {
921                         cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
922                         parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
923                 }
924
925                 else if (t.cs() == "[") {
926                         cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
927                         parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
928                 }
929
930                 else if (t.cs() == "protect")
931                         // ignore \\protect, will hopefully be re-added during output
932                         ;
933
934                 else if (t.cs() == "end") {
935                         if (flags & FLAG_END) {
936                                 // eat environment name
937                                 docstring const name = getArg('{', '}');
938                                 if (environments_.empty())
939                                         error("'found \\end{" + name +
940                                               "}' without matching '\\begin{" +
941                                               name + "}'");
942                                 else if (name != environments_.back())
943                                         error("'\\end{" + name +
944                                               "}' does not match '\\begin{" +
945                                               environments_.back() + "}'");
946                                 else {
947                                         environments_.pop_back();
948                                         // Delete empty last row in matrix
949                                         // like insets.
950                                         // If you abuse InsetMathGrid for
951                                         // non-matrix like structures you
952                                         // probably need to refine this test.
953                                         // Right now we only have to test for
954                                         // single line hull insets.
955                                         if (grid.nrows() > 1)
956                                                 delEmptyLastRow(grid);
957                                         return;
958                                 }
959                         } else
960                                 error("found 'end' unexpectedly");
961                 }
962
963                 else if (t.cs() == ")") {
964                         if (flags & FLAG_SIMPLE2)
965                                 return;
966                         error("found '\\)' unexpectedly");
967                 }
968
969                 else if (t.cs() == "]") {
970                         if (flags & FLAG_EQUATION)
971                                 return;
972                         error("found '\\]' unexpectedly");
973                 }
974
975                 else if (t.cs() == "\\") {
976                         if (flags & FLAG_ALIGN)
977                                 return;
978                         if (addRow(grid, cellrow, getArg('[', ']'))) {
979                                 cellcol = 0;
980                                 if (grid.asHullInset())
981                                         grid.asHullInset()->numbered(
982                                                         cellrow, numbered);
983                                 cell = &grid.cell(grid.index(cellrow,
984                                                              cellcol));
985                         }
986                 }
987
988 #if 0
989                 else if (t.cs() == "multicolumn") {
990                         // extract column count and insert dummy cells
991                         MathArray count;
992                         parse(count, FLAG_ITEM, mode);
993                         int cols = 1;
994                         if (!extractNumber(count, cols)) {
995                                 lyxerr << " can't extract number of cells from " << count << endl;
996                         }
997                         // resize the table if necessary
998                         for (int i = 0; i < cols; ++i) {
999                                 if (addCol(grid, cellcol)) {
1000                                         cell = &grid.cell(grid.index(
1001                                                         cellrow, cellcol));
1002                                         // mark this as dummy
1003                                         grid.cellinfo(grid.index(
1004                                                 cellrow, cellcol)).dummy_ = true;
1005                                 }
1006                         }
1007                         // the last cell is the real thing, not a dummy
1008                         grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1009
1010                         // read special alignment
1011                         MathArray align;
1012                         parse(align, FLAG_ITEM, mode);
1013                         //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1014
1015                         // parse the remaining contents into the "real" cell
1016                         parse(*cell, FLAG_ITEM, mode);
1017                 }
1018 #endif
1019
1020                 else if (t.cs() == "limits")
1021                         limits = 1;
1022
1023                 else if (t.cs() == "nolimits")
1024                         limits = -1;
1025
1026                 else if (t.cs() == "nonumber") {
1027                         if (grid.asHullInset())
1028                                 grid.asHullInset()->numbered(cellrow, false);
1029                 }
1030
1031                 else if (t.cs() == "number") {
1032                         if (grid.asHullInset())
1033                                 grid.asHullInset()->numbered(cellrow, true);
1034                 }
1035
1036                 else if (t.cs() == "hline") {
1037                         grid.rowinfo(cellrow).lines_ ++;
1038                 }
1039
1040                 else if (t.cs() == "sqrt") {
1041                         MathArray ar;
1042                         parse(ar, FLAG_OPTION, mode);
1043                         if (ar.size()) {
1044                                 cell->push_back(MathAtom(new InsetMathRoot));
1045                                 cell->back().nucleus()->cell(0) = ar;
1046                                 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1047                         } else {
1048                                 cell->push_back(MathAtom(new InsetMathSqrt));
1049                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1050                         }
1051                 }
1052
1053                 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1054                         cell->push_back(createInsetMath(t.cs()));
1055                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1056                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1057                 }
1058
1059                 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1060                                 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1061                         cell->push_back(MathAtom(new RefInset(t.cs())));
1062                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1063                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1064                 }
1065
1066                 else if (t.cs() == "left") {
1067                         skipSpaces();
1068                         Token const & tl = getToken();
1069                         // \| and \Vert are equivalent, and InsetMathDelim
1070                         // can't handle \|
1071                         // FIXME: fix this in InsetMathDelim itself!
1072                         docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1073                         MathArray ar;
1074                         parse(ar, FLAG_RIGHT, mode);
1075                         skipSpaces();
1076                         Token const & tr = getToken();
1077                         docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1078                         cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1079                 }
1080
1081                 else if (t.cs() == "right") {
1082                         if (flags & FLAG_RIGHT)
1083                                 return;
1084                         //lyxerr << "got so far: '" << cell << "'" << endl;
1085                         error("Unmatched right delimiter");
1086                         return;
1087                 }
1088
1089                 else if (t.cs() == "begin") {
1090                         docstring const name = getArg('{', '}');
1091                         environments_.push_back(name);
1092
1093                         if (name == "array" || name == "subarray") {
1094                                 docstring const valign = parse_verbatim_option() + 'c';
1095                                 docstring const halign = parse_verbatim_item();
1096                                 cell->push_back(MathAtom(new InsetMathArray(name, (char)valign[0], halign)));
1097                                 parse2(cell->back(), FLAG_END, mode, false);
1098                         }
1099
1100                         else if (name == "tabular") {
1101                                 docstring const valign = parse_verbatim_option() + 'c';
1102                                 docstring const halign = parse_verbatim_item();
1103                                 cell->push_back(MathAtom(new InsetMathTabular(name, (char)valign[0], halign)));
1104                                 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1105                         }
1106
1107                         else if (name == "split" || name == "cases") {
1108                                 cell->push_back(createInsetMath(name));
1109                                 parse2(cell->back(), FLAG_END, mode, false);
1110                         }
1111
1112                         else if (name == "alignedat") {
1113                                 docstring const valign = parse_verbatim_option() + 'c';
1114                                 // ignore this for a while
1115                                 getArg('{', '}');
1116                                 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1117                                 parse2(cell->back(), FLAG_END, mode, false);
1118                         }
1119
1120                         else if (name == "math") {
1121                                 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1122                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1123                         }
1124
1125                         else if (name == "equation" || name == "equation*"
1126                                         || name == "displaymath") {
1127                                 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1128                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1129                         }
1130
1131                         else if (name == "eqnarray" || name == "eqnarray*") {
1132                                 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1133                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1134                         }
1135
1136                         else if (name == "align" || name == "align*") {
1137                                 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1138                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1139                         }
1140
1141                         else if (name == "flalign" || name == "flalign*") {
1142                                 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1143                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1144                         }
1145
1146                         else if (name == "alignat" || name == "alignat*") {
1147                                 // ignore this for a while
1148                                 getArg('{', '}');
1149                                 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1150                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1151                         }
1152
1153                         else if (name == "xalignat" || name == "xalignat*") {
1154                                 // ignore this for a while
1155                                 getArg('{', '}');
1156                                 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1157                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1158                         }
1159
1160                         else if (name == "xxalignat") {
1161                                 // ignore this for a while
1162                                 getArg('{', '}');
1163                                 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1164                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1165                         }
1166
1167                         else if (name == "multline" || name == "multline*") {
1168                                 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1169                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1170                         }
1171
1172                         else if (name == "gather" || name == "gather*") {
1173                                 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1174                                 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1175                         }
1176
1177                         else if (latexkeys const * l = in_word_set(name)) {
1178                                 if (l->inset == "matrix") {
1179                                         cell->push_back(createInsetMath(name));
1180                                         parse2(cell->back(), FLAG_END, mode, false);
1181                                 } else if (l->inset == "split") {
1182                                         docstring const valign = parse_verbatim_option() + 'c';
1183                                         cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1184                                         parse2(cell->back(), FLAG_END, mode, false);
1185                                 } else {
1186                                         dump();
1187                                         lyxerr << "found math environment `" << name
1188                                                << "' in symbols file with unsupported inset `"
1189                                                << l->inset << "'." << endl;
1190                                         // create generic environment inset
1191                                         cell->push_back(MathAtom(new InsetMathEnv(name)));
1192                                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1193                                 }
1194                         }
1195
1196                         else {
1197                                 dump();
1198                                 lyxerr << "found unknown math environment '" << name << "'" << endl;
1199                                 // create generic environment inset
1200                                 cell->push_back(MathAtom(new InsetMathEnv(name)));
1201                                 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1202                         }
1203                 }
1204
1205                 else if (t.cs() == "kern") {
1206 #ifdef WITH_WARNINGS
1207 #warning A hack...
1208 #endif
1209                         docstring s;
1210                         while (true) {
1211                                 Token const & t = getToken();
1212                                 if (!good()) {
1213                                         putback();
1214                                         break;
1215                                 }
1216                                 s += t.character();
1217                                 if (isValidLength(to_utf8(s)))
1218                                         break;
1219                         }
1220                         cell->push_back(MathAtom(new InsetMathKern(s)));
1221                 }
1222
1223                 else if (t.cs() == "label") {
1224                         // FIXME: This is swallowed in inline formulas
1225                         docstring label = parse_verbatim_item();
1226                         MathArray ar;
1227                         asArray(label, ar);
1228                         if (grid.asHullInset()) {
1229                                 grid.asHullInset()->label(cellrow, label);
1230                         } else {
1231                                 cell->push_back(createInsetMath(t.cs()));
1232                                 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1233                         }
1234                 }
1235
1236                 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1237                         MathAtom at = createInsetMath(t.cs());
1238                         at.nucleus()->cell(0) = *cell;
1239                         cell->clear();
1240                         parse(at.nucleus()->cell(1), flags, mode);
1241                         cell->push_back(at);
1242                         return;
1243                 }
1244
1245                 else if (t.cs() == "color") {
1246                         docstring const color = parse_verbatim_item();
1247                         cell->push_back(MathAtom(new InsetMathColor(true, color)));
1248                         parse(cell->back().nucleus()->cell(0), flags, mode);
1249                         return;
1250                 }
1251
1252                 else if (t.cs() == "textcolor") {
1253                         docstring const color = parse_verbatim_item();
1254                         cell->push_back(MathAtom(new InsetMathColor(false, color)));
1255                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1256                 }
1257
1258                 else if (t.cs() == "normalcolor") {
1259                         cell->push_back(createInsetMath(t.cs()));
1260                         parse(cell->back().nucleus()->cell(0), flags, mode);
1261                         return;
1262                 }
1263
1264                 else if (t.cs() == "substack") {
1265                         cell->push_back(createInsetMath(t.cs()));
1266                         parse2(cell->back(), FLAG_ITEM, mode, false);
1267                 }
1268
1269                 else if (t.cs() == "xymatrix") {
1270                         cell->push_back(createInsetMath(t.cs()));
1271                         parse2(cell->back(), FLAG_ITEM, mode, false);
1272                 }
1273
1274                 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1275                         cell->push_back(createInsetMath(t.cs()));
1276                         parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1277                         parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1278                         parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1279                 }
1280
1281                 else if (t.cs() == "tag") {
1282                         if (nextToken().character() == '*') {
1283                                 getToken();
1284                                 cell->push_back(createInsetMath(t.cs() + '*'));
1285                         } else
1286                                 cell->push_back(createInsetMath(t.cs()));
1287                         parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1288                 }
1289
1290 #if 0
1291                 else if (t.cs() == "infer") {
1292                         MathArray ar;
1293                         parse(ar, FLAG_OPTION, mode);
1294                         cell->push_back(createInsetMath(t.cs()));
1295                         parse2(cell->back(), FLAG_ITEM, mode, false);
1296                 }
1297
1298                 // Disabled
1299                 else if (1 && t.cs() == "ar") {
1300                         auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1301                         // try to read target
1302                         parse(p->cell(0), FLAG_OTPTION, mode);
1303                         // try to read label
1304                         if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1305                                 p->up_ = nextToken().cat() == catSuper;
1306                                 getToken();
1307                                 parse(p->cell(1), FLAG_ITEM, mode);
1308                                 //lyxerr << "read label: " << p->cell(1) << endl;
1309                         }
1310
1311                         cell->push_back(MathAtom(p.release()));
1312                         //lyxerr << "read cell: " << cell << endl;
1313                 }
1314 #endif
1315
1316                 else if (t.cs().size()) {
1317                         latexkeys const * l = in_word_set(t.cs());
1318                         if (l) {
1319                                 if (l->inset == "big") {
1320                                         skipSpaces();
1321                                         docstring const delim = getToken().asInput();
1322                                         if (InsetMathBig::isBigInsetDelim(delim))
1323                                                 cell->push_back(MathAtom(
1324                                                         new InsetMathBig(t.cs(), delim)));
1325                                         else {
1326                                                 cell->push_back(createInsetMath(t.cs()));
1327                                                 putback();
1328                                         }
1329                                 }
1330
1331                                 else if (l->inset == "font") {
1332                                         cell->push_back(createInsetMath(t.cs()));
1333                                         parse(cell->back().nucleus()->cell(0),
1334                                                 FLAG_ITEM, asMode(mode, l->extra));
1335                                 }
1336
1337                                 else if (l->inset == "oldfont") {
1338                                         cell->push_back(createInsetMath(t.cs()));
1339                                         parse(cell->back().nucleus()->cell(0),
1340                                                 flags | FLAG_ALIGN, asMode(mode, l->extra));
1341                                         if (prevToken().cat() != catAlign &&
1342                                             prevToken().cs() != "\\")
1343                                                 return;
1344                                         putback();
1345                                 }
1346
1347                                 else if (l->inset == "style") {
1348                                         cell->push_back(createInsetMath(t.cs()));
1349                                         parse(cell->back().nucleus()->cell(0),
1350                                                 flags | FLAG_ALIGN, mode);
1351                                         if (prevToken().cat() != catAlign &&
1352                                             prevToken().cs() != "\\")
1353                                                 return;
1354                                         putback();
1355                                 }
1356
1357                                 else {
1358                                         MathAtom at = createInsetMath(t.cs());
1359                                         for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1360                                                 parse(at.nucleus()->cell(i),
1361                                                         FLAG_ITEM, asMode(mode, l->extra));
1362                                         cell->push_back(at);
1363                                 }
1364                         }
1365
1366                         else {
1367                                 MathAtom at = createInsetMath(t.cs());
1368                                 InsetMath::mode_type m = mode;
1369                                 //if (m == InsetMath::UNDECIDED_MODE)
1370                                 //lyxerr << "default creation: m1: " << m << endl;
1371                                 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1372                                         m = at->currentMode();
1373                                 //lyxerr << "default creation: m2: " << m << endl;
1374                                 InsetMath::idx_type start = 0;
1375                                 // this fails on \bigg[...\bigg]
1376                                 //MathArray opt;
1377                                 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1378                                 //if (opt.size()) {
1379                                 //      start = 1;
1380                                 //      at.nucleus()->cell(0) = opt;
1381                                 //}
1382                                 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1383                                         parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1384                                         skipSpaces();
1385                                 }
1386                                 cell->push_back(at);
1387                         }
1388                 }
1389
1390
1391                 if (flags & FLAG_LEAVE) {
1392                         flags &= ~FLAG_LEAVE;
1393                         break;
1394                 }
1395         }
1396 }
1397
1398
1399
1400 } // anonymous namespace
1401
1402
1403 void mathed_parse_cell(MathArray & ar, docstring const & str)
1404 {
1405         istringstream is(to_utf8(str));
1406         mathed_parse_cell(ar, is);
1407 }
1408
1409
1410 void mathed_parse_cell(MathArray & ar, istream & is)
1411 {
1412         Parser(is).parse(ar, 0, InsetMath::MATH_MODE);
1413 }
1414
1415
1416 bool mathed_parse_normal(MathAtom & t, string const & str)
1417 {
1418         istringstream is(str);
1419         return Parser(is).parse(t);
1420 }
1421
1422
1423 bool mathed_parse_normal(MathAtom & t, istream & is)
1424 {
1425         return Parser(is).parse(t);
1426 }
1427
1428
1429 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1430 {
1431         return Parser(lex).parse(t);
1432 }
1433
1434
1435 void mathed_parse_normal(InsetMathGrid & grid, string const & str)
1436 {
1437         istringstream is(str);
1438         Parser(is).parse1(grid, 0, InsetMath::MATH_MODE, false);
1439 }
1440
1441
1442 void initParser()
1443 {
1444         fill(theCatcode, theCatcode + 256, catOther);
1445         fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1446         fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1447
1448         theCatcode[int('\\')] = catEscape;
1449         theCatcode[int('{')]  = catBegin;
1450         theCatcode[int('}')]  = catEnd;
1451         theCatcode[int('$')]  = catMath;
1452         theCatcode[int('&')]  = catAlign;
1453         theCatcode[int('\n')] = catNewline;
1454         theCatcode[int('#')]  = catParameter;
1455         theCatcode[int('^')]  = catSuper;
1456         theCatcode[int('_')]  = catSub;
1457         theCatcode[int(0x7f)] = catIgnore;
1458         theCatcode[int(' ')]  = catSpace;
1459         theCatcode[int('\t')] = catSpace;
1460         theCatcode[int('\r')] = catNewline;
1461         theCatcode[int('~')]  = catActive;
1462         theCatcode[int('%')]  = catComment;
1463 }
1464
1465
1466 } // namespace lyx