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 "InsetMathCancelto.h"
46 #include "InsetMathChar.h"
47 #include "InsetMathColor.h"
48 #include "InsetMathComment.h"
49 #include "InsetMathDelim.h"
50 #include "InsetMathEnsureMath.h"
51 #include "InsetMathEnv.h"
52 #include "InsetMathFrac.h"
53 #include "InsetMathKern.h"
54 #include "MathMacro.h"
55 #include "InsetMathPar.h"
56 #include "InsetMathRef.h"
57 #include "InsetMathRoot.h"
58 #include "InsetMathScript.h"
59 #include "InsetMathSideset.h"
60 #include "InsetMathSpace.h"
61 #include "InsetMathSplit.h"
62 #include "InsetMathSqrt.h"
63 #include "InsetMathStackrel.h"
64 #include "InsetMathString.h"
65 #include "InsetMathTabular.h"
66 #include "MathMacroTemplate.h"
67 #include "MathFactory.h"
68 #include "MathMacroArgument.h"
69 #include "MathSupport.h"
72 #include "BufferParams.h"
76 #include "support/debug.h"
77 #include "support/convert.h"
78 #include "support/docstream.h"
90 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
92 //lyxerr << "handling mode: '" << str << "'" << endl;
93 if (str == "mathmode")
94 return InsetMath::MATH_MODE;
95 if (str == "textmode" || str == "forcetext")
96 return InsetMath::TEXT_MODE;
101 bool stared(docstring const & s)
103 size_t const n = s.size();
104 return n && s[n - 1] == '*';
108 docstring const repl(docstring const & oldstr, char_type const c,
109 docstring const & macro, bool textmode = false)
115 for (i = 0, j = 0; i < oldstr.size(); ++i) {
116 if (c == oldstr[i]) {
117 newstr.append(oldstr, j, i - j);
118 newstr.append(macro);
120 if (macro.size() > 2 && j < oldstr.size())
121 newstr += (textmode && oldstr[j] == ' ' ? '\\' : ' ');
129 newstr.append(oldstr, j, i - j);
134 docstring escapeSpecialChars(docstring const & str, bool textmode)
136 docstring const backslash = textmode ? from_ascii("\\textbackslash")
137 : from_ascii("\\backslash");
138 docstring const caret = textmode ? from_ascii("\\textasciicircum")
139 : from_ascii("\\mathcircumflex");
140 docstring const tilde = textmode ? from_ascii("\\textasciitilde")
141 : from_ascii("\\sim");
143 return repl(repl(repl(repl(repl(repl(repl(repl(repl(repl(str,
144 '\\', backslash, textmode),
145 '^', caret, textmode),
146 '~', tilde, textmode),
147 '_', from_ascii("\\_")),
148 '$', from_ascii("\\$")),
149 '#', from_ascii("\\#")),
150 '&', from_ascii("\\&")),
151 '%', from_ascii("\\%")),
152 '{', from_ascii("\\{")),
153 '}', from_ascii("\\}"));
158 * Add the row \p cellrow to \p grid.
159 * \returns wether the row could be added. Adding a row can fail for
160 * environments like "equation" that have a fixed number of rows.
162 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
163 docstring const & vskip, bool allow_newpage_ = true)
166 if (cellrow == grid.nrows()) {
167 //lyxerr << "adding row " << cellrow << endl;
168 grid.addRow(cellrow - 1);
169 if (cellrow == grid.nrows()) {
170 // We can't add a row to this grid, so let's
171 // append the content of this cell to the previous
173 // This does not happen in well formed .lyx files,
174 // but LyX versions 1.3.x and older could create
175 // such files and tex2lyx can still do that.
177 lyxerr << "ignoring extra row";
179 lyxerr << " with extra space " << to_utf8(vskip);
181 lyxerr << " with no page break allowed";
182 lyxerr << '.' << endl;
186 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
187 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
193 * Add the column \p cellcol to \p grid.
194 * \returns wether the column could be added. Adding a column can fail for
195 * environments like "eqnarray" that have a fixed number of columns.
197 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
200 if (cellcol == grid.ncols()) {
201 //lyxerr << "adding column " << cellcol << endl;
202 grid.addCol(cellcol);
203 if (cellcol == grid.ncols()) {
204 // We can't add a column to this grid, so let's
205 // append the content of this cell to the previous
207 // This does not happen in well formed .lyx files,
208 // but LyX versions 1.3.x and older could create
209 // such files and tex2lyx can still do that.
211 lyxerr << "ignoring extra column." << endl;
220 * Check whether the last row is empty and remove it if yes.
221 * Otherwise the following code
229 * will result in a grid with 3 rows (+ the dummy row that is always present),
230 * because the last '\\' opens a new row.
231 * Note that this is only needed for inner-hull grid types, such as array
232 * or aligned, but not for outer-hull grid types, such as eqnarray or align.
234 void delEmptyLastRow(InsetMathGrid & grid)
236 InsetMathGrid::row_type const row = grid.nrows() - 1;
237 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
238 if (!grid.cell(grid.index(row, col)).empty())
241 // Copy the row information of the empty row (which would contain the
242 // last hline in the example above) to the dummy row and delete the
244 grid.rowinfo(row + 1) = grid.rowinfo(row);
250 * Tell whether the environment name corresponds to an inner-hull grid type.
252 bool innerHull(docstring const & name)
254 // For [bB]matrix, [vV]matrix, and pmatrix we can check the suffix only
255 return name == "array" || name == "cases" || name == "aligned"
256 || name == "alignedat" || name == "gathered" || name == "split"
257 || name == "subarray" || name == "tabular" || name == "matrix"
258 || name == "smallmatrix" || name.substr(1) == "matrix";
262 // These are TeX's catcodes
264 catEscape, // 0 backslash
274 catSpace, // 10 space
275 catLetter, // 11 a-zA-Z
276 catOther, // 12 none of the above
279 catInvalid // 15 <delete>
282 CatCode theCatcode[128];
285 inline CatCode catcode(char_type c)
287 /* The only characters that are not catOther lie in the pure ASCII
288 * range. Therefore theCatcode has only 128 entries.
289 * TeX itself deals with 8bit characters, so if needed this table
290 * could be enlarged to 256 entries.
291 * Any larger value does not make sense, since the fact that we use
292 * unicode internally does not change Knuth's TeX engine.
293 * Apart from that a table for the full 21bit UCS4 range would waste
294 * too much memory. */
298 return theCatcode[c];
303 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
304 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
305 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
306 FLAG_END = 1 << 3, // next \\end ends the parsing process
307 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
308 FLAG_TEXTMODE = 1 << 5, // we are in a box
309 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
310 FLAG_LEAVE = 1 << 7, // leave the loop at the end
311 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
312 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
313 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
314 FLAG_OPTION = 1 << 11, // read [...] style option
315 FLAG_BRACED = 1 << 12 // read {...} style argument
320 // Helper class for parsing
326 Token() : cs_(), char_(0), cat_(catIgnore) {}
328 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
330 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
333 docstring const & cs() const { return cs_; }
335 CatCode cat() const { return cat_; }
337 char_type character() const { return char_; }
339 docstring asString() const { return !cs_.empty() ? cs_ : docstring(1, char_); }
341 docstring asInput() const { return !cs_.empty() ? '\\' + cs_ : docstring(1, char_); }
353 ostream & operator<<(ostream & os, Token const & t)
355 if (!t.cs().empty()) {
356 docstring const & cs = t.cs();
357 // FIXME: For some strange reason, the stream operator instanciate
358 // a new Token before outputting the contents of t.cs().
359 // Because of this the line
361 // below becomes recursive.
362 // In order to avoid that we return early:
365 os << '\\' << to_utf8(cs);
367 else if (t.cat() == catLetter)
370 os << '[' << t.character() << ',' << t.cat() << ']';
378 typedef InsetMath::mode_type mode_type;
380 typedef Parse::flags parse_mode;
383 Parser(Lexer & lex, parse_mode mode, Buffer * buf);
384 /// Only use this for reading from .lyx file format, for the reason
385 /// see Parser::tokenize(istream &).
386 Parser(istream & is, parse_mode mode, Buffer * buf);
388 Parser(docstring const & str, parse_mode mode, Buffer * buf);
391 bool parse(MathAtom & at);
393 bool parse(MathData & array, unsigned flags, mode_type mode);
395 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
398 MathData parse(unsigned flags, mode_type mode);
400 int lineno() const { return lineno_; }
406 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
407 /// get arg delimited by 'left' and 'right'
408 docstring getArg(char_type left, char_type right);
412 void error(string const & msg);
413 void error(docstring const & msg) { error(to_utf8(msg)); }
414 /// dump contents to screen
416 /// Only use this for reading from .lyx file format (see
417 /// implementation for reason)
418 void tokenize(istream & is);
420 void tokenize(docstring const & s);
422 void skipSpaceTokens(idocstream & is, char_type c);
424 void push_back(Token const & t);
428 Token const & prevToken() const;
430 Token const & nextToken() const;
432 Token const & getToken();
433 /// skips spaces if any
436 void lex(docstring const & s);
440 docstring parse_verbatim_item();
442 docstring parse_verbatim_option();
447 vector<Token> tokens_;
450 /// Stack of active environments
451 vector<docstring> environments_;
461 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
462 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
465 tokenize(lexer.getStream());
470 Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
471 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
477 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
478 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
484 void Parser::push_back(Token const & t)
486 tokens_.push_back(t);
490 void Parser::pop_back()
496 Token const & Parser::prevToken() const
498 static const Token dummy;
499 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
503 Token const & Parser::nextToken() const
505 static const Token dummy;
506 return good() ? tokens_[pos_] : dummy;
510 Token const & Parser::getToken()
512 static const Token dummy;
513 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
514 return good() ? tokens_[pos_++] : dummy;
518 void Parser::skipSpaces()
520 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
525 void Parser::putback()
531 bool Parser::good() const
533 return pos_ < tokens_.size();
537 char_type Parser::getChar()
540 error("The input stream is not well...");
543 return tokens_[pos_++].character();
547 docstring Parser::getArg(char_type left, char_type right)
555 char_type c = getChar();
560 while ((c = getChar()) != right && good())
567 void Parser::skipSpaceTokens(idocstream & is, char_type c)
569 // skip trailing spaces
570 while (catcode(c) == catSpace || catcode(c) == catNewline)
573 //lyxerr << "putting back: " << c << endl;
578 void Parser::tokenize(istream & is)
580 // eat everything up to the next \end_inset or end of stream
581 // and store it in s for further tokenization
586 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
587 s = s.substr(0, s.size() - 10);
591 // Remove the space after \end_inset
592 if (is.get(c) && c != ' ')
596 tokenize(from_utf8(s));
600 void Parser::tokenize(docstring const & buffer)
602 idocstringstream is(mode_ & Parse::VERBATIM
603 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
604 : buffer, ios::in | ios::binary);
608 //lyxerr << "reading c: " << c << endl;
610 switch (catcode(c)) {
614 if (catcode(c) == catNewline)
615 ; //push_back(Token("par"));
617 push_back(Token('\n', catNewline));
625 while (is.get(c) && catcode(c) != catNewline)
635 error("unexpected end of input");
640 if (catcode(c) == catLetter) {
642 while (is.get(c) && catcode(c) == catLetter)
644 skipSpaceTokens(is, c);
653 push_back(Token(c, catcode(c)));
655 skipSpaceTokens(is, c);
660 if (!(mode_ & Parse::QUIET))
661 lyxerr << "ignoring a char: " << int(c) << endl;
666 push_back(Token(c, catcode(c)));
676 void Parser::dump() const
678 lyxerr << "\nTokens: ";
679 for (unsigned i = 0; i < tokens_.size(); ++i) {
682 lyxerr << tokens_[i];
684 lyxerr << " pos: " << pos_ << endl;
688 void Parser::error(string const & msg)
691 if (!(mode_ & Parse::QUIET)) {
692 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
699 bool Parser::parse(MathAtom & at)
702 MathData ar(buffer_);
703 parse(ar, false, InsetMath::UNDECIDED_MODE);
704 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
705 if (!(mode_ & Parse::QUIET))
706 lyxerr << "unusual contents found: " << ar << endl;
707 at = MathAtom(new InsetMathPar(buffer_, ar));
708 //if (at->nargs() > 0)
709 // at.nucleus()->cell(0) = ar;
711 // lyxerr << "unusual contents found: " << ar << endl;
719 docstring Parser::parse_verbatim_option()
723 if (nextToken().character() == '[') {
724 Token t = getToken();
725 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
726 if (t.cat() == catBegin) {
728 res += '{' + parse_verbatim_item() + '}';
737 docstring Parser::parse_verbatim_item()
741 if (nextToken().cat() == catBegin) {
742 Token t = getToken();
743 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
744 if (t.cat() == catBegin) {
746 res += '{' + parse_verbatim_item() + '}';
756 MathData Parser::parse(unsigned flags, mode_type mode)
758 MathData ar(buffer_);
759 parse(ar, flags, mode);
764 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
766 InsetMathGrid grid(buffer_, 1, 1);
767 parse1(grid, flags, mode, false);
768 array = grid.cell(0);
773 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
776 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
780 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
781 const mode_type mode, const bool numbered)
784 InsetMathGrid::row_type cellrow = 0;
785 InsetMathGrid::col_type cellcol = 0;
786 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
787 Buffer * buf = buffer_;
789 if (grid.asHullInset())
790 grid.asHullInset()->numbered(cellrow, numbered);
793 //lyxerr << " flags: " << flags << endl;
794 //lyxerr << " mode: " << mode << endl;
795 //lyxerr << "grid: " << grid << endl;
798 Token const & t = getToken();
801 lyxerr << "t: " << t << " flags: " << flags << endl;
802 lyxerr << "mode: " << mode << endl;
807 if (flags & FLAG_ITEM) {
809 if (t.cat() == catBegin) {
810 // skip the brace and collect everything to the next matching
812 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
816 // handle only this single token, leave the loop if done
821 if (flags & FLAG_BRACED) {
822 if (t.cat() == catSpace)
825 if (t.cat() != catBegin) {
826 error("opening brace expected");
830 // skip the brace and collect everything to the next matching
832 flags = FLAG_BRACE_LAST;
836 if (flags & FLAG_OPTION) {
837 if (t.cat() == catOther && t.character() == '[') {
839 parse(ar, FLAG_BRACK_LAST, mode);
842 // no option found, put back token and we are done
851 if (t.cat() == catMath) {
852 if (mode != InsetMath::MATH_MODE) {
853 // we are inside some text mode thingy, so opening new math is allowed
854 Token const & n = getToken();
855 if (n.cat() == catMath) {
856 // TeX's $$...$$ syntax for displayed math
857 if (mode == InsetMath::UNDECIDED_MODE) {
858 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
859 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
860 getToken(); // skip the second '$' token
862 // This is not an outer hull and display math is
863 // not allowed inside text mode environments.
864 error("bad math environment");
868 // simple $...$ stuff
870 if (mode == InsetMath::UNDECIDED_MODE) {
871 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
872 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
874 // Don't create nested math hulls (bug #5392)
875 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
876 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
881 else if (flags & FLAG_SIMPLE) {
882 // this is the end of the formula
887 Token const & n = getToken();
888 if (n.cat() == catMath) {
889 error("something strange in the parser");
892 // This is inline math ($...$), but the parser thinks we are
893 // already in math mode and latex would issue an error, unless we
894 // are inside a text mode user macro. We have no way to tell, so
895 // let's play safe by using \ensuremath, as it will work in any case.
897 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
898 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
903 else if (t.cat() == catLetter)
904 cell->push_back(MathAtom(new InsetMathChar(t.character())));
906 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
907 if (cell->empty() || cell->back()->getChar() != ' ')
908 cell->push_back(MathAtom(new InsetMathChar(t.character())));
911 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
912 if (cell->empty() || cell->back()->getChar() != ' ')
913 cell->push_back(MathAtom(new InsetMathChar(' ')));
916 else if (t.cat() == catParameter) {
917 Token const & n = getToken();
918 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
921 else if (t.cat() == catActive)
922 cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), "")));
924 else if (t.cat() == catBegin) {
926 parse(ar, FLAG_BRACE_LAST, mode);
927 // do not create a BraceInset if they were written by LyX
928 // this helps to keep the annoyance of "a choose b" to a minimum
929 if (ar.size() == 1 && ar[0]->extraBraces())
932 cell->push_back(MathAtom(new InsetMathBrace(ar)));
935 else if (t.cat() == catEnd) {
936 if (flags & FLAG_BRACE_LAST)
938 error("found '}' unexpectedly");
939 //LASSERT(false, /**/);
940 //add(cell, '}', LM_TC_TEX);
943 else if (t.cat() == catAlign) {
944 //lyxerr << " column now " << (cellcol + 1)
945 // << " max: " << grid.ncols() << endl;
946 if (flags & FLAG_ALIGN)
948 if (addCol(grid, cellcol))
949 cell = &grid.cell(grid.index(cellrow, cellcol));
952 else if (t.cat() == catSuper || t.cat() == catSub) {
953 bool up = (t.cat() == catSuper);
954 // we need no new script inset if the last thing was a scriptinset,
955 // which has that script already not the same script already
957 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
958 else if (cell->back()->asScriptInset() &&
959 !cell->back()->asScriptInset()->has(up))
960 cell->back().nucleus()->asScriptInset()->ensure(up);
961 else if (cell->back()->asScriptInset())
962 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
964 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
965 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
966 // special handling of {}-bases
967 // Here we could remove the brace inset for things
968 // like {a'}^2 and add the braces back in
969 // InsetMathScript::write().
970 // We do not do it, since it is not possible to detect
971 // reliably whether the braces are needed because the
972 // nucleus contains more than one symbol, or whether
973 // they are needed for unknown commands like \xx{a}_0
974 // or \yy{a}{b}_0. This was done in revision 14819
975 // in an unreliable way. See this thread
976 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
978 // However, we remove empty braces because they look
979 // ugly on screen and we are sure that they were added
980 // by the write() method (and will be re-added on save).
981 if (p->nuc().size() == 1 &&
982 p->nuc().back()->asBraceInset() &&
983 p->nuc().back()->asBraceInset()->cell(0).empty())
986 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
993 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
994 //lyxerr << "finished reading option" << endl;
998 else if (t.cat() == catOther) {
999 char_type c = t.character();
1000 if (isAsciiOrMathAlpha(c)
1001 || mode_ & Parse::VERBATIM
1002 || !(mode_ & Parse::USETEXT)
1003 || mode == InsetMath::TEXT_MODE) {
1004 cell->push_back(MathAtom(new InsetMathChar(c)));
1006 MathAtom at = createInsetMath("text", buf);
1007 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1008 while (nextToken().cat() == catOther
1009 && !isAsciiOrMathAlpha(nextToken().character())) {
1010 c = getToken().character();
1011 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1013 cell->push_back(at);
1017 else if (t.cat() == catComment) {
1020 Token const & t = getToken();
1021 if (t.cat() == catNewline)
1025 cell->push_back(MathAtom(new InsetMathComment(buf, s)));
1030 // control sequences
1033 else if (t.cs() == "lyxlock") {
1035 cell->back().nucleus()->lock(true);
1038 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
1040 if (t.cs() == "global")
1044 docstring name = getToken().cs();
1049 while (good() && nextToken().cat() != catBegin) {
1050 pars += getToken().cs();
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, 0, MacroTypeDef,
1067 vector<MathData>(), def, display)));
1069 if (buf && (mode_ & Parse::TRACKMACRO))
1070 buf->usermacros.insert(name);
1073 else if (t.cs() == "newcommand" ||
1074 t.cs() == "renewcommand" ||
1075 t.cs() == "newlyxcommand") {
1077 if (getToken().cat() != catBegin) {
1078 error("'{' in \\newcommand expected (1) ");
1081 docstring name = getToken().cs();
1082 if (getToken().cat() != catEnd) {
1083 error("'}' in \\newcommand expected");
1088 docstring const arg = getArg('[', ']');
1091 nargs = convert<int>(arg);
1093 // optional argument given?
1096 vector<MathData> optionalValues;
1097 while (nextToken().character() == '[') {
1099 optionalValues.push_back(MathData());
1100 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1105 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1107 // is a version for display attached?
1110 if (nextToken().cat() == catBegin)
1111 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1113 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1114 name, nargs, optionals, MacroTypeNewcommand,
1115 optionalValues, def, display)));
1117 if (buf && (mode_ & Parse::TRACKMACRO))
1118 buf->usermacros.insert(name);
1121 else if (t.cs() == "newcommandx" ||
1122 t.cs() == "renewcommandx") {
1123 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1126 if (nextToken().cat() == catBegin) {
1128 name = getToken().cs();
1129 if (getToken().cat() != catEnd) {
1130 error("'}' in \\newcommandx expected");
1134 name = getToken().cs();
1137 docstring const arg = getArg('[', ']');
1139 error("[num] in \\newcommandx expected");
1142 int nargs = convert<int>(arg);
1146 vector<MathData> optionalValues;
1147 if (nextToken().character() == '[') {
1151 // handle 'opt=value' options, separated by ','.
1153 while (nextToken().character() != ']' && good()) {
1154 if (nextToken().character() >= '1'
1155 && nextToken().character() <= '9') {
1156 // optional value -> get parameter number
1157 int n = getChar() - '0';
1159 error("Arity of \\newcommandx too low "
1160 "for given optional parameter.");
1165 if (getToken().character() != '=') {
1166 error("'=' and optional parameter value "
1167 "expected for \\newcommandx");
1172 int optNum = max(size_t(n), optionalValues.size());
1173 optionalValues.resize(optNum);
1174 optionalValues[n - 1].clear();
1175 while (nextToken().character() != ']'
1176 && nextToken().character() != ',') {
1178 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1179 optionalValues[n - 1].append(data);
1181 optionals = max(n, optionals);
1182 } else if (nextToken().cat() == catLetter) {
1183 // we in fact ignore every non-optional
1188 while (nextToken().cat() == catLetter)
1194 if (nextToken().character() == '=') {
1196 while (nextToken().character() != ']'
1197 && nextToken().character() != ',')
1198 parse(value, FLAG_ITEM,
1199 InsetMath::UNDECIDED_MODE);
1202 error("option for \\newcommandx expected");
1208 if (nextToken().character() == ',') {
1211 } else if (nextToken().character() != ']') {
1212 error("Expecting ',' or ']' in options "
1213 "of \\newcommandx");
1226 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1228 // is a version for display attached?
1231 if (nextToken().cat() == catBegin)
1232 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1234 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1235 name, nargs, optionals, MacroTypeNewcommandx,
1236 optionalValues, def, display)));
1238 if (buf && (mode_ & Parse::TRACKMACRO))
1239 buf->usermacros.insert(name);
1242 else if (t.cs() == "(") {
1243 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1244 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE2, InsetMath::MATH_MODE);
1247 else if (t.cs() == "[") {
1248 if (mode != InsetMath::UNDECIDED_MODE) {
1249 error("bad math environment");
1252 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1253 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1256 else if (t.cs() == "protect")
1257 // ignore \\protect, will hopefully be re-added during output
1260 else if (t.cs() == "end") {
1261 if (flags & FLAG_END) {
1262 // eat environment name
1263 docstring const name = getArg('{', '}');
1264 if (environments_.empty())
1265 error("'found \\end{" + name +
1266 "}' without matching '\\begin{" +
1268 else if (name != environments_.back())
1269 error("'\\end{" + name +
1270 "}' does not match '\\begin{" +
1271 environments_.back() + "}'");
1273 environments_.pop_back();
1274 // Delete empty last row in matrix
1276 // If you abuse InsetMathGrid for
1277 // non-matrix like structures you
1278 // probably need to refine this test.
1279 // Right now we only have to test for
1280 // single line hull insets.
1281 if (grid.nrows() > 1 && innerHull(name))
1282 delEmptyLastRow(grid);
1286 error("found 'end' unexpectedly");
1289 else if (t.cs() == ")") {
1290 if (flags & FLAG_SIMPLE2)
1292 error("found '\\)' unexpectedly");
1295 else if (t.cs() == "]") {
1296 if (flags & FLAG_EQUATION)
1298 error("found '\\]' unexpectedly");
1301 else if (t.cs() == "\\") {
1302 if (flags & FLAG_ALIGN)
1305 if (nextToken().asInput() == "*") {
1307 added = addRow(grid, cellrow, docstring(), false);
1309 added = addRow(grid, cellrow, getArg('[', ']'));
1311 error("missing token after \\\\");
1314 if (grid.asHullInset())
1315 grid.asHullInset()->numbered(
1317 cell = &grid.cell(grid.index(cellrow,
1323 else if (t.cs() == "multicolumn") {
1324 // extract column count and insert dummy cells
1326 parse(count, FLAG_ITEM, mode);
1328 if (!extractNumber(count, cols)) {
1330 lyxerr << " can't extract number of cells from " << count << endl;
1332 // resize the table if necessary
1333 for (int i = 0; i < cols; ++i) {
1334 if (addCol(grid, cellcol)) {
1335 cell = &grid.cell(grid.index(
1337 // mark this as dummy
1338 grid.cellinfo(grid.index(
1339 cellrow, cellcol)).dummy_ = true;
1342 // the last cell is the real thing, not a dummy
1343 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1345 // read special alignment
1347 parse(align, FLAG_ITEM, mode);
1348 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1350 // parse the remaining contents into the "real" cell
1351 parse(*cell, FLAG_ITEM, mode);
1355 else if (t.cs() == "limits" || t.cs() == "nolimits") {
1356 CatCode const cat = nextToken().cat();
1357 if (cat == catSuper || cat == catSub)
1358 limits = t.cs() == "limits" ? 1 : -1;
1360 MathAtom at = createInsetMath(t.cs(), buf);
1361 cell->push_back(at);
1365 else if (t.cs() == "nonumber") {
1366 if (grid.asHullInset())
1367 grid.asHullInset()->numbered(cellrow, false);
1370 else if (t.cs() == "number") {
1371 if (grid.asHullInset())
1372 grid.asHullInset()->numbered(cellrow, true);
1375 else if (t.cs() == "hline") {
1376 grid.rowinfo(cellrow).lines_ ++;
1379 else if (t.cs() == "sqrt") {
1381 parse(ar, FLAG_OPTION, mode);
1383 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1384 cell->back().nucleus()->cell(0) = ar;
1385 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1387 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1388 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1392 else if (t.cs() == "cancelto") {
1394 parse(ar, FLAG_ITEM, mode);
1395 cell->push_back(MathAtom(new InsetMathCancelto(buf)));
1396 cell->back().nucleus()->cell(1) = ar;
1397 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1400 else if (t.cs() == "unit") {
1401 // Allowed formats \unit[val]{unit}
1403 parse(ar, FLAG_OPTION, mode);
1405 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1406 cell->back().nucleus()->cell(0) = ar;
1407 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1409 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1410 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1414 else if (t.cs() == "unitfrac") {
1415 // Here allowed formats are \unitfrac[val]{num}{denom}
1417 parse(ar, FLAG_OPTION, mode);
1419 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1420 cell->back().nucleus()->cell(2) = ar;
1422 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1424 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1425 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1428 else if (t.cs() == "cfrac") {
1429 // allowed formats are \cfrac[pos]{num}{denom}
1430 docstring const arg = getArg('[', ']');
1431 //lyxerr << "got so far: '" << arg << "'" << endl;
1433 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1434 else if (arg == "r")
1435 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1436 else if (arg.empty() || arg == "c")
1437 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1439 error("found invalid optional argument");
1442 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1443 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1446 else if (t.cs() == "sideset") {
1447 // Here allowed formats are \sideset{_{bl}^{tl}}{_{br}^{tr}}{operator}
1449 InsetMathScript * script[2] = {0, 0};
1450 for (int i = 0; i < 2; ++i) {
1451 parse(ar[i], FLAG_ITEM, mode);
1452 if (ar[i].size() == 1)
1453 script[i] = ar[i][0].nucleus()->asScriptInset();
1455 bool const hasscript[2] = {script[0] ? true : false, script[1] ? true : false};
1456 cell->push_back(MathAtom(new InsetMathSideset(buf, hasscript[0], hasscript[1])));
1458 if (script[0]->hasDown())
1459 cell->back().nucleus()->cell(1) = script[0]->down();
1460 if (script[0]->hasUp())
1461 cell->back().nucleus()->cell(2) = script[0]->up();
1463 cell->back().nucleus()->cell(1) = ar[0];
1465 if (script[1]->hasDown())
1466 cell->back().nucleus()->cell(2 + hasscript[0]) = script[1]->down();
1467 if (script[1]->hasUp())
1468 cell->back().nucleus()->cell(3 + hasscript[0]) = script[1]->up();
1470 cell->back().nucleus()->cell(2 + hasscript[0]) = ar[1];
1471 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1474 else if (t.cs() == "stackrel") {
1475 // Here allowed formats are \stackrel[subscript]{superscript}{operator}
1477 parse(ar, FLAG_OPTION, mode);
1478 cell->push_back(MathAtom(new InsetMathStackrel(buf, !ar.empty())));
1480 cell->back().nucleus()->cell(2) = ar;
1481 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1482 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1485 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1486 cell->push_back(createInsetMath(t.cs(), buf));
1487 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1488 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1491 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1492 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1493 cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1494 docstring const opt = parse_verbatim_option();
1495 docstring const ref = parse_verbatim_item();
1497 cell->back().nucleus()->cell(1).push_back(
1498 MathAtom(new InsetMathString(opt)));
1500 cell->back().nucleus()->cell(0).push_back(
1501 MathAtom(new InsetMathString(ref)));
1504 else if (t.cs() == "left") {
1506 Token const & tl = getToken();
1507 // \| and \Vert are equivalent, and InsetMathDelim
1509 // FIXME: fix this in InsetMathDelim itself!
1510 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1512 parse(ar, FLAG_RIGHT, mode);
1516 Token const & tr = getToken();
1517 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1518 cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1521 else if (t.cs() == "right") {
1522 if (flags & FLAG_RIGHT)
1524 //lyxerr << "got so far: '" << cell << "'" << endl;
1525 error("Unmatched right delimiter");
1529 else if (t.cs() == "begin") {
1530 docstring const name = getArg('{', '}');
1534 error("found invalid environment");
1538 environments_.push_back(name);
1540 if (name == "array" || name == "subarray") {
1541 docstring const valign = parse_verbatim_option() + 'c';
1542 docstring const halign = parse_verbatim_item();
1543 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1544 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1545 parse2(cell->back(), FLAG_END, mode, false);
1548 else if (name == "tabular") {
1549 docstring const valign = parse_verbatim_option() + 'c';
1550 docstring const halign = parse_verbatim_item();
1551 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1552 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1553 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1556 else if (name == "split" || name == "cases") {
1557 cell->push_back(createInsetMath(name, buf));
1558 parse2(cell->back(), FLAG_END, mode, false);
1561 else if (name == "alignedat") {
1562 docstring const valign = parse_verbatim_option() + 'c';
1563 // ignore this for a while
1565 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1566 parse2(cell->back(), FLAG_END, mode, false);
1569 else if (name == "math") {
1570 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1571 parse(cell->back().nucleus()->cell(0), FLAG_END, InsetMath::MATH_MODE);
1574 else if (name == "equation" || name == "equation*"
1575 || name == "displaymath") {
1576 if (mode != InsetMath::UNDECIDED_MODE) {
1577 error("bad math environment");
1580 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1581 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1584 else if (name == "eqnarray" || name == "eqnarray*") {
1585 if (mode != InsetMath::UNDECIDED_MODE) {
1586 error("bad math environment");
1589 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1590 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1593 else if (name == "align" || name == "align*") {
1594 if (mode != InsetMath::UNDECIDED_MODE) {
1595 error("bad math environment");
1598 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1599 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1602 else if (name == "flalign" || name == "flalign*") {
1603 if (mode != InsetMath::UNDECIDED_MODE) {
1604 error("bad math environment");
1607 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1608 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1611 else if (name == "alignat" || name == "alignat*") {
1612 if (mode != InsetMath::UNDECIDED_MODE) {
1613 error("bad math environment");
1616 // ignore this for a while
1618 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlignAt)));
1619 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1622 else if (name == "xalignat" || name == "xalignat*") {
1623 if (mode != InsetMath::UNDECIDED_MODE) {
1624 error("bad math environment");
1627 // ignore this for a while
1629 cell->push_back(MathAtom(new InsetMathHull(buf, hullXAlignAt)));
1630 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1633 else if (name == "xxalignat") {
1634 if (mode != InsetMath::UNDECIDED_MODE) {
1635 error("bad math environment");
1638 // ignore this for a while
1640 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1641 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1644 else if (name == "multline" || name == "multline*") {
1645 if (mode != InsetMath::UNDECIDED_MODE) {
1646 error("bad math environment");
1649 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1650 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1653 else if (name == "gather" || name == "gather*") {
1654 if (mode != InsetMath::UNDECIDED_MODE) {
1655 error("bad math environment");
1658 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1659 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1662 else if (latexkeys const * l = in_word_set(name)) {
1663 if (l->inset == "matrix") {
1664 cell->push_back(createInsetMath(name, buf));
1665 parse2(cell->back(), FLAG_END, mode, false);
1666 } else if (l->inset == "split") {
1667 docstring const valign = parse_verbatim_option() + 'c';
1668 cell->push_back(MathAtom(
1669 new InsetMathSplit(buf, name, (char)valign[0])));
1670 parse2(cell->back(), FLAG_END, mode, false);
1673 if (!(mode_ & Parse::QUIET)) {
1675 lyxerr << "found math environment `"
1677 << "' in symbols file with unsupported inset `"
1678 << to_utf8(l->inset)
1681 // create generic environment inset
1682 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1683 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1689 if (!(mode_ & Parse::QUIET)) {
1691 lyxerr << "found unknown math environment '"
1692 << to_utf8(name) << "'" << endl;
1694 // create generic environment inset
1695 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1696 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1700 else if (t.cs() == "kern") {
1705 Token const & t = getToken();
1709 while (num_tokens--)
1714 if (isValidLength(to_utf8(s)))
1718 cell->push_back(MathAtom(new InsetMathKern));
1720 cell->push_back(MathAtom(new InsetMathKern(s)));
1723 else if (t.cs() == "label") {
1724 // FIXME: This is swallowed in inline formulas
1725 docstring label = parse_verbatim_item();
1728 if (grid.asHullInset()) {
1729 grid.asHullInset()->label(cellrow, label);
1731 cell->push_back(createInsetMath(t.cs(), buf));
1732 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1736 else if (t.cs() == "choose" || t.cs() == "over"
1737 || t.cs() == "atop" || t.cs() == "brace"
1738 || t.cs() == "brack") {
1739 MathAtom at = createInsetMath(t.cs(), buf);
1740 at.nucleus()->cell(0) = *cell;
1742 parse(at.nucleus()->cell(1), flags, mode);
1743 cell->push_back(at);
1747 else if (t.cs() == "color") {
1748 docstring const color = parse_verbatim_item();
1749 cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1750 parse(cell->back().nucleus()->cell(0), flags, mode);
1754 else if (t.cs() == "textcolor") {
1755 docstring const color = parse_verbatim_item();
1756 cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1757 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1760 else if (t.cs() == "normalcolor") {
1761 cell->push_back(createInsetMath(t.cs(), buf));
1762 parse(cell->back().nucleus()->cell(0), flags, mode);
1766 else if (t.cs() == "substack") {
1767 cell->push_back(createInsetMath(t.cs(), buf));
1768 parse2(cell->back(), FLAG_ITEM, mode, false);
1769 // Delete empty last row if present
1770 InsetMathGrid & subgrid =
1771 *(cell->back().nucleus()->asGridInset());
1772 if (subgrid.nrows() > 1)
1773 delEmptyLastRow(subgrid);
1776 else if (t.cs() == "xymatrix") {
1777 odocstringstream os;
1778 while (good() && nextToken().cat() != catBegin)
1779 os << getToken().asInput();
1780 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1781 parse2(cell->back(), FLAG_ITEM, mode, false);
1782 // Delete empty last row if present
1783 InsetMathGrid & subgrid =
1784 *(cell->back().nucleus()->asGridInset());
1785 if (subgrid.nrows() > 1)
1786 delEmptyLastRow(subgrid);
1789 else if (t.cs() == "Diagram") {
1790 odocstringstream os;
1791 while (good() && nextToken().cat() != catBegin)
1792 os << getToken().asInput();
1793 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1794 parse2(cell->back(), FLAG_ITEM, mode, false);
1797 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1798 cell->push_back(createInsetMath(t.cs(), buf));
1799 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1800 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1801 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1804 else if (t.cs() == "tag") {
1805 if (nextToken().character() == '*') {
1807 cell->push_back(createInsetMath(t.cs() + '*', buf));
1809 cell->push_back(createInsetMath(t.cs(), buf));
1810 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1813 else if (t.cs() == "hspace") {
1814 bool const prot = nextToken().character() == '*';
1817 docstring const name = t.cs();
1818 docstring const arg = parse_verbatim_item();
1820 if (prot && arg == "\\fill")
1821 cell->push_back(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
1822 else if (isValidLength(to_utf8(arg), &length))
1823 cell->push_back(MathAtom(new InsetMathSpace(length, prot)));
1825 // Since the Length class cannot use length variables
1826 // we must not create an InsetMathSpace.
1827 cell->push_back(MathAtom(new MathMacro(buf, name)));
1829 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1835 else if (t.cs() == "infer") {
1837 parse(ar, FLAG_OPTION, mode);
1838 cell->push_back(createInsetMath(t.cs(), buf));
1839 parse2(cell->back(), FLAG_ITEM, mode, false);
1843 else if (1 && t.cs() == "ar") {
1844 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1845 // try to read target
1846 parse(p->cell(0), FLAG_OTPTION, mode);
1847 // try to read label
1848 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1849 p->up_ = nextToken().cat() == catSuper;
1851 parse(p->cell(1), FLAG_ITEM, mode);
1852 //lyxerr << "read label: " << p->cell(1) << endl;
1855 cell->push_back(MathAtom(p.release()));
1856 //lyxerr << "read cell: " << cell << endl;
1860 else if (t.cs() == "lyxmathsym") {
1862 if (getToken().cat() != catBegin) {
1863 error("'{' expected in \\" + t.cs());
1868 CatCode cat = nextToken().cat();
1869 while (good() && (count || cat != catEnd)) {
1870 if (cat == catBegin)
1872 else if (cat == catEnd)
1874 cmd += getToken().asInput();
1875 cat = nextToken().cat();
1877 if (getToken().cat() != catEnd) {
1878 error("'}' expected in \\" + t.cs());
1884 cmd = Encodings::fromLaTeXCommand(cmd,
1885 Encodings::MATH_CMD | Encodings::TEXT_CMD,
1887 for (size_t i = 0; i < cmd.size(); ++i)
1888 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1890 char_type c = rem[0];
1891 cell->push_back(MathAtom(new InsetMathChar(c)));
1892 cmd = rem.substr(1);
1896 } while (!cmd.empty());
1899 else if (!t.cs().empty()) {
1900 bool const no_mhchem =
1901 (t.cs() == "ce" || t.cs() == "cf")
1902 && buf && buf->params().use_package("mhchem") ==
1903 BufferParams::package_off;
1905 bool const is_user_macro = no_mhchem ||
1906 (buf && (mode_ & Parse::TRACKMACRO
1907 ? buf->usermacros.count(t.cs()) != 0
1908 : buf->getMacro(t.cs(), false) != 0));
1910 latexkeys const * l = in_word_set(t.cs());
1911 if (l && !is_user_macro) {
1912 if (l->inset == "big") {
1914 docstring const delim = getToken().asInput();
1915 if (InsetMathBig::isBigInsetDelim(delim))
1916 cell->push_back(MathAtom(
1917 new InsetMathBig(t.cs(), delim)));
1919 cell->push_back(createInsetMath(t.cs(), buf));
1924 else if (l->inset == "font") {
1925 cell->push_back(createInsetMath(t.cs(), buf));
1926 parse(cell->back().nucleus()->cell(0),
1927 FLAG_ITEM, asMode(mode, l->extra));
1930 else if (l->inset == "oldfont") {
1931 cell->push_back(createInsetMath(t.cs(), buf));
1932 parse(cell->back().nucleus()->cell(0),
1933 flags | FLAG_ALIGN, asMode(mode, l->extra));
1934 if (prevToken().cat() != catAlign &&
1935 prevToken().cs() != "\\")
1940 else if (l->inset == "style") {
1941 cell->push_back(createInsetMath(t.cs(), buf));
1942 parse(cell->back().nucleus()->cell(0),
1943 flags | FLAG_ALIGN, mode);
1944 if (prevToken().cat() != catAlign &&
1945 prevToken().cs() != "\\")
1951 MathAtom at = createInsetMath(t.cs(), buf);
1952 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1953 parse(at.nucleus()->cell(i),
1954 FLAG_ITEM, asMode(mode, l->extra));
1955 cell->push_back(at);
1960 bool is_unicode_symbol = false;
1961 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
1963 docstring cmd = prevToken().asInput();
1964 CatCode cat = nextToken().cat();
1965 if (cat == catBegin) {
1967 while (good() && (count || cat != catEnd)) {
1968 cat = nextToken().cat();
1969 cmd += getToken().asInput();
1971 if (cat == catBegin)
1973 else if (cat == catEnd)
1979 char_type c = Encodings::fromLaTeXCommand(cmd,
1980 Encodings::MATH_CMD | Encodings::TEXT_CMD,
1981 is_combining, termination);
1983 if (cat == catLetter)
1985 cmd += getToken().asInput();
1987 if (cat == catLetter)
1989 c = Encodings::fromLaTeXCommand(cmd,
1990 Encodings::MATH_CMD | Encodings::TEXT_CMD,
1991 is_combining, termination);
1995 if (nextToken().cat() == catBegin) {
1997 if (nextToken().cat() == catEnd) {
2003 while (nextToken().cat() == catSpace) {
2009 is_unicode_symbol = true;
2010 cell->push_back(MathAtom(new InsetMathChar(c)));
2012 while (num_tokens--)
2016 if (!is_unicode_symbol) {
2017 MathAtom at = is_user_macro ?
2018 MathAtom(new MathMacro(buf, t.cs()))
2019 : createInsetMath(t.cs(), buf);
2020 InsetMath::mode_type m = mode;
2021 //if (m == InsetMath::UNDECIDED_MODE)
2022 //lyxerr << "default creation: m1: " << m << endl;
2023 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
2024 m = at->currentMode();
2025 //lyxerr << "default creation: m2: " << m << endl;
2026 InsetMath::idx_type start = 0;
2027 // this fails on \bigg[...\bigg]
2029 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
2030 //if (!opt.empty()) {
2032 // at.nucleus()->cell(0) = opt;
2034 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
2035 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
2038 cell->push_back(at);
2044 if (flags & FLAG_LEAVE) {
2045 flags &= ~FLAG_LEAVE;
2054 } // anonymous namespace
2057 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
2059 return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
2060 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
2064 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
2066 return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
2067 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
2071 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
2074 return Parser(str, f, buf).parse(t);
2078 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
2081 return Parser(lex, f, buf).parse(t);
2085 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
2088 return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
2089 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
2095 fill(theCatcode, theCatcode + 128, catOther);
2096 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
2097 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
2099 theCatcode[int('\\')] = catEscape;
2100 theCatcode[int('{')] = catBegin;
2101 theCatcode[int('}')] = catEnd;
2102 theCatcode[int('$')] = catMath;
2103 theCatcode[int('&')] = catAlign;
2104 theCatcode[int('\n')] = catNewline;
2105 theCatcode[int('#')] = catParameter;
2106 theCatcode[int('^')] = catSuper;
2107 theCatcode[int('_')] = catSub;
2108 theCatcode[int(0x7f)] = catIgnore;
2109 theCatcode[int(' ')] = catSpace;
2110 theCatcode[int('\t')] = catSpace;
2111 theCatcode[int('\r')] = catNewline;
2112 theCatcode[int('~')] = catActive;
2113 theCatcode[int('%')] = catComment;