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 "InsetMathString.h"
62 #include "InsetMathTabular.h"
63 #include "MathMacroTemplate.h"
64 #include "MathFactory.h"
65 #include "MathMacroArgument.h"
66 #include "MathSupport.h"
69 #include "BufferParams.h"
73 #include "support/debug.h"
74 #include "support/convert.h"
75 #include "support/docstream.h"
87 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
89 //lyxerr << "handling mode: '" << str << "'" << endl;
90 if (str == "mathmode")
91 return InsetMath::MATH_MODE;
92 if (str == "textmode" || str == "forcetext")
93 return InsetMath::TEXT_MODE;
98 bool stared(docstring const & s)
100 size_t const n = s.size();
101 return n && s[n - 1] == '*';
105 docstring const repl(docstring const & oldstr, char_type const c,
106 docstring const & macro, bool textmode = false)
112 for (i = 0, j = 0; i < oldstr.size(); ++i) {
113 if (c == oldstr[i]) {
114 newstr.append(oldstr, j, i - j);
115 newstr.append(macro);
117 if (macro.size() > 2 && j < oldstr.size())
118 newstr += (textmode && oldstr[j] == ' ' ? '\\' : ' ');
126 newstr.append(oldstr, j, i - j);
131 docstring escapeSpecialChars(docstring const & str, bool textmode)
133 docstring const backslash = textmode ? from_ascii("\\textbackslash")
134 : from_ascii("\\backslash");
135 docstring const caret = textmode ? from_ascii("\\textasciicircum")
136 : from_ascii("\\mathcircumflex");
137 docstring const tilde = textmode ? from_ascii("\\textasciitilde")
138 : from_ascii("\\sim");
140 return repl(repl(repl(repl(repl(repl(repl(repl(repl(repl(str,
141 '\\', backslash, textmode),
142 '^', caret, textmode),
143 '~', tilde, textmode),
144 '_', from_ascii("\\_")),
145 '$', from_ascii("\\$")),
146 '#', from_ascii("\\#")),
147 '&', from_ascii("\\&")),
148 '%', from_ascii("\\%")),
149 '{', from_ascii("\\{")),
150 '}', from_ascii("\\}"));
155 * Add the row \p cellrow to \p grid.
156 * \returns wether the row could be added. Adding a row can fail for
157 * environments like "equation" that have a fixed number of rows.
159 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
160 docstring const & vskip, bool allow_newpage_ = true)
163 if (cellrow == grid.nrows()) {
164 //lyxerr << "adding row " << cellrow << endl;
165 grid.addRow(cellrow - 1);
166 if (cellrow == grid.nrows()) {
167 // We can't add a row to this grid, so let's
168 // append the content of this cell to the previous
170 // This does not happen in well formed .lyx files,
171 // but LyX versions 1.3.x and older could create
172 // such files and tex2lyx can still do that.
174 lyxerr << "ignoring extra row";
176 lyxerr << " with extra space " << to_utf8(vskip);
178 lyxerr << " with no page break allowed";
179 lyxerr << '.' << endl;
183 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
184 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
190 * Add the column \p cellcol to \p grid.
191 * \returns wether the column could be added. Adding a column can fail for
192 * environments like "eqnarray" that have a fixed number of columns.
194 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
197 if (cellcol == grid.ncols()) {
198 //lyxerr << "adding column " << cellcol << endl;
199 grid.addCol(cellcol);
200 if (cellcol == grid.ncols()) {
201 // We can't add a column to this grid, so let's
202 // append the content of this cell to the previous
204 // This does not happen in well formed .lyx files,
205 // but LyX versions 1.3.x and older could create
206 // such files and tex2lyx can still do that.
208 lyxerr << "ignoring extra column." << endl;
217 * Check wether the last row is empty and remove it if yes.
218 * Otherwise the following code
226 * will result in a grid with 3 rows (+ the dummy row that is always present),
227 * because the last '\\' opens a new row.
229 void delEmptyLastRow(InsetMathGrid & grid)
231 InsetMathGrid::row_type const row = grid.nrows() - 1;
232 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
233 if (!grid.cell(grid.index(row, col)).empty())
236 // Copy the row information of the empty row (which would contain the
237 // last hline in the example above) to the dummy row and delete the
239 grid.rowinfo(row + 1) = grid.rowinfo(row);
244 // These are TeX's catcodes
246 catEscape, // 0 backslash
256 catSpace, // 10 space
257 catLetter, // 11 a-zA-Z
258 catOther, // 12 none of the above
261 catInvalid // 15 <delete>
264 CatCode theCatcode[128];
267 inline CatCode catcode(char_type c)
269 /* The only characters that are not catOther lie in the pure ASCII
270 * range. Therefore theCatcode has only 128 entries.
271 * TeX itself deals with 8bit characters, so if needed this table
272 * could be enlarged to 256 entries.
273 * Any larger value does not make sense, since the fact that we use
274 * unicode internally does not change Knuth's TeX engine.
275 * Apart from that a table for the full 21bit UCS4 range would waste
276 * too much memory. */
280 return theCatcode[c];
285 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
286 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
287 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
288 FLAG_END = 1 << 3, // next \\end ends the parsing process
289 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
290 FLAG_TEXTMODE = 1 << 5, // we are in a box
291 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
292 FLAG_LEAVE = 1 << 7, // leave the loop at the end
293 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
294 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
295 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
296 FLAG_OPTION = 1 << 11, // read [...] style option
297 FLAG_BRACED = 1 << 12 // read {...} style argument
302 // Helper class for parsing
308 Token() : cs_(), char_(0), cat_(catIgnore) {}
310 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
312 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
315 docstring const & cs() const { return cs_; }
317 CatCode cat() const { return cat_; }
319 char_type character() const { return char_; }
321 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
323 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
335 ostream & operator<<(ostream & os, Token const & t)
338 docstring const & cs = t.cs();
339 // FIXME: For some strange reason, the stream operator instanciate
340 // a new Token before outputting the contents of t.cs().
341 // Because of this the line
343 // below becomes recursive.
344 // In order to avoid that we return early:
347 os << '\\' << to_utf8(cs);
349 else if (t.cat() == catLetter)
352 os << '[' << t.character() << ',' << t.cat() << ']';
360 typedef InsetMath::mode_type mode_type;
362 typedef Parse::flags parse_mode;
365 Parser(Lexer & lex, parse_mode mode, Buffer * buf);
366 /// Only use this for reading from .lyx file format, for the reason
367 /// see Parser::tokenize(istream &).
368 Parser(istream & is, parse_mode mode, Buffer * buf);
370 Parser(docstring const & str, parse_mode mode, Buffer * buf);
373 bool parse(MathAtom & at);
375 bool parse(MathData & array, unsigned flags, mode_type mode);
377 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
380 MathData parse(unsigned flags, mode_type mode);
382 int lineno() const { return lineno_; }
388 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
389 /// get arg delimited by 'left' and 'right'
390 docstring getArg(char_type left, char_type right);
394 void error(string const & msg);
395 void error(docstring const & msg) { error(to_utf8(msg)); }
396 /// dump contents to screen
398 /// Only use this for reading from .lyx file format (see
399 /// implementation for reason)
400 void tokenize(istream & is);
402 void tokenize(docstring const & s);
404 void skipSpaceTokens(idocstream & is, char_type c);
406 void push_back(Token const & t);
410 Token const & prevToken() const;
412 Token const & nextToken() const;
414 Token const & getToken();
415 /// skips spaces if any
418 void lex(docstring const & s);
422 docstring parse_verbatim_item();
424 docstring parse_verbatim_option();
429 vector<Token> tokens_;
432 /// Stack of active environments
433 vector<docstring> environments_;
443 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
444 : 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)
459 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
460 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
466 void Parser::push_back(Token const & t)
468 tokens_.push_back(t);
472 void Parser::pop_back()
478 Token const & Parser::prevToken() const
480 static const Token dummy;
481 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
485 Token const & Parser::nextToken() const
487 static const Token dummy;
488 return good() ? tokens_[pos_] : dummy;
492 Token const & Parser::getToken()
494 static const Token dummy;
495 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
496 return good() ? tokens_[pos_++] : dummy;
500 void Parser::skipSpaces()
502 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
507 void Parser::putback()
513 bool Parser::good() const
515 return pos_ < tokens_.size();
519 char_type Parser::getChar()
522 error("The input stream is not well...");
526 return tokens_[pos_++].character();
530 docstring Parser::getArg(char_type left, char_type right)
535 char_type c = getChar();
540 while ((c = getChar()) != right && good())
547 void Parser::skipSpaceTokens(idocstream & is, char_type c)
549 // skip trailing spaces
550 while (catcode(c) == catSpace || catcode(c) == catNewline)
553 //lyxerr << "putting back: " << c << endl;
558 void Parser::tokenize(istream & is)
560 // eat everything up to the next \end_inset or end of stream
561 // and store it in s for further tokenization
566 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
567 s = s.substr(0, s.size() - 10);
571 // Remove the space after \end_inset
572 if (is.get(c) && c != ' ')
576 tokenize(from_utf8(s));
580 void Parser::tokenize(docstring const & buffer)
582 idocstringstream is(mode_ & Parse::VERBATIM
583 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
584 : buffer, ios::in | ios::binary);
588 //lyxerr << "reading c: " << c << endl;
590 switch (catcode(c)) {
594 if (catcode(c) == catNewline)
595 ; //push_back(Token("par"));
597 push_back(Token('\n', catNewline));
605 while (is.get(c) && catcode(c) != catNewline)
615 error("unexpected end of input");
618 if (catcode(c) == catLetter) {
620 while (is.get(c) && catcode(c) == catLetter)
622 skipSpaceTokens(is, c);
631 push_back(Token(c, catcode(c)));
633 skipSpaceTokens(is, c);
638 if (!(mode_ & Parse::QUIET))
639 lyxerr << "ignoring a char: " << int(c) << endl;
644 push_back(Token(c, catcode(c)));
654 void Parser::dump() const
656 lyxerr << "\nTokens: ";
657 for (unsigned i = 0; i < tokens_.size(); ++i) {
660 lyxerr << tokens_[i];
662 lyxerr << " pos: " << pos_ << endl;
666 void Parser::error(string const & msg)
669 if (!(mode_ & Parse::QUIET)) {
670 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
677 bool Parser::parse(MathAtom & at)
680 MathData ar(buffer_);
681 parse(ar, false, InsetMath::UNDECIDED_MODE);
682 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
683 if (!(mode_ & Parse::QUIET))
684 lyxerr << "unusual contents found: " << ar << endl;
685 at = MathAtom(new InsetMathPar(buffer_, ar));
686 //if (at->nargs() > 0)
687 // at.nucleus()->cell(0) = ar;
689 // lyxerr << "unusual contents found: " << ar << endl;
697 docstring Parser::parse_verbatim_option()
701 if (nextToken().character() == '[') {
702 Token t = getToken();
703 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
704 if (t.cat() == catBegin) {
706 res += '{' + parse_verbatim_item() + '}';
715 docstring Parser::parse_verbatim_item()
719 if (nextToken().cat() == catBegin) {
720 Token t = getToken();
721 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
722 if (t.cat() == catBegin) {
724 res += '{' + parse_verbatim_item() + '}';
734 MathData Parser::parse(unsigned flags, mode_type mode)
736 MathData ar(buffer_);
737 parse(ar, flags, mode);
742 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
744 InsetMathGrid grid(buffer_, 1, 1);
745 parse1(grid, flags, mode, false);
746 array = grid.cell(0);
751 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
754 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
758 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
759 const mode_type mode, const bool numbered)
762 InsetMathGrid::row_type cellrow = 0;
763 InsetMathGrid::col_type cellcol = 0;
764 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
765 Buffer * buf = buffer_;
767 if (grid.asHullInset())
768 grid.asHullInset()->numbered(cellrow, numbered);
771 //lyxerr << " flags: " << flags << endl;
772 //lyxerr << " mode: " << mode << endl;
773 //lyxerr << "grid: " << grid << endl;
776 Token const & t = getToken();
779 lyxerr << "t: " << t << " flags: " << flags << endl;
780 lyxerr << "mode: " << mode << endl;
785 if (flags & FLAG_ITEM) {
787 if (t.cat() == catBegin) {
788 // skip the brace and collect everything to the next matching
790 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
794 // handle only this single token, leave the loop if done
799 if (flags & FLAG_BRACED) {
800 if (t.cat() == catSpace)
803 if (t.cat() != catBegin) {
804 error("opening brace expected");
808 // skip the brace and collect everything to the next matching
810 flags = FLAG_BRACE_LAST;
814 if (flags & FLAG_OPTION) {
815 if (t.cat() == catOther && t.character() == '[') {
817 parse(ar, FLAG_BRACK_LAST, mode);
820 // no option found, put back token and we are done
829 if (t.cat() == catMath) {
830 if (mode != InsetMath::MATH_MODE) {
831 // we are inside some text mode thingy, so opening new math is allowed
832 Token const & n = getToken();
833 if (n.cat() == catMath) {
834 // TeX's $$...$$ syntax for displayed math
835 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
836 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
837 getToken(); // skip the second '$' token
839 // simple $...$ stuff
841 if (mode == InsetMath::UNDECIDED_MODE) {
842 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
843 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
845 // Don't create nested math hulls (bug #5392)
846 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
847 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
852 else if (flags & FLAG_SIMPLE) {
853 // this is the end of the formula
858 error("something strange in the parser");
863 else if (t.cat() == catLetter)
864 cell->push_back(MathAtom(new InsetMathChar(t.character())));
866 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
867 if (cell->empty() || cell->back()->getChar() != ' ')
868 cell->push_back(MathAtom(new InsetMathChar(t.character())));
871 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
872 if (cell->empty() || cell->back()->getChar() != ' ')
873 cell->push_back(MathAtom(new InsetMathChar(' ')));
876 else if (t.cat() == catParameter) {
877 Token const & n = getToken();
878 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
881 else if (t.cat() == catActive)
882 cell->push_back(MathAtom(new InsetMathChar(t.character())));
884 else if (t.cat() == catBegin) {
886 parse(ar, FLAG_BRACE_LAST, mode);
887 // do not create a BraceInset if they were written by LyX
888 // this helps to keep the annoyance of "a choose b" to a minimum
889 if (ar.size() == 1 && ar[0]->extraBraces())
892 cell->push_back(MathAtom(new InsetMathBrace(ar)));
895 else if (t.cat() == catEnd) {
896 if (flags & FLAG_BRACE_LAST)
898 error("found '}' unexpectedly");
899 //LASSERT(false, /**/);
900 //add(cell, '}', LM_TC_TEX);
903 else if (t.cat() == catAlign) {
904 //lyxerr << " column now " << (cellcol + 1)
905 // << " max: " << grid.ncols() << endl;
906 if (flags & FLAG_ALIGN)
908 if (addCol(grid, cellcol))
909 cell = &grid.cell(grid.index(cellrow, cellcol));
912 else if (t.cat() == catSuper || t.cat() == catSub) {
913 bool up = (t.cat() == catSuper);
914 // we need no new script inset if the last thing was a scriptinset,
915 // which has that script already not the same script already
917 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
918 else if (cell->back()->asScriptInset() &&
919 !cell->back()->asScriptInset()->has(up))
920 cell->back().nucleus()->asScriptInset()->ensure(up);
921 else if (cell->back()->asScriptInset())
922 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
924 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
925 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
926 // special handling of {}-bases
927 // Here we could remove the brace inset for things
928 // like {a'}^2 and add the braces back in
929 // InsetMathScript::write().
930 // We do not do it, since it is not possible to detect
931 // reliably whether the braces are needed because the
932 // nucleus contains more than one symbol, or whether
933 // they are needed for unknown commands like \xx{a}_0
934 // or \yy{a}{b}_0. This was done in revision 14819
935 // in an unreliable way. See this thread
936 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
938 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
945 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
946 //lyxerr << "finished reading option" << endl;
950 else if (t.cat() == catOther) {
951 char_type c = t.character();
952 if (isAsciiOrMathAlpha(c)
953 || mode_ & Parse::VERBATIM
954 || !(mode_ & Parse::USETEXT)
955 || mode == InsetMath::TEXT_MODE) {
956 cell->push_back(MathAtom(new InsetMathChar(c)));
958 MathAtom at = createInsetMath("text", buf);
959 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
960 while (nextToken().cat() == catOther
961 && !isAsciiOrMathAlpha(nextToken().character())) {
962 c = getToken().character();
963 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
969 else if (t.cat() == catComment) {
972 Token const & t = getToken();
973 if (t.cat() == catNewline)
977 cell->push_back(MathAtom(new InsetMathComment(buf, s)));
985 else if (t.cs() == "lyxlock") {
987 cell->back().nucleus()->lock(true);
990 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
992 if (t.cs() == "global")
996 docstring name = getToken().cs();
1001 while (good() && nextToken().cat() != catBegin) {
1002 pars += getToken().cs();
1009 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1011 // is a version for display attached?
1014 if (nextToken().cat() == catBegin)
1015 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1017 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1018 name, nargs, 0, MacroTypeDef,
1019 vector<MathData>(), def, display)));
1021 if (buf && (mode_ & Parse::TRACKMACRO))
1022 buf->usermacros.insert(name);
1025 else if (t.cs() == "newcommand" ||
1026 t.cs() == "renewcommand" ||
1027 t.cs() == "newlyxcommand") {
1029 if (getToken().cat() != catBegin) {
1030 error("'{' in \\newcommand expected (1) ");
1033 docstring name = getToken().cs();
1034 if (getToken().cat() != catEnd) {
1035 error("'}' in \\newcommand expected");
1040 docstring const arg = getArg('[', ']');
1043 nargs = convert<int>(arg);
1045 // optional argument given?
1048 vector<MathData> optionalValues;
1049 while (nextToken().character() == '[') {
1051 optionalValues.push_back(MathData());
1052 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1057 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1059 // is a version for display attached?
1062 if (nextToken().cat() == catBegin)
1063 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1065 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1066 name, nargs, optionals, MacroTypeNewcommand,
1067 optionalValues, def, display)));
1069 if (buf && (mode_ & Parse::TRACKMACRO))
1070 buf->usermacros.insert(name);
1073 else if (t.cs() == "newcommandx" ||
1074 t.cs() == "renewcommandx") {
1075 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1078 if (nextToken().cat() == catBegin) {
1080 name = getToken().cs();
1081 if (getToken().cat() != catEnd) {
1082 error("'}' in \\newcommandx expected");
1086 name = getToken().cs();
1089 docstring const arg = getArg('[', ']');
1091 error("[num] in \\newcommandx expected");
1094 int nargs = convert<int>(arg);
1098 vector<MathData> optionalValues;
1099 if (nextToken().character() == '[') {
1103 // handle 'opt=value' options, separated by ','.
1105 while (nextToken().character() != ']' && good()) {
1106 if (nextToken().character() >= '1'
1107 && nextToken().character() <= '9') {
1108 // optional value -> get parameter number
1109 int n = getChar() - '0';
1111 error("Arity of \\newcommandx too low "
1112 "for given optional parameter.");
1117 if (getToken().character() != '=') {
1118 error("'=' and optional parameter value "
1119 "expected for \\newcommandx");
1124 int optNum = max(size_t(n), optionalValues.size());
1125 optionalValues.resize(optNum);
1126 optionalValues[n - 1].clear();
1127 while (nextToken().character() != ']'
1128 && nextToken().character() != ',') {
1130 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1131 optionalValues[n - 1].append(data);
1133 optionals = max(n, optionals);
1134 } else if (nextToken().cat() == catLetter) {
1135 // we in fact ignore every non-optional
1140 while (nextToken().cat() == catLetter)
1146 if (nextToken().character() == '=') {
1148 while (nextToken().character() != ']'
1149 && nextToken().character() != ',')
1150 parse(value, FLAG_ITEM,
1151 InsetMath::UNDECIDED_MODE);
1154 error("option for \\newcommandx expected");
1160 if (nextToken().character() == ',') {
1163 } else if (nextToken().character() != ']') {
1164 error("Expecting ',' or ']' in options "
1165 "of \\newcommandx");
1178 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1180 // is a version for display attached?
1183 if (nextToken().cat() == catBegin)
1184 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1186 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1187 name, nargs, optionals, MacroTypeNewcommandx,
1188 optionalValues, def, display)));
1190 if (buf && (mode_ & Parse::TRACKMACRO))
1191 buf->usermacros.insert(name);
1194 else if (t.cs() == "(") {
1195 if (mode == InsetMath::MATH_MODE) {
1196 error("bad math environment");
1199 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1200 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1203 else if (t.cs() == "[") {
1204 if (mode != InsetMath::UNDECIDED_MODE) {
1205 error("bad math environment");
1208 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1209 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1212 else if (t.cs() == "protect")
1213 // ignore \\protect, will hopefully be re-added during output
1216 else if (t.cs() == "end") {
1217 if (flags & FLAG_END) {
1218 // eat environment name
1219 docstring const name = getArg('{', '}');
1220 if (environments_.empty())
1221 error("'found \\end{" + name +
1222 "}' without matching '\\begin{" +
1224 else if (name != environments_.back())
1225 error("'\\end{" + name +
1226 "}' does not match '\\begin{" +
1227 environments_.back() + "}'");
1229 environments_.pop_back();
1230 // Delete empty last row in matrix
1232 // If you abuse InsetMathGrid for
1233 // non-matrix like structures you
1234 // probably need to refine this test.
1235 // Right now we only have to test for
1236 // single line hull insets.
1237 if (grid.nrows() > 1)
1238 delEmptyLastRow(grid);
1242 error("found 'end' unexpectedly");
1245 else if (t.cs() == ")") {
1246 if (flags & FLAG_SIMPLE2)
1248 error("found '\\)' unexpectedly");
1251 else if (t.cs() == "]") {
1252 if (flags & FLAG_EQUATION)
1254 error("found '\\]' unexpectedly");
1257 else if (t.cs() == "\\") {
1258 if (flags & FLAG_ALIGN)
1261 if (nextToken().asInput() == "*") {
1263 added = addRow(grid, cellrow, docstring(), false);
1265 added = addRow(grid, cellrow, getArg('[', ']'));
1267 error("missing token after \\\\");
1270 if (grid.asHullInset())
1271 grid.asHullInset()->numbered(
1273 cell = &grid.cell(grid.index(cellrow,
1279 else if (t.cs() == "multicolumn") {
1280 // extract column count and insert dummy cells
1282 parse(count, FLAG_ITEM, mode);
1284 if (!extractNumber(count, cols)) {
1286 lyxerr << " can't extract number of cells from " << count << endl;
1288 // resize the table if necessary
1289 for (int i = 0; i < cols; ++i) {
1290 if (addCol(grid, cellcol)) {
1291 cell = &grid.cell(grid.index(
1293 // mark this as dummy
1294 grid.cellinfo(grid.index(
1295 cellrow, cellcol)).dummy_ = true;
1298 // the last cell is the real thing, not a dummy
1299 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1301 // read special alignment
1303 parse(align, FLAG_ITEM, mode);
1304 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1306 // parse the remaining contents into the "real" cell
1307 parse(*cell, FLAG_ITEM, mode);
1311 else if (t.cs() == "limits")
1314 else if (t.cs() == "nolimits")
1317 else if (t.cs() == "nonumber") {
1318 if (grid.asHullInset())
1319 grid.asHullInset()->numbered(cellrow, false);
1322 else if (t.cs() == "number") {
1323 if (grid.asHullInset())
1324 grid.asHullInset()->numbered(cellrow, true);
1327 else if (t.cs() == "hline") {
1328 grid.rowinfo(cellrow).lines_ ++;
1331 else if (t.cs() == "sqrt") {
1333 parse(ar, FLAG_OPTION, mode);
1335 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1336 cell->back().nucleus()->cell(0) = ar;
1337 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1339 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1340 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1344 else if (t.cs() == "unit") {
1345 // Allowed formats \unit[val]{unit}
1347 parse(ar, FLAG_OPTION, mode);
1349 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1350 cell->back().nucleus()->cell(0) = ar;
1351 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1353 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1354 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1358 else if (t.cs() == "unitfrac") {
1359 // Here allowed formats are \unitfrac[val]{num}{denom}
1361 parse(ar, FLAG_OPTION, mode);
1363 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1364 cell->back().nucleus()->cell(2) = ar;
1366 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1368 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1369 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1372 else if (t.cs() == "cfrac") {
1373 // allowed formats are \cfrac[pos]{num}{denom}
1374 docstring const arg = getArg('[', ']');
1375 //lyxerr << "got so far: '" << arg << "'" << endl;
1377 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1378 else if (arg == "r")
1379 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1380 else if (arg.empty() || arg == "c")
1381 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1383 error("found invalid optional argument");
1386 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1387 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1390 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1391 cell->push_back(createInsetMath(t.cs(), buf));
1392 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1393 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1396 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1397 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1398 cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1399 docstring const opt = parse_verbatim_option();
1400 docstring const ref = parse_verbatim_item();
1402 cell->back().nucleus()->cell(1).push_back(
1403 MathAtom(new InsetMathString(opt)));
1405 cell->back().nucleus()->cell(0).push_back(
1406 MathAtom(new InsetMathString(ref)));
1409 else if (t.cs() == "left") {
1411 Token const & tl = getToken();
1412 // \| and \Vert are equivalent, and InsetMathDelim
1414 // FIXME: fix this in InsetMathDelim itself!
1415 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1417 parse(ar, FLAG_RIGHT, mode);
1421 Token const & tr = getToken();
1422 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1423 cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1426 else if (t.cs() == "right") {
1427 if (flags & FLAG_RIGHT)
1429 //lyxerr << "got so far: '" << cell << "'" << endl;
1430 error("Unmatched right delimiter");
1434 else if (t.cs() == "begin") {
1435 docstring const name = getArg('{', '}');
1436 environments_.push_back(name);
1438 if (name == "array" || name == "subarray") {
1439 docstring const valign = parse_verbatim_option() + 'c';
1440 docstring const halign = parse_verbatim_item();
1441 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1442 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1443 parse2(cell->back(), FLAG_END, mode, false);
1446 else if (name == "tabular") {
1447 docstring const valign = parse_verbatim_option() + 'c';
1448 docstring const halign = parse_verbatim_item();
1449 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1450 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1451 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1454 else if (name == "split" || name == "cases") {
1455 cell->push_back(createInsetMath(name, buf));
1456 parse2(cell->back(), FLAG_END, mode, false);
1459 else if (name == "alignedat") {
1460 docstring const valign = parse_verbatim_option() + 'c';
1461 // ignore this for a while
1463 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1464 parse2(cell->back(), FLAG_END, mode, false);
1467 else if (name == "math") {
1468 if (mode == InsetMath::MATH_MODE) {
1469 error("bad math environment");
1472 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1473 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1476 else if (name == "equation" || name == "equation*"
1477 || name == "displaymath") {
1478 if (mode != InsetMath::UNDECIDED_MODE) {
1479 error("bad math environment");
1482 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1483 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1486 else if (name == "eqnarray" || name == "eqnarray*") {
1487 if (mode != InsetMath::UNDECIDED_MODE) {
1488 error("bad math environment");
1491 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1492 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1495 else if (name == "align" || name == "align*") {
1496 if (mode != InsetMath::UNDECIDED_MODE) {
1497 error("bad math environment");
1500 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1501 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1504 else if (name == "flalign" || name == "flalign*") {
1505 if (mode != InsetMath::UNDECIDED_MODE) {
1506 error("bad math environment");
1509 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1510 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1513 else if (name == "alignat" || name == "alignat*") {
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, hullAlignAt)));
1521 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1524 else if (name == "xalignat" || name == "xalignat*") {
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, hullXAlignAt)));
1532 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1535 else if (name == "xxalignat") {
1536 if (mode != InsetMath::UNDECIDED_MODE) {
1537 error("bad math environment");
1540 // ignore this for a while
1542 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1543 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1546 else if (name == "multline" || name == "multline*") {
1547 if (mode != InsetMath::UNDECIDED_MODE) {
1548 error("bad math environment");
1551 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1552 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1555 else if (name == "gather" || name == "gather*") {
1556 if (mode != InsetMath::UNDECIDED_MODE) {
1557 error("bad math environment");
1560 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1561 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1564 else if (latexkeys const * l = in_word_set(name)) {
1565 if (l->inset == "matrix") {
1566 cell->push_back(createInsetMath(name, buf));
1567 parse2(cell->back(), FLAG_END, mode, false);
1568 } else if (l->inset == "split") {
1569 docstring const valign = parse_verbatim_option() + 'c';
1570 cell->push_back(MathAtom(
1571 new InsetMathSplit(buf, name, (char)valign[0])));
1572 parse2(cell->back(), FLAG_END, mode, false);
1575 if (!(mode_ & Parse::QUIET)) {
1577 lyxerr << "found math environment `"
1579 << "' in symbols file with unsupported inset `"
1580 << to_utf8(l->inset)
1583 // create generic environment inset
1584 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1585 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1591 if (!(mode_ & Parse::QUIET)) {
1593 lyxerr << "found unknown math environment '"
1594 << to_utf8(name) << "'" << endl;
1596 // create generic environment inset
1597 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1598 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1602 else if (t.cs() == "kern") {
1607 Token const & t = getToken();
1611 while (num_tokens--)
1616 if (isValidLength(to_utf8(s)))
1620 cell->push_back(MathAtom(new InsetMathKern));
1622 cell->push_back(MathAtom(new InsetMathKern(s)));
1625 else if (t.cs() == "label") {
1626 // FIXME: This is swallowed in inline formulas
1627 docstring label = parse_verbatim_item();
1630 if (grid.asHullInset()) {
1631 grid.asHullInset()->label(cellrow, label);
1633 cell->push_back(createInsetMath(t.cs(), buf));
1634 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1638 else if (t.cs() == "choose" || t.cs() == "over"
1639 || t.cs() == "atop" || t.cs() == "brace"
1640 || t.cs() == "brack") {
1641 MathAtom at = createInsetMath(t.cs(), buf);
1642 at.nucleus()->cell(0) = *cell;
1644 parse(at.nucleus()->cell(1), flags, mode);
1645 cell->push_back(at);
1649 else if (t.cs() == "color") {
1650 docstring const color = parse_verbatim_item();
1651 cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1652 parse(cell->back().nucleus()->cell(0), flags, mode);
1656 else if (t.cs() == "textcolor") {
1657 docstring const color = parse_verbatim_item();
1658 cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1659 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1662 else if (t.cs() == "normalcolor") {
1663 cell->push_back(createInsetMath(t.cs(), buf));
1664 parse(cell->back().nucleus()->cell(0), flags, mode);
1668 else if (t.cs() == "substack") {
1669 cell->push_back(createInsetMath(t.cs(), buf));
1670 parse2(cell->back(), FLAG_ITEM, mode, false);
1673 else if (t.cs() == "xymatrix") {
1674 odocstringstream os;
1675 while (good() && nextToken().cat() != catBegin)
1676 os << getToken().asInput();
1677 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1678 parse2(cell->back(), FLAG_ITEM, mode, false);
1681 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1682 cell->push_back(createInsetMath(t.cs(), buf));
1683 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1684 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1685 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1688 else if (t.cs() == "tag") {
1689 if (nextToken().character() == '*') {
1691 cell->push_back(createInsetMath(t.cs() + '*', buf));
1693 cell->push_back(createInsetMath(t.cs(), buf));
1694 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1697 else if (t.cs() == "hspace" && nextToken().character() != '*') {
1698 docstring const name = t.cs();
1699 docstring const arg = parse_verbatim_item();
1701 if (isValidLength(to_utf8(arg), &length))
1702 cell->push_back(MathAtom(new InsetMathSpace(length)));
1704 // Since the Length class cannot use length variables
1705 // we must not create an InsetMathSpace.
1706 cell->push_back(MathAtom(new MathMacro(buf, name)));
1708 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1714 else if (t.cs() == "infer") {
1716 parse(ar, FLAG_OPTION, mode);
1717 cell->push_back(createInsetMath(t.cs(), buf));
1718 parse2(cell->back(), FLAG_ITEM, mode, false);
1722 else if (1 && t.cs() == "ar") {
1723 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1724 // try to read target
1725 parse(p->cell(0), FLAG_OTPTION, mode);
1726 // try to read label
1727 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1728 p->up_ = nextToken().cat() == catSuper;
1730 parse(p->cell(1), FLAG_ITEM, mode);
1731 //lyxerr << "read label: " << p->cell(1) << endl;
1734 cell->push_back(MathAtom(p.release()));
1735 //lyxerr << "read cell: " << cell << endl;
1739 else if (t.cs() == "lyxmathsym") {
1741 if (getToken().cat() != catBegin) {
1742 error("'{' expected in \\" + t.cs());
1747 CatCode cat = nextToken().cat();
1748 while (good() && (count || cat != catEnd)) {
1749 if (cat == catBegin)
1751 else if (cat == catEnd)
1753 cmd += getToken().asInput();
1754 cat = nextToken().cat();
1756 if (getToken().cat() != catEnd) {
1757 error("'}' expected in \\" + t.cs());
1762 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1763 for (size_t i = 0; i < cmd.size(); ++i)
1764 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1766 char_type c = rem[0];
1767 cell->push_back(MathAtom(new InsetMathChar(c)));
1768 cmd = rem.substr(1);
1772 } while (cmd.size());
1775 else if (t.cs().size()) {
1776 bool const no_mhchem =
1777 (t.cs() == "ce" || t.cs() == "cf") && buf
1778 && buf->params().use_mhchem == BufferParams::package_off;
1779 bool const is_user_macro = no_mhchem ||
1780 (buf && (mode_ & Parse::TRACKMACRO
1781 ? buf->usermacros.count(t.cs()) != 0
1782 : buf->getMacro(t.cs(), false) != 0));
1783 latexkeys const * l = in_word_set(t.cs());
1784 if (l && !is_user_macro) {
1785 if (l->inset == "big") {
1787 docstring const delim = getToken().asInput();
1788 if (InsetMathBig::isBigInsetDelim(delim))
1789 cell->push_back(MathAtom(
1790 new InsetMathBig(t.cs(), delim)));
1792 cell->push_back(createInsetMath(t.cs(), buf));
1797 else if (l->inset == "font") {
1798 cell->push_back(createInsetMath(t.cs(), buf));
1799 parse(cell->back().nucleus()->cell(0),
1800 FLAG_ITEM, asMode(mode, l->extra));
1803 else if (l->inset == "oldfont") {
1804 cell->push_back(createInsetMath(t.cs(), buf));
1805 parse(cell->back().nucleus()->cell(0),
1806 flags | FLAG_ALIGN, asMode(mode, l->extra));
1807 if (prevToken().cat() != catAlign &&
1808 prevToken().cs() != "\\")
1813 else if (l->inset == "style") {
1814 cell->push_back(createInsetMath(t.cs(), buf));
1815 parse(cell->back().nucleus()->cell(0),
1816 flags | FLAG_ALIGN, mode);
1817 if (prevToken().cat() != catAlign &&
1818 prevToken().cs() != "\\")
1824 MathAtom at = createInsetMath(t.cs(), buf);
1825 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1826 parse(at.nucleus()->cell(i),
1827 FLAG_ITEM, asMode(mode, l->extra));
1828 cell->push_back(at);
1833 bool is_unicode_symbol = false;
1834 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
1836 docstring cmd = prevToken().asInput();
1837 CatCode cat = nextToken().cat();
1838 if (cat == catBegin) {
1840 while (good() && (count || cat != catEnd)) {
1841 cat = nextToken().cat();
1842 cmd += getToken().asInput();
1844 if (cat == catBegin)
1846 else if (cat == catEnd)
1852 Encodings::fromLaTeXCommand(cmd, is_combining);
1854 if (cat == catLetter)
1856 cmd += getToken().asInput();
1858 if (cat == catLetter)
1860 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1863 is_unicode_symbol = true;
1864 cell->push_back(MathAtom(new InsetMathChar(c)));
1866 while (num_tokens--)
1870 if (!is_unicode_symbol) {
1871 MathAtom at = is_user_macro ?
1872 MathAtom(new MathMacro(buf, t.cs()))
1873 : createInsetMath(t.cs(), buf);
1874 InsetMath::mode_type m = mode;
1875 //if (m == InsetMath::UNDECIDED_MODE)
1876 //lyxerr << "default creation: m1: " << m << endl;
1877 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1878 m = at->currentMode();
1879 //lyxerr << "default creation: m2: " << m << endl;
1880 InsetMath::idx_type start = 0;
1881 // this fails on \bigg[...\bigg]
1883 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1886 // at.nucleus()->cell(0) = opt;
1888 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1889 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1892 cell->push_back(at);
1898 if (flags & FLAG_LEAVE) {
1899 flags &= ~FLAG_LEAVE;
1908 } // anonymous namespace
1911 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1913 return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1914 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1918 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1920 return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1921 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1925 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
1928 return Parser(str, f, buf).parse(t);
1932 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
1935 return Parser(lex, f, buf).parse(t);
1939 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
1942 return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
1943 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
1949 fill(theCatcode, theCatcode + 128, catOther);
1950 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1951 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1953 theCatcode[int('\\')] = catEscape;
1954 theCatcode[int('{')] = catBegin;
1955 theCatcode[int('}')] = catEnd;
1956 theCatcode[int('$')] = catMath;
1957 theCatcode[int('&')] = catAlign;
1958 theCatcode[int('\n')] = catNewline;
1959 theCatcode[int('#')] = catParameter;
1960 theCatcode[int('^')] = catSuper;
1961 theCatcode[int('_')] = catSub;
1962 theCatcode[int(0x7f)] = catIgnore;
1963 theCatcode[int(' ')] = catSpace;
1964 theCatcode[int('\t')] = catSpace;
1965 theCatcode[int('\r')] = catNewline;
1966 theCatcode[int('~')] = catActive;
1967 theCatcode[int('%')] = catComment;