3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
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:
20 \def\makeamptab{\catcode`\&=4\relax}
21 \def\makeampletter{\catcode`\&=11\relax}
22 \def\b{\makeampletter\expandafter\makeamptab\bi}
41 #include "math_parser.h"
42 #include "math_arrayinset.h"
43 #include "math_biginset.h"
44 #include "math_braceinset.h"
45 #include "math_charinset.h"
46 #include "math_colorinset.h"
47 #include "math_commentinset.h"
48 #include "math_deliminset.h"
49 #include "math_envinset.h"
50 #include "math_factory.h"
51 #include "math_kerninset.h"
52 #include "math_macro.h"
53 #include "math_macroarg.h"
54 #include "math_macrotemplate.h"
55 #include "math_parinset.h"
56 #include "math_rootinset.h"
57 #include "math_scriptinset.h"
58 #include "math_sqrtinset.h"
59 #include "math_support.h"
60 #include "math_tabularinset.h"
62 //#include "insets/insetref.h"
63 #include "ref_inset.h"
68 #include "support/convert.h"
78 using std::istringstream;
88 MathInset::mode_type asMode(MathInset::mode_type oldmode, string const & str)
90 //lyxerr << "handling mode: '" << str << "'" << endl;
91 if (str == "mathmode")
92 return MathInset::MATH_MODE;
93 if (str == "textmode" || str == "forcetext")
94 return MathInset::TEXT_MODE;
99 bool stared(string const & s)
101 string::size_type const n = s.size();
102 return n && s[n - 1] == '*';
107 * Add the row \p cellrow to \p grid.
108 * \returns wether the row could be added. Adding a row can fail for
109 * environments like "equation" that have a fixed number of rows.
111 bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow,
112 string const & vskip)
115 if (cellrow == grid.nrows()) {
116 //lyxerr << "adding row " << cellrow << endl;
117 grid.addRow(cellrow - 1);
118 if (cellrow == grid.nrows()) {
119 // We can't add a row to this grid, so let's
120 // append the content of this cell to the previous
122 // This does not happen in well formed .lyx files,
123 // but LyX versions 1.3.x and older could create
124 // such files and tex2lyx can still do that.
126 lyxerr << "ignoring extra row";
128 lyxerr << " with extra space " << vskip;
129 lyxerr << '.' << endl;
133 grid.vcrskip(LyXLength(vskip), cellrow - 1);
139 * Add the column \p cellcol to \p grid.
140 * \returns wether the column could be added. Adding a column can fail for
141 * environments like "eqnarray" that have a fixed number of columns.
143 bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol)
146 if (cellcol == grid.ncols()) {
147 //lyxerr << "adding column " << cellcol << endl;
148 grid.addCol(cellcol - 1);
149 if (cellcol == grid.ncols()) {
150 // We can't add a column to this grid, so let's
151 // append the content of this cell to the previous
153 // This does not happen in well formed .lyx files,
154 // but LyX versions 1.3.x and older could create
155 // such files and tex2lyx can still do that.
157 lyxerr << "ignoring extra column." << endl;
166 * Check wether the last row is empty and remove it if yes.
167 * Otherwise the following code
175 * will result in a grid with 3 rows (+ the dummy row that is always present),
176 * because the last '\\' opens a new row.
178 void delEmptyLastRow(MathGridInset & grid)
180 MathGridInset::row_type const row = grid.nrows() - 1;
181 for (MathGridInset::col_type col = 0; col < grid.ncols(); ++col) {
182 if (!grid.cell(grid.index(row, col)).empty())
185 // Copy the row information of the empty row (which would contain the
186 // last hline in the example above) to the dummy row and delete the
188 grid.rowinfo(row + 1) = grid.rowinfo(row);
193 // These are TeX's catcodes
195 catEscape, // 0 backslash
205 catSpace, // 10 space
206 catLetter, // 11 a-zA-Z
207 catOther, // 12 none of the above
210 catInvalid // 15 <delete>
213 CatCode theCatcode[256];
216 inline CatCode catcode(unsigned char c)
218 return theCatcode[c];
223 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
224 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
225 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
226 FLAG_END = 1 << 3, // next \\end ends the parsing process
227 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
228 FLAG_TEXTMODE = 1 << 5, // we are in a box
229 FLAG_ITEM = 1 << 6, // read a (possibly braced token)
230 FLAG_LEAVE = 1 << 7, // leave the loop at the end
231 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
232 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
233 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
234 FLAG_OPTION = 1 << 11, // read [...] style option
235 FLAG_BRACED = 1 << 12 // read {...} style argument
240 // Helper class for parsing
246 Token() : cs_(), char_(0), cat_(catIgnore) {}
248 Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
250 Token(string const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
253 string const & cs() const { return cs_; }
255 CatCode cat() const { return cat_; }
257 char character() const { return char_; }
259 string asString() const { return cs_.size() ? cs_ : string(1, char_); }
261 string asInput() const { return cs_.size() ? '\\' + cs_ : string(1, char_); }
272 ostream & operator<<(ostream & os, Token const & t)
275 os << '\\' << t.cs();
276 else if (t.cat() == catLetter)
279 os << '[' << t.character() << ',' << t.cat() << ']';
287 typedef MathInset::mode_type mode_type;
290 Parser(LyXLex & lex);
292 Parser(istream & is);
295 bool parse(MathAtom & at);
297 void parse(MathArray & array, unsigned flags, mode_type mode);
299 void parse1(MathGridInset & grid, unsigned flags, mode_type mode,
302 MathArray parse(unsigned flags, mode_type mode);
304 int lineno() const { return lineno_; }
310 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
311 /// get arg delimited by 'left' and 'right'
312 string getArg(char left, char right);
316 void error(string const & msg);
317 /// dump contents to screen
320 void tokenize(istream & is);
322 void tokenize(string const & s);
324 void skipSpaceTokens(istream & is, char c);
326 void push_back(Token const & t);
330 Token const & prevToken() const;
332 Token const & nextToken() const;
334 Token const & getToken();
335 /// skips spaces if any
338 void lex(string const & s);
342 string parse_verbatim_item();
344 string parse_verbatim_option();
349 vector<Token> tokens_;
352 /// Stack of active environments
353 vector<string> environments_;
357 Parser::Parser(LyXLex & lexer)
358 : lineno_(lexer.getLineNo()), pos_(0)
360 tokenize(lexer.getStream());
365 Parser::Parser(istream & is)
366 : lineno_(0), pos_(0)
372 void Parser::push_back(Token const & t)
374 tokens_.push_back(t);
378 void Parser::pop_back()
384 Token const & Parser::prevToken() const
386 static const Token dummy;
387 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
391 Token const & Parser::nextToken() const
393 static const Token dummy;
394 return good() ? tokens_[pos_] : dummy;
398 Token const & Parser::getToken()
400 static const Token dummy;
401 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
402 return good() ? tokens_[pos_++] : dummy;
406 void Parser::skipSpaces()
408 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
413 void Parser::putback()
419 bool Parser::good() const
421 return pos_ < tokens_.size();
425 char Parser::getChar()
428 error("The input stream is not well...");
429 return tokens_[pos_++].character();
433 string Parser::getArg(char left, char right)
443 while ((c = getChar()) != right && good())
450 void Parser::skipSpaceTokens(istream & is, char c)
452 // skip trailing spaces
453 while (catcode(c) == catSpace || catcode(c) == catNewline)
456 //lyxerr << "putting back: " << c << endl;
461 void Parser::tokenize(istream & is)
463 // eat everything up to the next \end_inset or end of stream
464 // and store it in s for further tokenization
469 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
470 s = s.substr(0, s.size() - 10);
474 // Remove the space after \end_inset
475 if (is.get(c) && c != ' ')
483 void Parser::tokenize(string const & buffer)
485 istringstream is(buffer, ios::in | ios::binary);
489 //lyxerr << "reading c: " << c << endl;
491 switch (catcode(c)) {
495 if (catcode(c) == catNewline)
496 ; //push_back(Token("par"));
498 push_back(Token('\n', catNewline));
506 while (is.get(c) && catcode(c) != catNewline)
516 error("unexpected end of input");
519 if (catcode(c) == catLetter) {
521 while (is.get(c) && catcode(c) == catLetter)
523 skipSpaceTokens(is, c);
532 push_back(Token(c, catcode(c)));
534 skipSpaceTokens(is, c);
539 lyxerr << "ignoring a char: " << int(c) << endl;
544 push_back(Token(c, catcode(c)));
554 void Parser::dump() const
556 lyxerr << "\nTokens: ";
557 for (unsigned i = 0; i < tokens_.size(); ++i) {
560 lyxerr << tokens_[i];
562 lyxerr << " pos: " << pos_ << endl;
566 void Parser::error(string const & msg)
568 lyxerr << "Line ~" << lineno_ << ": Math parse error: " << msg << endl;
574 bool Parser::parse(MathAtom & at)
578 parse(ar, false, MathInset::UNDECIDED_MODE);
579 if (ar.size() != 1 || ar.front()->getType() == "none") {
580 lyxerr << "unusual contents found: " << ar << endl;
581 at = MathAtom(new MathParInset(ar));
582 //if (at->nargs() > 0)
583 // at.nucleus()->cell(0) = ar;
585 // lyxerr << "unusual contents found: " << ar << endl;
593 string Parser::parse_verbatim_option()
597 if (nextToken().character() == '[') {
598 Token t = getToken();
599 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
600 if (t.cat() == catBegin) {
602 res += '{' + parse_verbatim_item() + '}';
611 string Parser::parse_verbatim_item()
615 if (nextToken().cat() == catBegin) {
616 Token t = getToken();
617 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
618 if (t.cat() == catBegin) {
620 res += '{' + parse_verbatim_item() + '}';
630 MathArray Parser::parse(unsigned flags, mode_type mode)
633 parse(ar, flags, mode);
638 void Parser::parse(MathArray & array, unsigned flags, mode_type mode)
640 MathGridInset grid(1, 1);
641 parse1(grid, flags, mode, false);
642 array = grid.cell(0);
646 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
649 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
653 void Parser::parse1(MathGridInset & grid, unsigned flags,
654 const mode_type mode, const bool numbered)
657 MathGridInset::row_type cellrow = 0;
658 MathGridInset::col_type cellcol = 0;
659 MathArray * cell = &grid.cell(grid.index(cellrow, cellcol));
661 if (grid.asHullInset())
662 grid.asHullInset()->numbered(cellrow, numbered);
665 //lyxerr << " flags: " << flags << endl;
666 //lyxerr << " mode: " << mode << endl;
667 //lyxerr << "grid: " << grid << endl;
670 Token const & t = getToken();
673 lyxerr << "t: " << t << " flags: " << flags << endl;
674 lyxerr << "mode: " << mode << endl;
679 if (flags & FLAG_ITEM) {
681 if (t.cat() == catBegin) {
682 // skip the brace and collect everything to the next matching
684 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
688 // handle only this single token, leave the loop if done
693 if (flags & FLAG_BRACED) {
694 if (t.cat() == catSpace)
697 if (t.cat() != catBegin) {
698 error("opening brace expected");
702 // skip the brace and collect everything to the next matching
704 flags = FLAG_BRACE_LAST;
708 if (flags & FLAG_OPTION) {
709 if (t.cat() == catOther && t.character() == '[') {
711 parse(ar, FLAG_BRACK_LAST, mode);
714 // no option found, put back token and we are done
723 if (t.cat() == catMath) {
724 if (mode != MathInset::MATH_MODE) {
725 // we are inside some text mode thingy, so opening new math is allowed
726 Token const & n = getToken();
727 if (n.cat() == catMath) {
728 // TeX's $$...$$ syntax for displayed math
729 cell->push_back(MathAtom(new MathHullInset("equation")));
730 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
731 getToken(); // skip the second '$' token
733 // simple $...$ stuff
735 cell->push_back(MathAtom(new MathHullInset("simple")));
736 parse2(cell->back(), FLAG_SIMPLE, MathInset::MATH_MODE, false);
740 else if (flags & FLAG_SIMPLE) {
741 // this is the end of the formula
746 error("something strange in the parser");
751 else if (t.cat() == catLetter)
752 cell->push_back(MathAtom(new MathCharInset(t.character())));
754 else if (t.cat() == catSpace && mode != MathInset::MATH_MODE) {
755 if (cell->empty() || cell->back()->getChar() != ' ')
756 cell->push_back(MathAtom(new MathCharInset(t.character())));
759 else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) {
760 if (cell->empty() || cell->back()->getChar() != ' ')
761 cell->push_back(MathAtom(new MathCharInset(' ')));
764 else if (t.cat() == catParameter) {
765 Token const & n = getToken();
766 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
769 else if (t.cat() == catActive)
770 cell->push_back(MathAtom(new MathCharInset(t.character())));
772 else if (t.cat() == catBegin) {
774 parse(ar, FLAG_BRACE_LAST, mode);
775 // do not create a BraceInset if they were written by LyX
776 // this helps to keep the annoyance of "a choose b" to a minimum
777 if (ar.size() == 1 && ar[0]->extraBraces())
780 cell->push_back(MathAtom(new MathBraceInset(ar)));
783 else if (t.cat() == catEnd) {
784 if (flags & FLAG_BRACE_LAST)
786 error("found '}' unexpectedly");
787 //BOOST_ASSERT(false);
788 //add(cell, '}', LM_TC_TEX);
791 else if (t.cat() == catAlign) {
792 //lyxerr << " column now " << (cellcol + 1)
793 // << " max: " << grid.ncols() << endl;
794 if (flags & FLAG_ALIGN)
796 if (addCol(grid, cellcol))
797 cell = &grid.cell(grid.index(cellrow, cellcol));
800 else if (t.cat() == catSuper || t.cat() == catSub) {
801 bool up = (t.cat() == catSuper);
802 // we need no new script inset if the last thing was a scriptinset,
803 // which has that script already not the same script already
805 cell->push_back(MathAtom(new MathScriptInset(up)));
806 else if (cell->back()->asScriptInset() &&
807 !cell->back()->asScriptInset()->has(up))
808 cell->back().nucleus()->asScriptInset()->ensure(up);
809 else if (cell->back()->asScriptInset())
810 cell->push_back(MathAtom(new MathScriptInset(up)));
812 cell->back() = MathAtom(new MathScriptInset(cell->back(), up));
813 MathScriptInset * p = cell->back().nucleus()->asScriptInset();
814 // special handling of {}-bases
815 // is this always correct?
816 // It appears that this is wrong (Dekel)
817 //if (p->nuc().size() == 1 && p->nuc().back()->asNestInset() &&
818 // p->nuc().back()->extraBraces())
819 // p->nuc() = p->nuc().back()->asNestInset()->cell(0);
820 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
827 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
828 //lyxerr << "finished reading option" << endl;
832 else if (t.cat() == catOther)
833 cell->push_back(MathAtom(new MathCharInset(t.character())));
835 else if (t.cat() == catComment) {
838 Token const & t = getToken();
839 if (t.cat() == catNewline)
843 cell->push_back(MathAtom(new MathCommentInset(s)));
851 else if (t.cs() == "lyxlock") {
853 cell->back().nucleus()->lock(true);
856 else if (t.cs() == "def" ||
857 t.cs() == "newcommand" ||
858 t.cs() == "renewcommand")
860 string const type = t.cs();
863 if (t.cs() == "def") {
865 name = getToken().cs();
869 while (good() && nextToken().cat() != catBegin) {
870 pars += getToken().cs();
874 //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
876 } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
878 if (getToken().cat() != catBegin) {
879 error("'{' in \\newcommand expected (1) ");
883 name = getToken().cs();
885 if (getToken().cat() != catEnd) {
886 error("'}' in \\newcommand expected");
890 string const arg = getArg('[', ']');
892 nargs = convert<int>(arg);
897 parse(ar1, FLAG_ITEM, MathInset::UNDECIDED_MODE);
899 // we cannot handle recursive stuff at all
901 //test.push_back(createMathInset(name));
902 //if (ar1.contains(test)) {
903 // error("we cannot handle recursive macros at all.");
907 // is a version for display attached?
910 if (nextToken().cat() == catBegin)
911 parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
913 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
917 else if (t.cs() == "(") {
918 cell->push_back(MathAtom(new MathHullInset("simple")));
919 parse2(cell->back(), FLAG_SIMPLE2, MathInset::MATH_MODE, false);
922 else if (t.cs() == "[") {
923 cell->push_back(MathAtom(new MathHullInset("equation")));
924 parse2(cell->back(), FLAG_EQUATION, MathInset::MATH_MODE, false);
927 else if (t.cs() == "protect")
928 // ignore \\protect, will hopefully be re-added during output
931 else if (t.cs() == "end") {
932 if (flags & FLAG_END) {
933 // eat environment name
934 string const name = getArg('{', '}');
935 if (environments_.empty())
936 error("'found \\end{" + name +
937 "}' without matching '\\begin{" +
939 else if (name != environments_.back())
940 error("'\\end{" + name +
941 "}' does not match '\\begin{" +
942 environments_.back() + "}'");
944 environments_.pop_back();
945 // Delete empty last row in matrix
947 // If you abuse MathGridInset for
948 // non-matrix like structures you
949 // probably need to refine this test.
950 // Right now we only have to test for
951 // single line hull insets.
952 if (grid.nrows() > 1)
953 delEmptyLastRow(grid);
957 error("found 'end' unexpectedly");
960 else if (t.cs() == ")") {
961 if (flags & FLAG_SIMPLE2)
963 error("found '\\)' unexpectedly");
966 else if (t.cs() == "]") {
967 if (flags & FLAG_EQUATION)
969 error("found '\\]' unexpectedly");
972 else if (t.cs() == "\\") {
973 if (flags & FLAG_ALIGN)
975 if (addRow(grid, cellrow, getArg('[', ']'))) {
977 if (grid.asHullInset())
978 grid.asHullInset()->numbered(
980 cell = &grid.cell(grid.index(cellrow,
986 else if (t.cs() == "multicolumn") {
987 // extract column count and insert dummy cells
989 parse(count, FLAG_ITEM, mode);
991 if (!extractNumber(count, cols)) {
992 lyxerr << " can't extract number of cells from " << count << endl;
994 // resize the table if necessary
995 for (int i = 0; i < cols; ++i) {
996 if (addCol(grid, cellcol)) {
997 cell = &grid.cell(grid.index(
999 // mark this as dummy
1000 grid.cellinfo(grid.index(
1001 cellrow, cellcol)).dummy_ = true;
1004 // the last cell is the real thing, not a dummy
1005 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1007 // read special alignment
1009 parse(align, FLAG_ITEM, mode);
1010 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1012 // parse the remaining contents into the "real" cell
1013 parse(*cell, FLAG_ITEM, mode);
1017 else if (t.cs() == "limits")
1020 else if (t.cs() == "nolimits")
1023 else if (t.cs() == "nonumber") {
1024 if (grid.asHullInset())
1025 grid.asHullInset()->numbered(cellrow, false);
1028 else if (t.cs() == "number") {
1029 if (grid.asHullInset())
1030 grid.asHullInset()->numbered(cellrow, true);
1033 else if (t.cs() == "hline") {
1034 grid.rowinfo(cellrow).lines_ ++;
1037 else if (t.cs() == "sqrt") {
1039 parse(ar, FLAG_OPTION, mode);
1041 cell->push_back(MathAtom(new MathRootInset));
1042 cell->back().nucleus()->cell(0) = ar;
1043 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1045 cell->push_back(MathAtom(new MathSqrtInset));
1046 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1050 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1051 cell->push_back(createMathInset(t.cs()));
1052 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1053 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1056 else if (t.cs() == "ref" || t.cs() == "prettyref" ||
1057 t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1058 cell->push_back(MathAtom(new RefInset(t.cs())));
1059 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1060 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1063 else if (t.cs() == "left") {
1065 Token const & tl = getToken();
1066 // \| and \Vert are equivalent, and MathDelimInset
1068 // FIXME: fix this in MathDelimInset itself!
1069 string const l = tl.cs() == "|" ? "Vert" : tl.asString();
1071 parse(ar, FLAG_RIGHT, mode);
1073 Token const & tr = getToken();
1074 string const r = tr.cs() == "|" ? "Vert" : tr.asString();
1075 cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
1078 else if (t.cs() == "right") {
1079 if (flags & FLAG_RIGHT)
1081 //lyxerr << "got so far: '" << cell << "'" << endl;
1082 error("Unmatched right delimiter");
1086 else if (t.cs() == "begin") {
1087 string const name = getArg('{', '}');
1088 environments_.push_back(name);
1090 if (name == "array" || name == "subarray") {
1091 string const valign = parse_verbatim_option() + 'c';
1092 string const halign = parse_verbatim_item();
1093 cell->push_back(MathAtom(new MathArrayInset(name, valign[0], halign)));
1094 parse2(cell->back(), FLAG_END, mode, false);
1097 else if (name == "tabular") {
1098 string const valign = parse_verbatim_option() + 'c';
1099 string const halign = parse_verbatim_item();
1100 cell->push_back(MathAtom(new MathTabularInset(name, valign[0], halign)));
1101 parse2(cell->back(), FLAG_END, MathInset::TEXT_MODE, false);
1104 else if (name == "split" || name == "cases" ||
1105 name == "gathered" || name == "aligned") {
1106 cell->push_back(createMathInset(name));
1107 parse2(cell->back(), FLAG_END, mode, false);
1110 else if (name == "alignedat") {
1111 // ignore this for a while
1113 cell->push_back(createMathInset(name));
1114 parse2(cell->back(), FLAG_END, mode, false);
1117 else if (name == "math") {
1118 cell->push_back(MathAtom(new MathHullInset("simple")));
1119 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, true);
1122 else if (name == "equation" || name == "equation*"
1123 || name == "displaymath") {
1124 cell->push_back(MathAtom(new MathHullInset("equation")));
1125 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, (name == "equation"));
1128 else if (name == "eqnarray" || name == "eqnarray*") {
1129 cell->push_back(MathAtom(new MathHullInset("eqnarray")));
1130 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1133 else if (name == "align" || name == "align*") {
1134 cell->push_back(MathAtom(new MathHullInset("align")));
1135 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1138 else if (name == "flalign" || name == "flalign*") {
1139 cell->push_back(MathAtom(new MathHullInset("flalign")));
1140 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1143 else if (name == "alignat" || name == "alignat*") {
1144 // ignore this for a while
1146 cell->push_back(MathAtom(new MathHullInset("alignat")));
1147 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1150 else if (name == "xalignat" || name == "xalignat*") {
1151 // ignore this for a while
1153 cell->push_back(MathAtom(new MathHullInset("xalignat")));
1154 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1157 else if (name == "xxalignat") {
1158 // ignore this for a while
1160 cell->push_back(MathAtom(new MathHullInset("xxalignat")));
1161 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1164 else if (name == "multline" || name == "multline*") {
1165 cell->push_back(MathAtom(new MathHullInset("multline")));
1166 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1169 else if (name == "gather" || name == "gather*") {
1170 cell->push_back(MathAtom(new MathHullInset("gather")));
1171 parse2(cell->back(), FLAG_END, MathInset::MATH_MODE, !stared(name));
1174 else if (latexkeys const * l = in_word_set(name)) {
1175 if (l->inset == "matrix") {
1176 cell->push_back(createMathInset(name));
1177 parse2(cell->back(), FLAG_END, mode, false);
1183 lyxerr << "found unknown math environment '" << name << "'" << endl;
1184 // create generic environment inset
1185 cell->push_back(MathAtom(new MathEnvInset(name)));
1186 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1190 else if (t.cs() == "kern") {
1191 #ifdef WITH_WARNINGS
1196 Token const & t = getToken();
1202 if (isValidLength(s))
1205 cell->push_back(MathAtom(new MathKernInset(s)));
1208 else if (t.cs() == "label") {
1209 // FIXME: This is swallowed in inline formulas
1210 string label = parse_verbatim_item();
1213 if (grid.asHullInset()) {
1214 grid.asHullInset()->label(cellrow, label);
1216 cell->push_back(createMathInset(t.cs()));
1217 cell->push_back(MathAtom(new MathBraceInset(ar)));
1221 else if (t.cs() == "choose" || t.cs() == "over" || t.cs() == "atop") {
1222 MathAtom at = createMathInset(t.cs());
1223 at.nucleus()->cell(0) = *cell;
1225 parse(at.nucleus()->cell(1), flags, mode);
1226 cell->push_back(at);
1230 else if (t.cs() == "color") {
1231 string const color = parse_verbatim_item();
1232 cell->push_back(MathAtom(new MathColorInset(true, color)));
1233 parse(cell->back().nucleus()->cell(0), flags, mode);
1237 else if (t.cs() == "textcolor") {
1238 string const color = parse_verbatim_item();
1239 cell->push_back(MathAtom(new MathColorInset(false, color)));
1240 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1243 else if (t.cs() == "normalcolor") {
1244 cell->push_back(createMathInset(t.cs()));
1245 parse(cell->back().nucleus()->cell(0), flags, mode);
1249 else if (t.cs() == "substack") {
1250 cell->push_back(createMathInset(t.cs()));
1251 parse2(cell->back(), FLAG_ITEM, mode, false);
1254 else if (t.cs() == "xymatrix") {
1255 cell->push_back(createMathInset(t.cs()));
1256 parse2(cell->back(), FLAG_ITEM, mode, false);
1259 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1260 cell->push_back(createMathInset(t.cs()));
1261 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
1262 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, MathInset::TEXT_MODE);
1263 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, MathInset::TEXT_MODE);
1266 else if (t.cs() == "tag") {
1267 if (nextToken().character() == '*') {
1269 cell->push_back(createMathInset(t.cs() + '*'));
1271 cell->push_back(createMathInset(t.cs()));
1272 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
1276 else if (t.cs() == "infer") {
1278 parse(ar, FLAG_OPTION, mode);
1279 cell->push_back(createMathInset(t.cs()));
1280 parse2(cell->back(), FLAG_ITEM, mode, false);
1284 else if (1 && t.cs() == "ar") {
1285 auto_ptr<MathXYArrowInset> p(new MathXYArrowInset);
1286 // try to read target
1287 parse(p->cell(0), FLAG_OTPTION, mode);
1288 // try to read label
1289 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1290 p->up_ = nextToken().cat() == catSuper;
1292 parse(p->cell(1), FLAG_ITEM, mode);
1293 //lyxerr << "read label: " << p->cell(1) << endl;
1296 cell->push_back(MathAtom(p.release()));
1297 //lyxerr << "read cell: " << cell << endl;
1301 else if (t.cs().size()) {
1302 latexkeys const * l = in_word_set(t.cs());
1304 if (l->inset == "big") {
1306 string const delim = getToken().asInput();
1307 if (MathBigInset::isBigInsetDelim(delim))
1308 cell->push_back(MathAtom(
1309 new MathBigInset(t.cs(), delim)));
1311 cell->push_back(createMathInset(t.cs()));
1312 cell->push_back(createMathInset(
1317 else if (l->inset == "font") {
1318 cell->push_back(createMathInset(t.cs()));
1319 parse(cell->back().nucleus()->cell(0),
1320 FLAG_ITEM, asMode(mode, l->extra));
1323 else if (l->inset == "oldfont") {
1324 cell->push_back(createMathInset(t.cs()));
1325 parse(cell->back().nucleus()->cell(0),
1326 flags | FLAG_ALIGN, asMode(mode, l->extra));
1327 if (prevToken().cat() != catAlign &&
1328 prevToken().cs() != "\\")
1333 else if (l->inset == "style") {
1334 cell->push_back(createMathInset(t.cs()));
1335 parse(cell->back().nucleus()->cell(0),
1336 flags | FLAG_ALIGN, mode);
1337 if (prevToken().cat() != catAlign &&
1338 prevToken().cs() != "\\")
1344 MathAtom at = createMathInset(t.cs());
1345 for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
1346 parse(at.nucleus()->cell(i),
1347 FLAG_ITEM, asMode(mode, l->extra));
1348 cell->push_back(at);
1353 MathAtom at = createMathInset(t.cs());
1354 MathInset::mode_type m = mode;
1355 //if (m == MathInset::UNDECIDED_MODE)
1356 //lyxerr << "default creation: m1: " << m << endl;
1357 if (at->currentMode() != MathInset::UNDECIDED_MODE)
1358 m = at->currentMode();
1359 //lyxerr << "default creation: m2: " << m << endl;
1360 MathInset::idx_type start = 0;
1361 // this fails on \bigg[...\bigg]
1363 //parse(opt, FLAG_OPTION, MathInset::VERBATIM_MODE);
1366 // at.nucleus()->cell(0) = opt;
1368 for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
1369 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1372 cell->push_back(at);
1377 if (flags & FLAG_LEAVE) {
1378 flags &= ~FLAG_LEAVE;
1386 } // anonymous namespace
1389 void mathed_parse_cell(MathArray & ar, string const & str)
1391 istringstream is(str);
1392 mathed_parse_cell(ar, is);
1396 void mathed_parse_cell(MathArray & ar, istream & is)
1398 Parser(is).parse(ar, 0, MathInset::MATH_MODE);
1402 bool mathed_parse_normal(MathAtom & t, string const & str)
1404 istringstream is(str);
1405 return Parser(is).parse(t);
1409 bool mathed_parse_normal(MathAtom & t, istream & is)
1411 return Parser(is).parse(t);
1415 bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
1417 return Parser(lex).parse(t);
1421 void mathed_parse_normal(MathGridInset & grid, string const & str)
1423 istringstream is(str);
1424 Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
1430 fill(theCatcode, theCatcode + 256, catOther);
1431 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1432 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1434 theCatcode[int('\\')] = catEscape;
1435 theCatcode[int('{')] = catBegin;
1436 theCatcode[int('}')] = catEnd;
1437 theCatcode[int('$')] = catMath;
1438 theCatcode[int('&')] = catAlign;
1439 theCatcode[int('\n')] = catNewline;
1440 theCatcode[int('#')] = catParameter;
1441 theCatcode[int('^')] = catSuper;
1442 theCatcode[int('_')] = catSub;
1443 theCatcode[int(0x7f)] = catIgnore;
1444 theCatcode[int(' ')] = catSpace;
1445 theCatcode[int('\t')] = catSpace;
1446 theCatcode[int('\r')] = catNewline;
1447 theCatcode[int('~')] = catActive;
1448 theCatcode[int('%')] = catComment;