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}
40 #include "MathParser.h"
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathChar.h"
46 #include "InsetMathColor.h"
47 #include "InsetMathComment.h"
48 #include "InsetMathDelim.h"
49 #include "InsetMathEnsureMath.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathFrac.h"
52 #include "InsetMathKern.h"
53 #include "MathMacro.h"
54 #include "InsetMathPar.h"
55 #include "InsetMathRef.h"
56 #include "InsetMathRoot.h"
57 #include "InsetMathScript.h"
58 #include "InsetMathSpace.h"
59 #include "InsetMathSplit.h"
60 #include "InsetMathSqrt.h"
61 #include "InsetMathTabular.h"
62 #include "MathMacroTemplate.h"
63 #include "MathFactory.h"
64 #include "MathMacroArgument.h"
65 #include "MathSupport.h"
71 #include "support/debug.h"
72 #include "support/convert.h"
73 #include "support/docstream.h"
85 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
87 //lyxerr << "handling mode: '" << str << "'" << endl;
88 if (str == "mathmode")
89 return InsetMath::MATH_MODE;
90 if (str == "textmode" || str == "forcetext")
91 return InsetMath::TEXT_MODE;
96 bool stared(docstring const & s)
98 size_t const n = s.size();
99 return n && s[n - 1] == '*';
103 docstring const repl(docstring const & oldstr, char_type const c,
104 docstring const & macro, bool textmode = false)
110 for (i = 0, j = 0; i < oldstr.size(); ++i) {
111 if (c == oldstr[i]) {
112 newstr.append(oldstr, j, i - j);
113 newstr.append(macro);
115 if (macro.size() > 2 && j < oldstr.size())
116 newstr += (textmode && oldstr[j] == ' ' ? '\\' : ' ');
124 newstr.append(oldstr, j, i - j);
129 docstring escapeSpecialChars(docstring const & str, bool textmode)
131 docstring const backslash = textmode ? from_ascii("\\textbackslash")
132 : from_ascii("\\backslash");
133 docstring const caret = textmode ? from_ascii("\\textasciicircum")
134 : from_ascii("\\mathcircumflex");
135 docstring const tilde = textmode ? from_ascii("\\textasciitilde")
136 : from_ascii("\\sim");
138 return repl(repl(repl(repl(repl(repl(repl(repl(repl(repl(str,
139 '\\', backslash, textmode),
140 '^', caret, textmode),
141 '~', tilde, textmode),
142 '_', from_ascii("\\_")),
143 '$', from_ascii("\\$")),
144 '#', from_ascii("\\#")),
145 '&', from_ascii("\\&")),
146 '%', from_ascii("\\%")),
147 '{', from_ascii("\\{")),
148 '}', from_ascii("\\}"));
153 * Add the row \p cellrow to \p grid.
154 * \returns wether the row could be added. Adding a row can fail for
155 * environments like "equation" that have a fixed number of rows.
157 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
158 docstring const & vskip, bool allow_newpage_ = true)
161 if (cellrow == grid.nrows()) {
162 //lyxerr << "adding row " << cellrow << endl;
163 grid.addRow(cellrow - 1);
164 if (cellrow == grid.nrows()) {
165 // We can't add a row to this grid, so let's
166 // append the content of this cell to the previous
168 // This does not happen in well formed .lyx files,
169 // but LyX versions 1.3.x and older could create
170 // such files and tex2lyx can still do that.
172 lyxerr << "ignoring extra row";
174 lyxerr << " with extra space " << to_utf8(vskip);
176 lyxerr << " with no page break allowed";
177 lyxerr << '.' << endl;
181 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
182 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
188 * Add the column \p cellcol to \p grid.
189 * \returns wether the column could be added. Adding a column can fail for
190 * environments like "eqnarray" that have a fixed number of columns.
192 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
195 if (cellcol == grid.ncols()) {
196 //lyxerr << "adding column " << cellcol << endl;
197 grid.addCol(cellcol);
198 if (cellcol == grid.ncols()) {
199 // We can't add a column to this grid, so let's
200 // append the content of this cell to the previous
202 // This does not happen in well formed .lyx files,
203 // but LyX versions 1.3.x and older could create
204 // such files and tex2lyx can still do that.
206 lyxerr << "ignoring extra column." << endl;
215 * Check wether the last row is empty and remove it if yes.
216 * Otherwise the following code
224 * will result in a grid with 3 rows (+ the dummy row that is always present),
225 * because the last '\\' opens a new row.
227 void delEmptyLastRow(InsetMathGrid & grid)
229 InsetMathGrid::row_type const row = grid.nrows() - 1;
230 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
231 if (!grid.cell(grid.index(row, col)).empty())
234 // Copy the row information of the empty row (which would contain the
235 // last hline in the example above) to the dummy row and delete the
237 grid.rowinfo(row + 1) = grid.rowinfo(row);
242 // These are TeX's catcodes
244 catEscape, // 0 backslash
254 catSpace, // 10 space
255 catLetter, // 11 a-zA-Z
256 catOther, // 12 none of the above
259 catInvalid // 15 <delete>
262 CatCode theCatcode[128];
265 inline CatCode catcode(char_type c)
267 /* The only characters that are not catOther lie in the pure ASCII
268 * range. Therefore theCatcode has only 128 entries.
269 * TeX itself deals with 8bit characters, so if needed this table
270 * could be enlarged to 256 entries.
271 * Any larger value does not make sense, since the fact that we use
272 * unicode internally does not change Knuth's TeX engine.
273 * Apart from that a table for the full 21bit UCS4 range would waste
274 * too much memory. */
278 return theCatcode[c];
283 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
284 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
285 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
286 FLAG_END = 1 << 3, // next \\end ends the parsing process
287 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
288 FLAG_TEXTMODE = 1 << 5, // we are in a box
289 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
290 FLAG_LEAVE = 1 << 7, // leave the loop at the end
291 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
292 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
293 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
294 FLAG_OPTION = 1 << 11, // read [...] style option
295 FLAG_BRACED = 1 << 12 // read {...} style argument
300 // Helper class for parsing
306 Token() : cs_(), char_(0), cat_(catIgnore) {}
308 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
310 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
313 docstring const & cs() const { return cs_; }
315 CatCode cat() const { return cat_; }
317 char_type character() const { return char_; }
319 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
321 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
333 ostream & operator<<(ostream & os, Token const & t)
336 docstring const & cs = t.cs();
337 // FIXME: For some strange reason, the stream operator instanciate
338 // a new Token before outputting the contents of t.cs().
339 // Because of this the line
341 // below becomes recursive.
342 // In order to avoid that we return early:
345 os << '\\' << to_utf8(cs);
347 else if (t.cat() == catLetter)
350 os << '[' << t.character() << ',' << t.cat() << ']';
358 typedef InsetMath::mode_type mode_type;
360 typedef Parse::flags parse_mode;
363 Parser(Lexer & lex, parse_mode mode, Buffer * buf);
364 /// Only use this for reading from .lyx file format, for the reason
365 /// see Parser::tokenize(istream &).
366 Parser(istream & is, parse_mode mode, Buffer * buf);
368 Parser(docstring const & str, parse_mode mode, Buffer * buf);
371 bool parse(MathAtom & at);
373 bool parse(MathData & array, unsigned flags, mode_type mode);
375 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
378 MathData parse(unsigned flags, mode_type mode);
380 int lineno() const { return lineno_; }
386 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
387 /// get arg delimited by 'left' and 'right'
388 docstring getArg(char_type left, char_type right);
392 void error(string const & msg);
393 void error(docstring const & msg) { error(to_utf8(msg)); }
394 /// dump contents to screen
396 /// Only use this for reading from .lyx file format (see
397 /// implementation for reason)
398 void tokenize(istream & is);
400 void tokenize(docstring const & s);
402 void skipSpaceTokens(idocstream & is, char_type c);
404 void push_back(Token const & t);
408 Token const & prevToken() const;
410 Token const & nextToken() const;
412 Token const & getToken();
413 /// skips spaces if any
416 void lex(docstring const & s);
420 docstring parse_verbatim_item();
422 docstring parse_verbatim_option();
427 vector<Token> tokens_;
430 /// Stack of active environments
431 vector<docstring> environments_;
441 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
442 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
447 tokenize(lexer.getStream());
452 Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
453 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
461 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
462 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
470 void Parser::push_back(Token const & t)
472 tokens_.push_back(t);
476 void Parser::pop_back()
482 Token const & Parser::prevToken() const
484 static const Token dummy;
485 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
489 Token const & Parser::nextToken() const
491 static const Token dummy;
492 return good() ? tokens_[pos_] : dummy;
496 Token const & Parser::getToken()
498 static const Token dummy;
499 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
500 return good() ? tokens_[pos_++] : dummy;
504 void Parser::skipSpaces()
506 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
511 void Parser::putback()
517 bool Parser::good() const
519 return pos_ < tokens_.size();
523 char_type Parser::getChar()
526 error("The input stream is not well...");
530 return tokens_[pos_++].character();
534 docstring Parser::getArg(char_type left, char_type right)
539 char_type c = getChar();
544 while ((c = getChar()) != right && good())
551 void Parser::skipSpaceTokens(idocstream & is, char_type c)
553 // skip trailing spaces
554 while (catcode(c) == catSpace || catcode(c) == catNewline)
557 //lyxerr << "putting back: " << c << endl;
562 void Parser::tokenize(istream & is)
564 // eat everything up to the next \end_inset or end of stream
565 // and store it in s for further tokenization
570 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
571 s = s.substr(0, s.size() - 10);
575 // Remove the space after \end_inset
576 if (is.get(c) && c != ' ')
580 tokenize(from_utf8(s));
584 void Parser::tokenize(docstring const & buffer)
586 idocstringstream is(mode_ & Parse::VERBATIM
587 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
588 : buffer, ios::in | ios::binary);
592 //lyxerr << "reading c: " << c << endl;
594 switch (catcode(c)) {
598 if (catcode(c) == catNewline)
599 ; //push_back(Token("par"));
601 push_back(Token('\n', catNewline));
609 while (is.get(c) && catcode(c) != catNewline)
619 error("unexpected end of input");
622 if (catcode(c) == catLetter) {
624 while (is.get(c) && catcode(c) == catLetter)
626 skipSpaceTokens(is, c);
635 push_back(Token(c, catcode(c)));
637 skipSpaceTokens(is, c);
642 if (!(mode_ & Parse::QUIET))
643 lyxerr << "ignoring a char: " << int(c) << endl;
648 push_back(Token(c, catcode(c)));
658 void Parser::dump() const
660 lyxerr << "\nTokens: ";
661 for (unsigned i = 0; i < tokens_.size(); ++i) {
664 lyxerr << tokens_[i];
666 lyxerr << " pos: " << pos_ << endl;
670 void Parser::error(string const & msg)
673 if (!(mode_ & Parse::QUIET)) {
674 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
681 bool Parser::parse(MathAtom & at)
684 MathData ar(buffer_);
685 parse(ar, false, InsetMath::UNDECIDED_MODE);
686 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
687 if (!(mode_ & Parse::QUIET))
688 lyxerr << "unusual contents found: " << ar << endl;
689 at = MathAtom(new InsetMathPar(buffer_, ar));
690 //if (at->nargs() > 0)
691 // at.nucleus()->cell(0) = ar;
693 // lyxerr << "unusual contents found: " << ar << endl;
701 docstring Parser::parse_verbatim_option()
705 if (nextToken().character() == '[') {
706 Token t = getToken();
707 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
708 if (t.cat() == catBegin) {
710 res += '{' + parse_verbatim_item() + '}';
719 docstring Parser::parse_verbatim_item()
723 if (nextToken().cat() == catBegin) {
724 Token t = getToken();
725 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
726 if (t.cat() == catBegin) {
728 res += '{' + parse_verbatim_item() + '}';
738 MathData Parser::parse(unsigned flags, mode_type mode)
740 MathData ar(buffer_);
741 parse(ar, flags, mode);
746 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
748 InsetMathGrid grid(buffer_, 1, 1);
749 parse1(grid, flags, mode, false);
750 array = grid.cell(0);
755 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
758 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
762 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
763 const mode_type mode, const bool numbered)
766 InsetMathGrid::row_type cellrow = 0;
767 InsetMathGrid::col_type cellcol = 0;
768 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
769 Buffer * buf = buffer_;
771 if (grid.asHullInset())
772 grid.asHullInset()->numbered(cellrow, numbered);
775 //lyxerr << " flags: " << flags << endl;
776 //lyxerr << " mode: " << mode << endl;
777 //lyxerr << "grid: " << grid << endl;
780 Token const & t = getToken();
783 lyxerr << "t: " << t << " flags: " << flags << endl;
784 lyxerr << "mode: " << mode << endl;
789 if (flags & FLAG_ITEM) {
791 if (t.cat() == catBegin) {
792 // skip the brace and collect everything to the next matching
794 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
798 // handle only this single token, leave the loop if done
803 if (flags & FLAG_BRACED) {
804 if (t.cat() == catSpace)
807 if (t.cat() != catBegin) {
808 error("opening brace expected");
812 // skip the brace and collect everything to the next matching
814 flags = FLAG_BRACE_LAST;
818 if (flags & FLAG_OPTION) {
819 if (t.cat() == catOther && t.character() == '[') {
821 parse(ar, FLAG_BRACK_LAST, mode);
824 // no option found, put back token and we are done
833 if (t.cat() == catMath) {
834 if (mode != InsetMath::MATH_MODE) {
835 // we are inside some text mode thingy, so opening new math is allowed
836 Token const & n = getToken();
837 if (n.cat() == catMath) {
838 // TeX's $$...$$ syntax for displayed math
839 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
840 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
841 getToken(); // skip the second '$' token
843 // simple $...$ stuff
845 if (mode == InsetMath::UNDECIDED_MODE) {
846 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
847 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
849 // Don't create nested math hulls (bug #5392)
850 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
851 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
856 else if (flags & FLAG_SIMPLE) {
857 // this is the end of the formula
862 error("something strange in the parser");
867 else if (t.cat() == catLetter)
868 cell->push_back(MathAtom(new InsetMathChar(t.character())));
870 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
871 if (cell->empty() || cell->back()->getChar() != ' ')
872 cell->push_back(MathAtom(new InsetMathChar(t.character())));
875 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
876 if (cell->empty() || cell->back()->getChar() != ' ')
877 cell->push_back(MathAtom(new InsetMathChar(' ')));
880 else if (t.cat() == catParameter) {
881 Token const & n = getToken();
882 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
885 else if (t.cat() == catActive)
886 cell->push_back(MathAtom(new InsetMathChar(t.character())));
888 else if (t.cat() == catBegin) {
890 parse(ar, FLAG_BRACE_LAST, mode);
891 // do not create a BraceInset if they were written by LyX
892 // this helps to keep the annoyance of "a choose b" to a minimum
893 if (ar.size() == 1 && ar[0]->extraBraces())
896 cell->push_back(MathAtom(new InsetMathBrace(ar)));
899 else if (t.cat() == catEnd) {
900 if (flags & FLAG_BRACE_LAST)
902 error("found '}' unexpectedly");
903 //LASSERT(false, /**/);
904 //add(cell, '}', LM_TC_TEX);
907 else if (t.cat() == catAlign) {
908 //lyxerr << " column now " << (cellcol + 1)
909 // << " max: " << grid.ncols() << endl;
910 if (flags & FLAG_ALIGN)
912 if (addCol(grid, cellcol))
913 cell = &grid.cell(grid.index(cellrow, cellcol));
916 else if (t.cat() == catSuper || t.cat() == catSub) {
917 bool up = (t.cat() == catSuper);
918 // we need no new script inset if the last thing was a scriptinset,
919 // which has that script already not the same script already
921 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
922 else if (cell->back()->asScriptInset() &&
923 !cell->back()->asScriptInset()->has(up))
924 cell->back().nucleus()->asScriptInset()->ensure(up);
925 else if (cell->back()->asScriptInset())
926 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
928 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
929 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
930 // special handling of {}-bases
931 // Here we could remove the brace inset for things
932 // like {a'}^2 and add the braces back in
933 // InsetMathScript::write().
934 // We do not do it, since it is not possible to detect
935 // reliably whether the braces are needed because the
936 // nucleus contains more than one symbol, or whether
937 // they are needed for unknown commands like \xx{a}_0
938 // or \yy{a}{b}_0. This was done in revision 14819
939 // in an unreliable way. See this thread
940 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
942 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
949 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
950 //lyxerr << "finished reading option" << endl;
954 else if (t.cat() == catOther) {
955 char_type c = t.character();
956 if (isAsciiOrMathAlpha(c)
957 || mode_ & Parse::VERBATIM
958 || !(mode_ & Parse::USETEXT)
959 || mode == InsetMath::TEXT_MODE) {
960 cell->push_back(MathAtom(new InsetMathChar(c)));
962 MathAtom at = createInsetMath("text", buf);
963 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
964 while (nextToken().cat() == catOther
965 && !isAsciiOrMathAlpha(nextToken().character())) {
966 c = getToken().character();
967 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
973 else if (t.cat() == catComment) {
976 Token const & t = getToken();
977 if (t.cat() == catNewline)
981 cell->push_back(MathAtom(new InsetMathComment(buf, s)));
989 else if (t.cs() == "lyxlock") {
991 cell->back().nucleus()->lock(true);
994 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
996 if (t.cs() == "global")
1000 docstring name = getToken().cs();
1005 while (good() && nextToken().cat() != catBegin) {
1006 pars += getToken().cs();
1013 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1015 // is a version for display attached?
1018 if (nextToken().cat() == catBegin)
1019 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1021 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1022 name, nargs, 0, MacroTypeDef,
1023 vector<MathData>(), def, display)));
1026 else if (t.cs() == "newcommand" ||
1027 t.cs() == "renewcommand" ||
1028 t.cs() == "newlyxcommand") {
1030 if (getToken().cat() != catBegin) {
1031 error("'{' in \\newcommand expected (1) ");
1034 docstring name = getToken().cs();
1035 if (getToken().cat() != catEnd) {
1036 error("'}' in \\newcommand expected");
1041 docstring const arg = getArg('[', ']');
1044 nargs = convert<int>(arg);
1046 // optional argument given?
1049 vector<MathData> optionalValues;
1050 while (nextToken().character() == '[') {
1052 optionalValues.push_back(MathData());
1053 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1058 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1060 // is a version for display attached?
1063 if (nextToken().cat() == catBegin)
1064 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1066 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1067 name, nargs, optionals, MacroTypeNewcommand,
1068 optionalValues, def, display)));
1071 else if (t.cs() == "newcommandx" ||
1072 t.cs() == "renewcommandx") {
1073 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1076 if (nextToken().cat() == catBegin) {
1078 name = getToken().cs();
1079 if (getToken().cat() != catEnd) {
1080 error("'}' in \\newcommandx expected");
1084 name = getToken().cs();
1087 docstring const arg = getArg('[', ']');
1089 error("[num] in \\newcommandx expected");
1092 int nargs = convert<int>(arg);
1096 vector<MathData> optionalValues;
1097 if (nextToken().character() == '[') {
1101 // handle 'opt=value' options, separated by ','.
1103 while (nextToken().character() != ']' && good()) {
1104 if (nextToken().character() >= '1'
1105 && nextToken().character() <= '9') {
1106 // optional value -> get parameter number
1107 int n = getChar() - '0';
1109 error("Arity of \\newcommandx too low "
1110 "for given optional parameter.");
1115 if (getToken().character() != '=') {
1116 error("'=' and optional parameter value "
1117 "expected for \\newcommandx");
1122 int optNum = max(size_t(n), optionalValues.size());
1123 optionalValues.resize(optNum);
1124 optionalValues[n - 1].clear();
1125 while (nextToken().character() != ']'
1126 && nextToken().character() != ',') {
1128 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1129 optionalValues[n - 1].append(data);
1131 optionals = max(n, optionals);
1132 } else if (nextToken().cat() == catLetter) {
1133 // we in fact ignore every non-optional
1138 while (nextToken().cat() == catLetter)
1144 if (nextToken().character() == '=') {
1146 while (nextToken().character() != ']'
1147 && nextToken().character() != ',')
1148 parse(value, FLAG_ITEM,
1149 InsetMath::UNDECIDED_MODE);
1152 error("option for \\newcommandx expected");
1158 if (nextToken().character() == ',') {
1161 } else if (nextToken().character() != ']') {
1162 error("Expecting ',' or ']' in options "
1163 "of \\newcommandx");
1176 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1178 // is a version for display attached?
1181 if (nextToken().cat() == catBegin)
1182 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1184 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1185 name, nargs, optionals, MacroTypeNewcommandx,
1186 optionalValues, def, display)));
1189 else if (t.cs() == "(") {
1190 if (mode == InsetMath::MATH_MODE) {
1191 error("bad math environment");
1194 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1195 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1198 else if (t.cs() == "[") {
1199 if (mode != InsetMath::UNDECIDED_MODE) {
1200 error("bad math environment");
1203 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1204 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1207 else if (t.cs() == "protect")
1208 // ignore \\protect, will hopefully be re-added during output
1211 else if (t.cs() == "end") {
1212 if (flags & FLAG_END) {
1213 // eat environment name
1214 docstring const name = getArg('{', '}');
1215 if (environments_.empty())
1216 error("'found \\end{" + name +
1217 "}' without matching '\\begin{" +
1219 else if (name != environments_.back())
1220 error("'\\end{" + name +
1221 "}' does not match '\\begin{" +
1222 environments_.back() + "}'");
1224 environments_.pop_back();
1225 // Delete empty last row in matrix
1227 // If you abuse InsetMathGrid for
1228 // non-matrix like structures you
1229 // probably need to refine this test.
1230 // Right now we only have to test for
1231 // single line hull insets.
1232 if (grid.nrows() > 1)
1233 delEmptyLastRow(grid);
1237 error("found 'end' unexpectedly");
1240 else if (t.cs() == ")") {
1241 if (flags & FLAG_SIMPLE2)
1243 error("found '\\)' unexpectedly");
1246 else if (t.cs() == "]") {
1247 if (flags & FLAG_EQUATION)
1249 error("found '\\]' unexpectedly");
1252 else if (t.cs() == "\\") {
1253 if (flags & FLAG_ALIGN)
1256 if (nextToken().asInput() == "*") {
1258 added = addRow(grid, cellrow, docstring(), false);
1260 added = addRow(grid, cellrow, getArg('[', ']'));
1262 error("missing token after \\\\");
1265 if (grid.asHullInset())
1266 grid.asHullInset()->numbered(
1268 cell = &grid.cell(grid.index(cellrow,
1274 else if (t.cs() == "multicolumn") {
1275 // extract column count and insert dummy cells
1277 parse(count, FLAG_ITEM, mode);
1279 if (!extractNumber(count, cols)) {
1281 lyxerr << " can't extract number of cells from " << count << endl;
1283 // resize the table if necessary
1284 for (int i = 0; i < cols; ++i) {
1285 if (addCol(grid, cellcol)) {
1286 cell = &grid.cell(grid.index(
1288 // mark this as dummy
1289 grid.cellinfo(grid.index(
1290 cellrow, cellcol)).dummy_ = true;
1293 // the last cell is the real thing, not a dummy
1294 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1296 // read special alignment
1298 parse(align, FLAG_ITEM, mode);
1299 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1301 // parse the remaining contents into the "real" cell
1302 parse(*cell, FLAG_ITEM, mode);
1306 else if (t.cs() == "limits")
1309 else if (t.cs() == "nolimits")
1312 else if (t.cs() == "nonumber") {
1313 if (grid.asHullInset())
1314 grid.asHullInset()->numbered(cellrow, false);
1317 else if (t.cs() == "number") {
1318 if (grid.asHullInset())
1319 grid.asHullInset()->numbered(cellrow, true);
1322 else if (t.cs() == "hline") {
1323 grid.rowinfo(cellrow).lines_ ++;
1326 else if (t.cs() == "sqrt") {
1328 parse(ar, FLAG_OPTION, mode);
1330 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1331 cell->back().nucleus()->cell(0) = ar;
1332 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1334 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1335 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1339 else if (t.cs() == "unit") {
1340 // Allowed formats \unit[val]{unit}
1342 parse(ar, FLAG_OPTION, mode);
1344 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1345 cell->back().nucleus()->cell(0) = ar;
1346 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1348 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1349 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1353 else if (t.cs() == "unitfrac") {
1354 // Here allowed formats are \unitfrac[val]{num}{denom}
1356 parse(ar, FLAG_OPTION, mode);
1358 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1359 cell->back().nucleus()->cell(2) = ar;
1361 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1363 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1364 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1367 else if (t.cs() == "cfrac") {
1368 // allowed formats are \cfrac[pos]{num}{denom}
1369 docstring const arg = getArg('[', ']');
1370 //lyxerr << "got so far: '" << arg << "'" << endl;
1372 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1373 else if (arg == "r")
1374 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1375 else if (arg.empty() || arg == "c")
1376 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1378 error("found invalid optional argument");
1381 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1382 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1385 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1386 cell->push_back(createInsetMath(t.cs(), buf));
1387 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1388 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1391 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1392 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1393 cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1394 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1395 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1398 else if (t.cs() == "left") {
1400 Token const & tl = getToken();
1401 // \| and \Vert are equivalent, and InsetMathDelim
1403 // FIXME: fix this in InsetMathDelim itself!
1404 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1406 parse(ar, FLAG_RIGHT, mode);
1410 Token const & tr = getToken();
1411 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1412 cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1415 else if (t.cs() == "right") {
1416 if (flags & FLAG_RIGHT)
1418 //lyxerr << "got so far: '" << cell << "'" << endl;
1419 error("Unmatched right delimiter");
1423 else if (t.cs() == "begin") {
1424 docstring const name = getArg('{', '}');
1425 environments_.push_back(name);
1427 if (name == "array" || name == "subarray") {
1428 docstring const valign = parse_verbatim_option() + 'c';
1429 docstring const halign = parse_verbatim_item();
1430 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1431 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1432 parse2(cell->back(), FLAG_END, mode, false);
1435 else if (name == "tabular") {
1436 docstring const valign = parse_verbatim_option() + 'c';
1437 docstring const halign = parse_verbatim_item();
1438 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1439 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1440 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1443 else if (name == "split" || name == "cases") {
1444 cell->push_back(createInsetMath(name, buf));
1445 parse2(cell->back(), FLAG_END, mode, false);
1448 else if (name == "alignedat") {
1449 docstring const valign = parse_verbatim_option() + 'c';
1450 // ignore this for a while
1452 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1453 parse2(cell->back(), FLAG_END, mode, false);
1456 else if (name == "math") {
1457 if (mode == InsetMath::MATH_MODE) {
1458 error("bad math environment");
1461 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1462 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1465 else if (name == "equation" || name == "equation*"
1466 || name == "displaymath") {
1467 if (mode != InsetMath::UNDECIDED_MODE) {
1468 error("bad math environment");
1471 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1472 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1475 else if (name == "eqnarray" || name == "eqnarray*") {
1476 if (mode != InsetMath::UNDECIDED_MODE) {
1477 error("bad math environment");
1480 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1481 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1484 else if (name == "align" || name == "align*") {
1485 if (mode != InsetMath::UNDECIDED_MODE) {
1486 error("bad math environment");
1489 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1490 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1493 else if (name == "flalign" || name == "flalign*") {
1494 if (mode != InsetMath::UNDECIDED_MODE) {
1495 error("bad math environment");
1498 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1499 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1502 else if (name == "alignat" || name == "alignat*") {
1503 if (mode != InsetMath::UNDECIDED_MODE) {
1504 error("bad math environment");
1507 // ignore this for a while
1509 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlignAt)));
1510 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1513 else if (name == "xalignat" || name == "xalignat*") {
1514 if (mode != InsetMath::UNDECIDED_MODE) {
1515 error("bad math environment");
1518 // ignore this for a while
1520 cell->push_back(MathAtom(new InsetMathHull(buf, hullXAlignAt)));
1521 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1524 else if (name == "xxalignat") {
1525 if (mode != InsetMath::UNDECIDED_MODE) {
1526 error("bad math environment");
1529 // ignore this for a while
1531 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1532 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1535 else if (name == "multline" || name == "multline*") {
1536 if (mode != InsetMath::UNDECIDED_MODE) {
1537 error("bad math environment");
1540 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1541 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1544 else if (name == "gather" || name == "gather*") {
1545 if (mode != InsetMath::UNDECIDED_MODE) {
1546 error("bad math environment");
1549 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1550 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1553 else if (latexkeys const * l = in_word_set(name)) {
1554 if (l->inset == "matrix") {
1555 cell->push_back(createInsetMath(name, buf));
1556 parse2(cell->back(), FLAG_END, mode, false);
1557 } else if (l->inset == "split") {
1558 docstring const valign = parse_verbatim_option() + 'c';
1559 cell->push_back(MathAtom(
1560 new InsetMathSplit(buf, name, (char)valign[0])));
1561 parse2(cell->back(), FLAG_END, mode, false);
1564 if (!(mode_ & Parse::QUIET)) {
1566 lyxerr << "found math environment `"
1568 << "' in symbols file with unsupported inset `"
1569 << to_utf8(l->inset)
1572 // create generic environment inset
1573 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1574 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1580 if (!(mode_ & Parse::QUIET)) {
1582 lyxerr << "found unknown math environment '"
1583 << to_utf8(name) << "'" << endl;
1585 // create generic environment inset
1586 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1587 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1591 else if (t.cs() == "kern") {
1596 Token const & t = getToken();
1600 while (num_tokens--)
1605 if (isValidLength(to_utf8(s)))
1609 cell->push_back(MathAtom(new InsetMathKern));
1611 cell->push_back(MathAtom(new InsetMathKern(s)));
1614 else if (t.cs() == "label") {
1615 // FIXME: This is swallowed in inline formulas
1616 docstring label = parse_verbatim_item();
1619 if (grid.asHullInset()) {
1620 grid.asHullInset()->label(cellrow, label);
1622 cell->push_back(createInsetMath(t.cs(), buf));
1623 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1627 else if (t.cs() == "choose" || t.cs() == "over"
1628 || t.cs() == "atop" || t.cs() == "brace"
1629 || t.cs() == "brack") {
1630 MathAtom at = createInsetMath(t.cs(), buf);
1631 at.nucleus()->cell(0) = *cell;
1633 parse(at.nucleus()->cell(1), flags, mode);
1634 cell->push_back(at);
1638 else if (t.cs() == "color") {
1639 docstring const color = parse_verbatim_item();
1640 cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1641 parse(cell->back().nucleus()->cell(0), flags, mode);
1645 else if (t.cs() == "textcolor") {
1646 docstring const color = parse_verbatim_item();
1647 cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1648 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1651 else if (t.cs() == "normalcolor") {
1652 cell->push_back(createInsetMath(t.cs(), buf));
1653 parse(cell->back().nucleus()->cell(0), flags, mode);
1657 else if (t.cs() == "substack") {
1658 cell->push_back(createInsetMath(t.cs(), buf));
1659 parse2(cell->back(), FLAG_ITEM, mode, false);
1662 else if (t.cs() == "xymatrix") {
1663 odocstringstream os;
1664 while (good() && nextToken().cat() != catBegin)
1665 os << getToken().asInput();
1666 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1667 parse2(cell->back(), FLAG_ITEM, mode, false);
1670 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1671 cell->push_back(createInsetMath(t.cs(), buf));
1672 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1673 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1674 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1677 else if (t.cs() == "tag") {
1678 if (nextToken().character() == '*') {
1680 cell->push_back(createInsetMath(t.cs() + '*', buf));
1682 cell->push_back(createInsetMath(t.cs(), buf));
1683 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1686 else if (t.cs() == "hspace" && nextToken().character() != '*') {
1687 docstring const name = t.cs();
1688 docstring const arg = parse_verbatim_item();
1690 if (isValidLength(to_utf8(arg), &length))
1691 cell->push_back(MathAtom(new InsetMathSpace(length)));
1693 // Since the Length class cannot use length variables
1694 // we must not create an InsetMathSpace.
1695 cell->push_back(MathAtom(new MathMacro(buf, name)));
1697 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1703 else if (t.cs() == "infer") {
1705 parse(ar, FLAG_OPTION, mode);
1706 cell->push_back(createInsetMath(t.cs(), buf));
1707 parse2(cell->back(), FLAG_ITEM, mode, false);
1711 else if (1 && t.cs() == "ar") {
1712 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1713 // try to read target
1714 parse(p->cell(0), FLAG_OTPTION, mode);
1715 // try to read label
1716 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1717 p->up_ = nextToken().cat() == catSuper;
1719 parse(p->cell(1), FLAG_ITEM, mode);
1720 //lyxerr << "read label: " << p->cell(1) << endl;
1723 cell->push_back(MathAtom(p.release()));
1724 //lyxerr << "read cell: " << cell << endl;
1728 else if (t.cs() == "lyxmathsym") {
1730 if (getToken().cat() != catBegin) {
1731 error("'{' expected in \\" + t.cs());
1736 CatCode cat = nextToken().cat();
1737 while (good() && (count || cat != catEnd)) {
1738 if (cat == catBegin)
1740 else if (cat == catEnd)
1742 cmd += getToken().asInput();
1743 cat = nextToken().cat();
1745 if (getToken().cat() != catEnd) {
1746 error("'}' expected in \\" + t.cs());
1751 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1752 for (size_t i = 0; i < cmd.size(); ++i)
1753 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1755 char_type c = rem[0];
1756 cell->push_back(MathAtom(new InsetMathChar(c)));
1757 cmd = rem.substr(1);
1761 } while (cmd.size());
1764 else if (t.cs().size()) {
1765 bool const is_user_macro =
1766 buf && buf->getMacro(t.cs(), false);
1767 latexkeys const * l = in_word_set(t.cs());
1768 if (l && !is_user_macro) {
1769 if (l->inset == "big") {
1771 docstring const delim = getToken().asInput();
1772 if (InsetMathBig::isBigInsetDelim(delim))
1773 cell->push_back(MathAtom(
1774 new InsetMathBig(t.cs(), delim)));
1776 cell->push_back(createInsetMath(t.cs(), buf));
1781 else if (l->inset == "font") {
1782 cell->push_back(createInsetMath(t.cs(), buf));
1783 parse(cell->back().nucleus()->cell(0),
1784 FLAG_ITEM, asMode(mode, l->extra));
1787 else if (l->inset == "oldfont") {
1788 cell->push_back(createInsetMath(t.cs(), buf));
1789 parse(cell->back().nucleus()->cell(0),
1790 flags | FLAG_ALIGN, asMode(mode, l->extra));
1791 if (prevToken().cat() != catAlign &&
1792 prevToken().cs() != "\\")
1797 else if (l->inset == "style") {
1798 cell->push_back(createInsetMath(t.cs(), buf));
1799 parse(cell->back().nucleus()->cell(0),
1800 flags | FLAG_ALIGN, mode);
1801 if (prevToken().cat() != catAlign &&
1802 prevToken().cs() != "\\")
1808 MathAtom at = createInsetMath(t.cs(), buf);
1809 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1810 parse(at.nucleus()->cell(i),
1811 FLAG_ITEM, asMode(mode, l->extra));
1812 cell->push_back(at);
1817 bool is_unicode_symbol = false;
1818 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
1820 docstring cmd = prevToken().asInput();
1821 CatCode cat = nextToken().cat();
1822 if (cat == catBegin) {
1824 while (good() && (count || cat != catEnd)) {
1825 cat = nextToken().cat();
1826 cmd += getToken().asInput();
1828 if (cat == catBegin)
1830 else if (cat == catEnd)
1836 Encodings::fromLaTeXCommand(cmd, is_combining);
1838 if (cat == catLetter)
1840 cmd += getToken().asInput();
1842 if (cat == catLetter)
1844 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1847 is_unicode_symbol = true;
1848 cell->push_back(MathAtom(new InsetMathChar(c)));
1850 while (num_tokens--)
1854 if (!is_unicode_symbol) {
1855 MathAtom at = is_user_macro ?
1856 MathAtom(new MathMacro(buf, t.cs()))
1857 : createInsetMath(t.cs(), buf);
1858 InsetMath::mode_type m = mode;
1859 //if (m == InsetMath::UNDECIDED_MODE)
1860 //lyxerr << "default creation: m1: " << m << endl;
1861 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1862 m = at->currentMode();
1863 //lyxerr << "default creation: m2: " << m << endl;
1864 InsetMath::idx_type start = 0;
1865 // this fails on \bigg[...\bigg]
1867 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1870 // at.nucleus()->cell(0) = opt;
1872 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1873 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1876 cell->push_back(at);
1882 if (flags & FLAG_LEAVE) {
1883 flags &= ~FLAG_LEAVE;
1892 } // anonymous namespace
1895 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1897 return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1898 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1902 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1904 return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1905 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1909 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
1912 return Parser(str, f, buf).parse(t);
1916 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
1919 return Parser(lex, f, buf).parse(t);
1923 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
1926 return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
1927 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
1933 fill(theCatcode, theCatcode + 128, catOther);
1934 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1935 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1937 theCatcode[int('\\')] = catEscape;
1938 theCatcode[int('{')] = catBegin;
1939 theCatcode[int('}')] = catEnd;
1940 theCatcode[int('$')] = catMath;
1941 theCatcode[int('&')] = catAlign;
1942 theCatcode[int('\n')] = catNewline;
1943 theCatcode[int('#')] = catParameter;
1944 theCatcode[int('^')] = catSuper;
1945 theCatcode[int('_')] = catSub;
1946 theCatcode[int(0x7f)] = catIgnore;
1947 theCatcode[int(' ')] = catSpace;
1948 theCatcode[int('\t')] = catSpace;
1949 theCatcode[int('\r')] = catNewline;
1950 theCatcode[int('~')] = catActive;
1951 theCatcode[int('%')] = catComment;