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 whether 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.
228 * Note that this is only needed for inner-hull grid types, such as array
229 * or aligned, but not for outer-hull grid types, such as eqnarray or align.
231 void delEmptyLastRow(InsetMathGrid & grid)
233 InsetMathGrid::row_type const row = grid.nrows() - 1;
234 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
235 if (!grid.cell(grid.index(row, col)).empty())
238 // Copy the row information of the empty row (which would contain the
239 // last hline in the example above) to the dummy row and delete the
241 grid.rowinfo(row + 1) = grid.rowinfo(row);
247 * Tell whether the environment name corresponds to an inner-hull grid type.
249 bool innerHull(docstring const & name)
251 return name == "array" || name == "cases" || name == "aligned"
252 || name == "alignedat" || name == "gathered" || name == "split"
253 || name == "tabular";
257 // These are TeX's catcodes
259 catEscape, // 0 backslash
269 catSpace, // 10 space
270 catLetter, // 11 a-zA-Z
271 catOther, // 12 none of the above
274 catInvalid // 15 <delete>
277 CatCode theCatcode[128];
280 inline CatCode catcode(char_type c)
282 /* The only characters that are not catOther lie in the pure ASCII
283 * range. Therefore theCatcode has only 128 entries.
284 * TeX itself deals with 8bit characters, so if needed this table
285 * could be enlarged to 256 entries.
286 * Any larger value does not make sense, since the fact that we use
287 * unicode internally does not change Knuth's TeX engine.
288 * Apart from that a table for the full 21bit UCS4 range would waste
289 * too much memory. */
293 return theCatcode[c];
298 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
299 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
300 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
301 FLAG_END = 1 << 3, // next \\end ends the parsing process
302 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
303 FLAG_TEXTMODE = 1 << 5, // we are in a box
304 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
305 FLAG_LEAVE = 1 << 7, // leave the loop at the end
306 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
307 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
308 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
309 FLAG_OPTION = 1 << 11, // read [...] style option
310 FLAG_BRACED = 1 << 12 // read {...} style argument
315 // Helper class for parsing
321 Token() : cs_(), char_(0), cat_(catIgnore) {}
323 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
325 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
328 docstring const & cs() const { return cs_; }
330 CatCode cat() const { return cat_; }
332 char_type character() const { return char_; }
334 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
336 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
348 ostream & operator<<(ostream & os, Token const & t)
351 docstring const & cs = t.cs();
352 // FIXME: For some strange reason, the stream operator instanciate
353 // a new Token before outputting the contents of t.cs().
354 // Because of this the line
356 // below becomes recursive.
357 // In order to avoid that we return early:
360 os << '\\' << to_utf8(cs);
362 else if (t.cat() == catLetter)
365 os << '[' << t.character() << ',' << t.cat() << ']';
373 typedef InsetMath::mode_type mode_type;
375 typedef Parse::flags parse_mode;
378 Parser(Lexer & lex, parse_mode mode, Buffer * buf);
379 /// Only use this for reading from .lyx file format, for the reason
380 /// see Parser::tokenize(istream &).
381 Parser(istream & is, parse_mode mode, Buffer * buf);
383 Parser(docstring const & str, parse_mode mode, Buffer * buf);
386 bool parse(MathAtom & at);
388 bool parse(MathData & array, unsigned flags, mode_type mode);
390 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
393 MathData parse(unsigned flags, mode_type mode);
395 int lineno() const { return lineno_; }
401 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
402 /// get arg delimited by 'left' and 'right'
403 docstring getArg(char_type left, char_type right);
407 void error(string const & msg);
408 void error(docstring const & msg) { error(to_utf8(msg)); }
409 /// dump contents to screen
411 /// Only use this for reading from .lyx file format (see
412 /// implementation for reason)
413 void tokenize(istream & is);
415 void tokenize(docstring const & s);
417 void skipSpaceTokens(idocstream & is, char_type c);
419 void push_back(Token const & t);
423 Token const & prevToken() const;
425 Token const & nextToken() const;
427 Token const & getToken();
428 /// skips spaces if any
431 void lex(docstring const & s);
435 docstring parse_verbatim_item();
437 docstring parse_verbatim_option();
442 vector<Token> tokens_;
445 /// Stack of active environments
446 vector<docstring> environments_;
456 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
457 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
460 tokenize(lexer.getStream());
465 Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
466 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
472 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
473 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
479 void Parser::push_back(Token const & t)
481 tokens_.push_back(t);
485 void Parser::pop_back()
491 Token const & Parser::prevToken() const
493 static const Token dummy;
494 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
498 Token const & Parser::nextToken() const
500 static const Token dummy;
501 return good() ? tokens_[pos_] : dummy;
505 Token const & Parser::getToken()
507 static const Token dummy;
508 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
509 return good() ? tokens_[pos_++] : dummy;
513 void Parser::skipSpaces()
515 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
520 void Parser::putback()
526 bool Parser::good() const
528 return pos_ < tokens_.size();
532 char_type Parser::getChar()
535 error("The input stream is not well...");
539 return tokens_[pos_++].character();
543 docstring Parser::getArg(char_type left, char_type right)
548 char_type c = getChar();
553 while ((c = getChar()) != right && good())
560 void Parser::skipSpaceTokens(idocstream & is, char_type c)
562 // skip trailing spaces
563 while (catcode(c) == catSpace || catcode(c) == catNewline)
566 //lyxerr << "putting back: " << c << endl;
571 void Parser::tokenize(istream & is)
573 // eat everything up to the next \end_inset or end of stream
574 // and store it in s for further tokenization
579 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
580 s = s.substr(0, s.size() - 10);
584 // Remove the space after \end_inset
585 if (is.get(c) && c != ' ')
589 tokenize(from_utf8(s));
593 void Parser::tokenize(docstring const & buffer)
595 idocstringstream is(mode_ & Parse::VERBATIM
596 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
597 : buffer, ios::in | ios::binary);
601 //lyxerr << "reading c: " << c << endl;
603 switch (catcode(c)) {
607 if (catcode(c) == catNewline)
608 ; //push_back(Token("par"));
610 push_back(Token('\n', catNewline));
618 while (is.get(c) && catcode(c) != catNewline)
628 error("unexpected end of input");
633 if (catcode(c) == catLetter) {
635 while (is.get(c) && catcode(c) == catLetter)
637 skipSpaceTokens(is, c);
646 push_back(Token(c, catcode(c)));
648 skipSpaceTokens(is, c);
653 if (!(mode_ & Parse::QUIET))
654 lyxerr << "ignoring a char: " << int(c) << endl;
659 push_back(Token(c, catcode(c)));
669 void Parser::dump() const
671 lyxerr << "\nTokens: ";
672 for (unsigned i = 0; i < tokens_.size(); ++i) {
675 lyxerr << tokens_[i];
677 lyxerr << " pos: " << pos_ << endl;
681 void Parser::error(string const & msg)
684 if (!(mode_ & Parse::QUIET)) {
685 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
692 bool Parser::parse(MathAtom & at)
695 MathData ar(buffer_);
696 parse(ar, false, InsetMath::UNDECIDED_MODE);
697 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
698 if (!(mode_ & Parse::QUIET))
699 lyxerr << "unusual contents found: " << ar << endl;
700 at = MathAtom(new InsetMathPar(buffer_, ar));
701 //if (at->nargs() > 0)
702 // at.nucleus()->cell(0) = ar;
704 // lyxerr << "unusual contents found: " << ar << endl;
712 docstring Parser::parse_verbatim_option()
716 if (nextToken().character() == '[') {
717 Token t = getToken();
718 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
719 if (t.cat() == catBegin) {
721 res += '{' + parse_verbatim_item() + '}';
730 docstring Parser::parse_verbatim_item()
734 if (nextToken().cat() == catBegin) {
735 Token t = getToken();
736 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
737 if (t.cat() == catBegin) {
739 res += '{' + parse_verbatim_item() + '}';
749 MathData Parser::parse(unsigned flags, mode_type mode)
751 MathData ar(buffer_);
752 parse(ar, flags, mode);
757 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
759 InsetMathGrid grid(buffer_, 1, 1);
760 parse1(grid, flags, mode, false);
761 array = grid.cell(0);
766 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
769 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
773 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
774 const mode_type mode, const bool numbered)
777 InsetMathGrid::row_type cellrow = 0;
778 InsetMathGrid::col_type cellcol = 0;
779 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
780 Buffer * buf = buffer_;
782 if (grid.asHullInset())
783 grid.asHullInset()->numbered(cellrow, numbered);
786 //lyxerr << " flags: " << flags << endl;
787 //lyxerr << " mode: " << mode << endl;
788 //lyxerr << "grid: " << grid << endl;
791 Token const & t = getToken();
794 lyxerr << "t: " << t << " flags: " << flags << endl;
795 lyxerr << "mode: " << mode << endl;
800 if (flags & FLAG_ITEM) {
802 if (t.cat() == catBegin) {
803 // skip the brace and collect everything to the next matching
805 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
809 // handle only this single token, leave the loop if done
814 if (flags & FLAG_BRACED) {
815 if (t.cat() == catSpace)
818 if (t.cat() != catBegin) {
819 error("opening brace expected");
823 // skip the brace and collect everything to the next matching
825 flags = FLAG_BRACE_LAST;
829 if (flags & FLAG_OPTION) {
830 if (t.cat() == catOther && t.character() == '[') {
832 parse(ar, FLAG_BRACK_LAST, mode);
835 // no option found, put back token and we are done
844 if (t.cat() == catMath) {
845 if (mode != InsetMath::MATH_MODE) {
846 // we are inside some text mode thingy, so opening new math is allowed
847 Token const & n = getToken();
848 if (n.cat() == catMath) {
849 // TeX's $$...$$ syntax for displayed math
850 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
851 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
852 getToken(); // skip the second '$' token
854 // simple $...$ stuff
856 if (mode == InsetMath::UNDECIDED_MODE) {
857 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
858 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
860 // Don't create nested math hulls (bug #5392)
861 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
862 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
867 else if (flags & FLAG_SIMPLE) {
868 // this is the end of the formula
873 error("something strange in the parser");
878 else if (t.cat() == catLetter)
879 cell->push_back(MathAtom(new InsetMathChar(t.character())));
881 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
882 if (cell->empty() || cell->back()->getChar() != ' ')
883 cell->push_back(MathAtom(new InsetMathChar(t.character())));
886 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
887 if (cell->empty() || cell->back()->getChar() != ' ')
888 cell->push_back(MathAtom(new InsetMathChar(' ')));
891 else if (t.cat() == catParameter) {
892 Token const & n = getToken();
893 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
896 else if (t.cat() == catActive)
897 cell->push_back(MathAtom(new InsetMathChar(t.character())));
899 else if (t.cat() == catBegin) {
901 parse(ar, FLAG_BRACE_LAST, mode);
902 // do not create a BraceInset if they were written by LyX
903 // this helps to keep the annoyance of "a choose b" to a minimum
904 if (ar.size() == 1 && ar[0]->extraBraces())
907 cell->push_back(MathAtom(new InsetMathBrace(ar)));
910 else if (t.cat() == catEnd) {
911 if (flags & FLAG_BRACE_LAST)
913 error("found '}' unexpectedly");
914 //LASSERT(false, /**/);
915 //add(cell, '}', LM_TC_TEX);
918 else if (t.cat() == catAlign) {
919 //lyxerr << " column now " << (cellcol + 1)
920 // << " max: " << grid.ncols() << endl;
921 if (flags & FLAG_ALIGN)
923 if (addCol(grid, cellcol))
924 cell = &grid.cell(grid.index(cellrow, cellcol));
927 else if (t.cat() == catSuper || t.cat() == catSub) {
928 bool up = (t.cat() == catSuper);
929 // we need no new script inset if the last thing was a scriptinset,
930 // which has that script already not the same script already
932 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
933 else if (cell->back()->asScriptInset() &&
934 !cell->back()->asScriptInset()->has(up))
935 cell->back().nucleus()->asScriptInset()->ensure(up);
936 else if (cell->back()->asScriptInset())
937 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
939 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
940 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
941 // special handling of {}-bases
942 // Here we could remove the brace inset for things
943 // like {a'}^2 and add the braces back in
944 // InsetMathScript::write().
945 // We do not do it, since it is not possible to detect
946 // reliably whether the braces are needed because the
947 // nucleus contains more than one symbol, or whether
948 // they are needed for unknown commands like \xx{a}_0
949 // or \yy{a}{b}_0. This was done in revision 14819
950 // in an unreliable way. See this thread
951 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
953 // However, we remove empty braces because they look
954 // ugly on screen and we are sure that they were added
955 // by the write() method (and will be re-added on save).
956 if (p->nuc().size() == 1 &&
957 p->nuc().back()->asBraceInset() &&
958 p->nuc().back()->asBraceInset()->cell(0).empty())
961 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
968 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
969 //lyxerr << "finished reading option" << endl;
973 else if (t.cat() == catOther) {
974 char_type c = t.character();
975 if (isAsciiOrMathAlpha(c)
976 || mode_ & Parse::VERBATIM
977 || !(mode_ & Parse::USETEXT)
978 || mode == InsetMath::TEXT_MODE) {
979 cell->push_back(MathAtom(new InsetMathChar(c)));
981 MathAtom at = createInsetMath("text", buf);
982 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
983 while (nextToken().cat() == catOther
984 && !isAsciiOrMathAlpha(nextToken().character())) {
985 c = getToken().character();
986 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
992 else if (t.cat() == catComment) {
995 Token const & t = getToken();
996 if (t.cat() == catNewline)
1000 cell->push_back(MathAtom(new InsetMathComment(buf, s)));
1005 // control sequences
1008 else if (t.cs() == "lyxlock") {
1010 cell->back().nucleus()->lock(true);
1013 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
1015 if (t.cs() == "global")
1019 docstring name = getToken().cs();
1024 while (good() && nextToken().cat() != catBegin) {
1025 pars += getToken().cs();
1032 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1034 // is a version for display attached?
1037 if (nextToken().cat() == catBegin)
1038 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1040 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1041 name, nargs, 0, MacroTypeDef,
1042 vector<MathData>(), def, display)));
1044 if (buf && (mode_ & Parse::TRACKMACRO))
1045 buf->usermacros.insert(name);
1048 else if (t.cs() == "newcommand" ||
1049 t.cs() == "renewcommand" ||
1050 t.cs() == "newlyxcommand") {
1052 if (getToken().cat() != catBegin) {
1053 error("'{' in \\newcommand expected (1) ");
1056 docstring name = getToken().cs();
1057 if (getToken().cat() != catEnd) {
1058 error("'}' in \\newcommand expected");
1063 docstring const arg = getArg('[', ']');
1066 nargs = convert<int>(arg);
1068 // optional argument given?
1071 vector<MathData> optionalValues;
1072 while (nextToken().character() == '[') {
1074 optionalValues.push_back(MathData());
1075 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1080 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1082 // is a version for display attached?
1085 if (nextToken().cat() == catBegin)
1086 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1088 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1089 name, nargs, optionals, MacroTypeNewcommand,
1090 optionalValues, def, display)));
1092 if (buf && (mode_ & Parse::TRACKMACRO))
1093 buf->usermacros.insert(name);
1096 else if (t.cs() == "newcommandx" ||
1097 t.cs() == "renewcommandx") {
1098 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1101 if (nextToken().cat() == catBegin) {
1103 name = getToken().cs();
1104 if (getToken().cat() != catEnd) {
1105 error("'}' in \\newcommandx expected");
1109 name = getToken().cs();
1112 docstring const arg = getArg('[', ']');
1114 error("[num] in \\newcommandx expected");
1117 int nargs = convert<int>(arg);
1121 vector<MathData> optionalValues;
1122 if (nextToken().character() == '[') {
1126 // handle 'opt=value' options, separated by ','.
1128 while (nextToken().character() != ']' && good()) {
1129 if (nextToken().character() >= '1'
1130 && nextToken().character() <= '9') {
1131 // optional value -> get parameter number
1132 int n = getChar() - '0';
1134 error("Arity of \\newcommandx too low "
1135 "for given optional parameter.");
1140 if (getToken().character() != '=') {
1141 error("'=' and optional parameter value "
1142 "expected for \\newcommandx");
1147 int optNum = max(size_t(n), optionalValues.size());
1148 optionalValues.resize(optNum);
1149 optionalValues[n - 1].clear();
1150 while (nextToken().character() != ']'
1151 && nextToken().character() != ',') {
1153 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1154 optionalValues[n - 1].append(data);
1156 optionals = max(n, optionals);
1157 } else if (nextToken().cat() == catLetter) {
1158 // we in fact ignore every non-optional
1163 while (nextToken().cat() == catLetter)
1169 if (nextToken().character() == '=') {
1171 while (nextToken().character() != ']'
1172 && nextToken().character() != ',')
1173 parse(value, FLAG_ITEM,
1174 InsetMath::UNDECIDED_MODE);
1177 error("option for \\newcommandx expected");
1183 if (nextToken().character() == ',') {
1186 } else if (nextToken().character() != ']') {
1187 error("Expecting ',' or ']' in options "
1188 "of \\newcommandx");
1201 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1203 // is a version for display attached?
1206 if (nextToken().cat() == catBegin)
1207 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1209 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1210 name, nargs, optionals, MacroTypeNewcommandx,
1211 optionalValues, def, display)));
1213 if (buf && (mode_ & Parse::TRACKMACRO))
1214 buf->usermacros.insert(name);
1217 else if (t.cs() == "(") {
1218 if (mode == InsetMath::MATH_MODE) {
1219 error("bad math environment");
1222 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1223 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1226 else if (t.cs() == "[") {
1227 if (mode != InsetMath::UNDECIDED_MODE) {
1228 error("bad math environment");
1231 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1232 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1235 else if (t.cs() == "protect")
1236 // ignore \\protect, will hopefully be re-added during output
1239 else if (t.cs() == "end") {
1240 if (flags & FLAG_END) {
1241 // eat environment name
1242 docstring const name = getArg('{', '}');
1243 if (environments_.empty())
1244 error("'found \\end{" + name +
1245 "}' without matching '\\begin{" +
1247 else if (name != environments_.back())
1248 error("'\\end{" + name +
1249 "}' does not match '\\begin{" +
1250 environments_.back() + "}'");
1252 environments_.pop_back();
1253 // Delete empty last row in matrix
1255 // If you abuse InsetMathGrid for
1256 // non-matrix like structures you
1257 // probably need to refine this test.
1258 // Right now we only have to test for
1259 // single line hull insets.
1260 if (grid.nrows() > 1 && innerHull(name))
1261 delEmptyLastRow(grid);
1265 error("found 'end' unexpectedly");
1268 else if (t.cs() == ")") {
1269 if (flags & FLAG_SIMPLE2)
1271 error("found '\\)' unexpectedly");
1274 else if (t.cs() == "]") {
1275 if (flags & FLAG_EQUATION)
1277 error("found '\\]' unexpectedly");
1280 else if (t.cs() == "\\") {
1281 if (flags & FLAG_ALIGN)
1284 if (nextToken().asInput() == "*") {
1286 added = addRow(grid, cellrow, docstring(), false);
1288 added = addRow(grid, cellrow, getArg('[', ']'));
1290 error("missing token after \\\\");
1293 if (grid.asHullInset())
1294 grid.asHullInset()->numbered(
1296 cell = &grid.cell(grid.index(cellrow,
1302 else if (t.cs() == "multicolumn") {
1303 // extract column count and insert dummy cells
1305 parse(count, FLAG_ITEM, mode);
1307 if (!extractNumber(count, cols)) {
1309 lyxerr << " can't extract number of cells from " << count << endl;
1311 // resize the table if necessary
1312 for (int i = 0; i < cols; ++i) {
1313 if (addCol(grid, cellcol)) {
1314 cell = &grid.cell(grid.index(
1316 // mark this as dummy
1317 grid.cellinfo(grid.index(
1318 cellrow, cellcol)).dummy_ = true;
1321 // the last cell is the real thing, not a dummy
1322 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1324 // read special alignment
1326 parse(align, FLAG_ITEM, mode);
1327 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1329 // parse the remaining contents into the "real" cell
1330 parse(*cell, FLAG_ITEM, mode);
1334 else if (t.cs() == "limits" || t.cs() == "nolimits") {
1335 CatCode cat = nextToken().cat();
1336 if (cat == catSuper || cat == catSub)
1337 limits = t.cs() == "limits" ? 1 : -1;
1339 MathAtom at = createInsetMath(t.cs(), buf);
1340 cell->push_back(at);
1344 else if (t.cs() == "nonumber") {
1345 if (grid.asHullInset())
1346 grid.asHullInset()->numbered(cellrow, false);
1349 else if (t.cs() == "number") {
1350 if (grid.asHullInset())
1351 grid.asHullInset()->numbered(cellrow, true);
1354 else if (t.cs() == "hline") {
1355 grid.rowinfo(cellrow).lines_ ++;
1358 else if (t.cs() == "sqrt") {
1360 parse(ar, FLAG_OPTION, mode);
1362 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1363 cell->back().nucleus()->cell(0) = ar;
1364 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1366 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1367 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1371 else if (t.cs() == "unit") {
1372 // Allowed formats \unit[val]{unit}
1374 parse(ar, FLAG_OPTION, mode);
1376 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1377 cell->back().nucleus()->cell(0) = ar;
1378 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1380 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1381 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1385 else if (t.cs() == "unitfrac") {
1386 // Here allowed formats are \unitfrac[val]{num}{denom}
1388 parse(ar, FLAG_OPTION, mode);
1390 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1391 cell->back().nucleus()->cell(2) = ar;
1393 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1395 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1396 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1399 else if (t.cs() == "cfrac") {
1400 // allowed formats are \cfrac[pos]{num}{denom}
1401 docstring const arg = getArg('[', ']');
1402 //lyxerr << "got so far: '" << arg << "'" << endl;
1404 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1405 else if (arg == "r")
1406 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1407 else if (arg.empty() || arg == "c")
1408 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1410 error("found invalid optional argument");
1413 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1414 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1417 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1418 cell->push_back(createInsetMath(t.cs(), buf));
1419 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1420 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1423 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1424 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1425 cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1426 docstring const opt = parse_verbatim_option();
1427 docstring const ref = parse_verbatim_item();
1429 cell->back().nucleus()->cell(1).push_back(
1430 MathAtom(new InsetMathString(opt)));
1432 cell->back().nucleus()->cell(0).push_back(
1433 MathAtom(new InsetMathString(ref)));
1436 else if (t.cs() == "left") {
1438 Token const & tl = getToken();
1439 // \| and \Vert are equivalent, and InsetMathDelim
1441 // FIXME: fix this in InsetMathDelim itself!
1442 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1444 parse(ar, FLAG_RIGHT, mode);
1448 Token const & tr = getToken();
1449 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1450 cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1453 else if (t.cs() == "right") {
1454 if (flags & FLAG_RIGHT)
1456 //lyxerr << "got so far: '" << cell << "'" << endl;
1457 error("Unmatched right delimiter");
1461 else if (t.cs() == "begin") {
1462 docstring const name = getArg('{', '}');
1463 environments_.push_back(name);
1465 if (name == "array" || name == "subarray") {
1466 docstring const valign = parse_verbatim_option() + 'c';
1467 docstring const halign = parse_verbatim_item();
1468 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1469 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1470 parse2(cell->back(), FLAG_END, mode, false);
1473 else if (name == "tabular") {
1474 docstring const valign = parse_verbatim_option() + 'c';
1475 docstring const halign = parse_verbatim_item();
1476 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1477 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1478 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1481 else if (name == "split" || name == "cases") {
1482 cell->push_back(createInsetMath(name, buf));
1483 parse2(cell->back(), FLAG_END, mode, false);
1486 else if (name == "alignedat") {
1487 docstring const valign = parse_verbatim_option() + 'c';
1488 // ignore this for a while
1490 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1491 parse2(cell->back(), FLAG_END, mode, false);
1494 else if (name == "math") {
1495 if (mode == InsetMath::MATH_MODE) {
1496 error("bad math environment");
1499 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
1500 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1503 else if (name == "equation" || name == "equation*"
1504 || name == "displaymath") {
1505 if (mode != InsetMath::UNDECIDED_MODE) {
1506 error("bad math environment");
1509 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1510 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1513 else if (name == "eqnarray" || name == "eqnarray*") {
1514 if (mode != InsetMath::UNDECIDED_MODE) {
1515 error("bad math environment");
1518 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1519 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1522 else if (name == "align" || name == "align*") {
1523 if (mode != InsetMath::UNDECIDED_MODE) {
1524 error("bad math environment");
1527 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1528 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1531 else if (name == "flalign" || name == "flalign*") {
1532 if (mode != InsetMath::UNDECIDED_MODE) {
1533 error("bad math environment");
1536 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1537 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1540 else if (name == "alignat" || name == "alignat*") {
1541 if (mode != InsetMath::UNDECIDED_MODE) {
1542 error("bad math environment");
1545 // ignore this for a while
1547 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlignAt)));
1548 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1551 else if (name == "xalignat" || name == "xalignat*") {
1552 if (mode != InsetMath::UNDECIDED_MODE) {
1553 error("bad math environment");
1556 // ignore this for a while
1558 cell->push_back(MathAtom(new InsetMathHull(buf, hullXAlignAt)));
1559 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1562 else if (name == "xxalignat") {
1563 if (mode != InsetMath::UNDECIDED_MODE) {
1564 error("bad math environment");
1567 // ignore this for a while
1569 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1570 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1573 else if (name == "multline" || name == "multline*") {
1574 if (mode != InsetMath::UNDECIDED_MODE) {
1575 error("bad math environment");
1578 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1579 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1582 else if (name == "gather" || name == "gather*") {
1583 if (mode != InsetMath::UNDECIDED_MODE) {
1584 error("bad math environment");
1587 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1588 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1591 else if (latexkeys const * l = in_word_set(name)) {
1592 if (l->inset == "matrix") {
1593 cell->push_back(createInsetMath(name, buf));
1594 parse2(cell->back(), FLAG_END, mode, false);
1595 } else if (l->inset == "split") {
1596 docstring const valign = parse_verbatim_option() + 'c';
1597 cell->push_back(MathAtom(
1598 new InsetMathSplit(buf, name, (char)valign[0])));
1599 parse2(cell->back(), FLAG_END, mode, false);
1602 if (!(mode_ & Parse::QUIET)) {
1604 lyxerr << "found math environment `"
1606 << "' in symbols file with unsupported inset `"
1607 << to_utf8(l->inset)
1610 // create generic environment inset
1611 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1612 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1618 if (!(mode_ & Parse::QUIET)) {
1620 lyxerr << "found unknown math environment '"
1621 << to_utf8(name) << "'" << endl;
1623 // create generic environment inset
1624 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1625 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1629 else if (t.cs() == "kern") {
1634 Token const & t = getToken();
1638 while (num_tokens--)
1643 if (isValidLength(to_utf8(s)))
1647 cell->push_back(MathAtom(new InsetMathKern));
1649 cell->push_back(MathAtom(new InsetMathKern(s)));
1652 else if (t.cs() == "label") {
1653 // FIXME: This is swallowed in inline formulas
1654 docstring label = parse_verbatim_item();
1657 if (grid.asHullInset()) {
1658 grid.asHullInset()->label(cellrow, label);
1660 cell->push_back(createInsetMath(t.cs(), buf));
1661 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1665 else if (t.cs() == "choose" || t.cs() == "over"
1666 || t.cs() == "atop" || t.cs() == "brace"
1667 || t.cs() == "brack") {
1668 MathAtom at = createInsetMath(t.cs(), buf);
1669 at.nucleus()->cell(0) = *cell;
1671 parse(at.nucleus()->cell(1), flags, mode);
1672 cell->push_back(at);
1676 else if (t.cs() == "color") {
1677 docstring const color = parse_verbatim_item();
1678 cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1679 parse(cell->back().nucleus()->cell(0), flags, mode);
1683 else if (t.cs() == "textcolor") {
1684 docstring const color = parse_verbatim_item();
1685 cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1686 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1689 else if (t.cs() == "normalcolor") {
1690 cell->push_back(createInsetMath(t.cs(), buf));
1691 parse(cell->back().nucleus()->cell(0), flags, mode);
1695 else if (t.cs() == "substack") {
1696 cell->push_back(createInsetMath(t.cs(), buf));
1697 parse2(cell->back(), FLAG_ITEM, mode, false);
1698 // Delete empty last row if present
1699 InsetMathGrid & subgrid =
1700 *(cell->back().nucleus()->asGridInset());
1701 if (subgrid.nrows() > 1)
1702 delEmptyLastRow(subgrid);
1705 else if (t.cs() == "xymatrix") {
1706 odocstringstream os;
1707 while (good() && nextToken().cat() != catBegin)
1708 os << getToken().asInput();
1709 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1710 parse2(cell->back(), FLAG_ITEM, mode, false);
1711 // Delete empty last row if present
1712 InsetMathGrid & subgrid =
1713 *(cell->back().nucleus()->asGridInset());
1714 if (subgrid.nrows() > 1)
1715 delEmptyLastRow(subgrid);
1718 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1719 cell->push_back(createInsetMath(t.cs(), buf));
1720 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1721 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1722 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1725 else if (t.cs() == "tag") {
1726 if (nextToken().character() == '*') {
1728 cell->push_back(createInsetMath(t.cs() + '*', buf));
1730 cell->push_back(createInsetMath(t.cs(), buf));
1731 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1734 else if (t.cs() == "hspace" && nextToken().character() != '*') {
1735 docstring const name = t.cs();
1736 docstring const arg = parse_verbatim_item();
1738 if (isValidLength(to_utf8(arg), &length))
1739 cell->push_back(MathAtom(new InsetMathSpace(length)));
1741 // Since the Length class cannot use length variables
1742 // we must not create an InsetMathSpace.
1743 cell->push_back(MathAtom(new MathMacro(buf, name)));
1745 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1751 else if (t.cs() == "infer") {
1753 parse(ar, FLAG_OPTION, mode);
1754 cell->push_back(createInsetMath(t.cs(), buf));
1755 parse2(cell->back(), FLAG_ITEM, mode, false);
1759 else if (1 && t.cs() == "ar") {
1760 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1761 // try to read target
1762 parse(p->cell(0), FLAG_OTPTION, mode);
1763 // try to read label
1764 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1765 p->up_ = nextToken().cat() == catSuper;
1767 parse(p->cell(1), FLAG_ITEM, mode);
1768 //lyxerr << "read label: " << p->cell(1) << endl;
1771 cell->push_back(MathAtom(p.release()));
1772 //lyxerr << "read cell: " << cell << endl;
1776 else if (t.cs() == "lyxmathsym") {
1778 if (getToken().cat() != catBegin) {
1779 error("'{' expected in \\" + t.cs());
1784 CatCode cat = nextToken().cat();
1785 while (good() && (count || cat != catEnd)) {
1786 if (cat == catBegin)
1788 else if (cat == catEnd)
1790 cmd += getToken().asInput();
1791 cat = nextToken().cat();
1793 if (getToken().cat() != catEnd) {
1794 error("'}' expected in \\" + t.cs());
1799 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1800 for (size_t i = 0; i < cmd.size(); ++i)
1801 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1803 char_type c = rem[0];
1804 cell->push_back(MathAtom(new InsetMathChar(c)));
1805 cmd = rem.substr(1);
1809 } while (cmd.size());
1812 else if (t.cs().size()) {
1813 bool const no_mhchem =
1814 (t.cs() == "ce" || t.cs() == "cf") && buf
1815 && buf->params().use_mhchem == BufferParams::package_off;
1816 bool const is_user_macro = no_mhchem ||
1817 (buf && (mode_ & Parse::TRACKMACRO
1818 ? buf->usermacros.count(t.cs()) != 0
1819 : buf->getMacro(t.cs(), false) != 0));
1820 latexkeys const * l = in_word_set(t.cs());
1821 if (l && !is_user_macro) {
1822 if (l->inset == "big") {
1824 docstring const delim = getToken().asInput();
1825 if (InsetMathBig::isBigInsetDelim(delim))
1826 cell->push_back(MathAtom(
1827 new InsetMathBig(t.cs(), delim)));
1829 cell->push_back(createInsetMath(t.cs(), buf));
1834 else if (l->inset == "font") {
1835 cell->push_back(createInsetMath(t.cs(), buf));
1836 parse(cell->back().nucleus()->cell(0),
1837 FLAG_ITEM, asMode(mode, l->extra));
1840 else if (l->inset == "oldfont") {
1841 cell->push_back(createInsetMath(t.cs(), buf));
1842 parse(cell->back().nucleus()->cell(0),
1843 flags | FLAG_ALIGN, asMode(mode, l->extra));
1844 if (prevToken().cat() != catAlign &&
1845 prevToken().cs() != "\\")
1850 else if (l->inset == "style") {
1851 cell->push_back(createInsetMath(t.cs(), buf));
1852 parse(cell->back().nucleus()->cell(0),
1853 flags | FLAG_ALIGN, mode);
1854 if (prevToken().cat() != catAlign &&
1855 prevToken().cs() != "\\")
1861 MathAtom at = createInsetMath(t.cs(), buf);
1862 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1863 parse(at.nucleus()->cell(i),
1864 FLAG_ITEM, asMode(mode, l->extra));
1865 cell->push_back(at);
1870 bool is_unicode_symbol = false;
1871 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
1873 docstring cmd = prevToken().asInput();
1874 CatCode cat = nextToken().cat();
1875 if (cat == catBegin) {
1877 while (good() && (count || cat != catEnd)) {
1878 cat = nextToken().cat();
1879 cmd += getToken().asInput();
1881 if (cat == catBegin)
1883 else if (cat == catEnd)
1889 Encodings::fromLaTeXCommand(cmd, is_combining);
1891 if (cat == catLetter)
1893 cmd += getToken().asInput();
1895 if (cat == catLetter)
1897 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1900 is_unicode_symbol = true;
1901 cell->push_back(MathAtom(new InsetMathChar(c)));
1903 while (num_tokens--)
1907 if (!is_unicode_symbol) {
1908 MathAtom at = is_user_macro ?
1909 MathAtom(new MathMacro(buf, t.cs()))
1910 : createInsetMath(t.cs(), buf);
1911 InsetMath::mode_type m = mode;
1912 //if (m == InsetMath::UNDECIDED_MODE)
1913 //lyxerr << "default creation: m1: " << m << endl;
1914 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1915 m = at->currentMode();
1916 //lyxerr << "default creation: m2: " << m << endl;
1917 InsetMath::idx_type start = 0;
1918 // this fails on \bigg[...\bigg]
1920 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1923 // at.nucleus()->cell(0) = opt;
1925 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1926 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1929 cell->push_back(at);
1935 if (flags & FLAG_LEAVE) {
1936 flags &= ~FLAG_LEAVE;
1945 } // anonymous namespace
1948 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1950 return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1951 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1955 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1957 return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1958 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1962 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
1965 return Parser(str, f, buf).parse(t);
1969 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
1972 return Parser(lex, f, buf).parse(t);
1976 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
1979 return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
1980 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
1986 fill(theCatcode, theCatcode + 128, catOther);
1987 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1988 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1990 theCatcode[int('\\')] = catEscape;
1991 theCatcode[int('{')] = catBegin;
1992 theCatcode[int('}')] = catEnd;
1993 theCatcode[int('$')] = catMath;
1994 theCatcode[int('&')] = catAlign;
1995 theCatcode[int('\n')] = catNewline;
1996 theCatcode[int('#')] = catParameter;
1997 theCatcode[int('^')] = catSuper;
1998 theCatcode[int('_')] = catSub;
1999 theCatcode[int(0x7f)] = catIgnore;
2000 theCatcode[int(' ')] = catSpace;
2001 theCatcode[int('\t')] = catSpace;
2002 theCatcode[int('\r')] = catNewline;
2003 theCatcode[int('~')] = catActive;
2004 theCatcode[int('%')] = catComment;