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 // For [bB]matrix, [vV]matrix, and pmatrix we can check the suffix only
252 return name == "array" || name == "cases" || name == "aligned"
253 || name == "alignedat" || name == "gathered" || name == "split"
254 || name == "subarray" || name == "tabular" || name == "matrix"
255 || name == "smallmatrix" || name.substr(1) == "matrix";
259 // These are TeX's catcodes
261 catEscape, // 0 backslash
271 catSpace, // 10 space
272 catLetter, // 11 a-zA-Z
273 catOther, // 12 none of the above
276 catInvalid // 15 <delete>
279 CatCode theCatcode[128];
282 inline CatCode catcode(char_type c)
284 /* The only characters that are not catOther lie in the pure ASCII
285 * range. Therefore theCatcode has only 128 entries.
286 * TeX itself deals with 8bit characters, so if needed this table
287 * could be enlarged to 256 entries.
288 * Any larger value does not make sense, since the fact that we use
289 * unicode internally does not change Knuth's TeX engine.
290 * Apart from that a table for the full 21bit UCS4 range would waste
291 * too much memory. */
295 return theCatcode[c];
300 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
301 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
302 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
303 FLAG_END = 1 << 3, // next \\end ends the parsing process
304 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
305 FLAG_TEXTMODE = 1 << 5, // we are in a box
306 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
307 FLAG_LEAVE = 1 << 7, // leave the loop at the end
308 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
309 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
310 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
311 FLAG_OPTION = 1 << 11, // read [...] style option
312 FLAG_BRACED = 1 << 12 // read {...} style argument
317 // Helper class for parsing
323 Token() : cs_(), char_(0), cat_(catIgnore) {}
325 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
327 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
330 docstring const & cs() const { return cs_; }
332 CatCode cat() const { return cat_; }
334 char_type character() const { return char_; }
336 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
338 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
350 ostream & operator<<(ostream & os, Token const & t)
353 docstring const & cs = t.cs();
354 // FIXME: For some strange reason, the stream operator instanciate
355 // a new Token before outputting the contents of t.cs().
356 // Because of this the line
358 // below becomes recursive.
359 // In order to avoid that we return early:
362 os << '\\' << to_utf8(cs);
364 else if (t.cat() == catLetter)
367 os << '[' << t.character() << ',' << t.cat() << ']';
375 typedef InsetMath::mode_type mode_type;
377 typedef Parse::flags parse_mode;
380 Parser(Lexer & lex, parse_mode mode, Buffer * buf);
381 /// Only use this for reading from .lyx file format, for the reason
382 /// see Parser::tokenize(istream &).
383 Parser(istream & is, parse_mode mode, Buffer * buf);
385 Parser(docstring const & str, parse_mode mode, Buffer * buf);
388 bool parse(MathAtom & at);
390 bool parse(MathData & array, unsigned flags, mode_type mode);
392 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
395 MathData parse(unsigned flags, mode_type mode);
397 int lineno() const { return lineno_; }
403 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
404 /// get arg delimited by 'left' and 'right'
405 docstring getArg(char_type left, char_type right);
409 void error(string const & msg);
410 void error(docstring const & msg) { error(to_utf8(msg)); }
411 /// dump contents to screen
413 /// Only use this for reading from .lyx file format (see
414 /// implementation for reason)
415 void tokenize(istream & is);
417 void tokenize(docstring const & s);
419 void skipSpaceTokens(idocstream & is, char_type c);
421 void push_back(Token const & t);
425 Token const & prevToken() const;
427 Token const & nextToken() const;
429 Token const & getToken();
430 /// skips spaces if any
433 void lex(docstring const & s);
437 docstring parse_verbatim_item();
439 docstring parse_verbatim_option();
444 vector<Token> tokens_;
447 /// Stack of active environments
448 vector<docstring> environments_;
458 Parser::Parser(Lexer & lexer, parse_mode mode, Buffer * buf)
459 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true),
462 tokenize(lexer.getStream());
467 Parser::Parser(istream & is, parse_mode mode, Buffer * buf)
468 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
474 Parser::Parser(docstring const & str, parse_mode mode, Buffer * buf)
475 : lineno_(0), pos_(0), mode_(mode), success_(true), buffer_(buf)
481 void Parser::push_back(Token const & t)
483 tokens_.push_back(t);
487 void Parser::pop_back()
493 Token const & Parser::prevToken() const
495 static const Token dummy;
496 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
500 Token const & Parser::nextToken() const
502 static const Token dummy;
503 return good() ? tokens_[pos_] : dummy;
507 Token const & Parser::getToken()
509 static const Token dummy;
510 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
511 return good() ? tokens_[pos_++] : dummy;
515 void Parser::skipSpaces()
517 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
522 void Parser::putback()
528 bool Parser::good() const
530 return pos_ < tokens_.size();
534 char_type Parser::getChar()
537 error("The input stream is not well...");
541 return tokens_[pos_++].character();
545 docstring Parser::getArg(char_type left, char_type right)
550 char_type c = getChar();
555 while ((c = getChar()) != right && good())
562 void Parser::skipSpaceTokens(idocstream & is, char_type c)
564 // skip trailing spaces
565 while (catcode(c) == catSpace || catcode(c) == catNewline)
568 //lyxerr << "putting back: " << c << endl;
573 void Parser::tokenize(istream & is)
575 // eat everything up to the next \end_inset or end of stream
576 // and store it in s for further tokenization
581 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
582 s = s.substr(0, s.size() - 10);
586 // Remove the space after \end_inset
587 if (is.get(c) && c != ' ')
591 tokenize(from_utf8(s));
595 void Parser::tokenize(docstring const & buffer)
597 idocstringstream is(mode_ & Parse::VERBATIM
598 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
599 : buffer, ios::in | ios::binary);
603 //lyxerr << "reading c: " << c << endl;
605 switch (catcode(c)) {
609 if (catcode(c) == catNewline)
610 ; //push_back(Token("par"));
612 push_back(Token('\n', catNewline));
620 while (is.get(c) && catcode(c) != catNewline)
630 error("unexpected end of input");
635 if (catcode(c) == catLetter) {
637 while (is.get(c) && catcode(c) == catLetter)
639 skipSpaceTokens(is, c);
648 push_back(Token(c, catcode(c)));
650 skipSpaceTokens(is, c);
655 if (!(mode_ & Parse::QUIET))
656 lyxerr << "ignoring a char: " << int(c) << endl;
661 push_back(Token(c, catcode(c)));
671 void Parser::dump() const
673 lyxerr << "\nTokens: ";
674 for (unsigned i = 0; i < tokens_.size(); ++i) {
677 lyxerr << tokens_[i];
679 lyxerr << " pos: " << pos_ << endl;
683 void Parser::error(string const & msg)
686 if (!(mode_ & Parse::QUIET)) {
687 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
694 bool Parser::parse(MathAtom & at)
697 MathData ar(buffer_);
698 parse(ar, false, InsetMath::UNDECIDED_MODE);
699 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
700 if (!(mode_ & Parse::QUIET))
701 lyxerr << "unusual contents found: " << ar << endl;
702 at = MathAtom(new InsetMathPar(buffer_, ar));
703 //if (at->nargs() > 0)
704 // at.nucleus()->cell(0) = ar;
706 // lyxerr << "unusual contents found: " << ar << endl;
714 docstring Parser::parse_verbatim_option()
718 if (nextToken().character() == '[') {
719 Token t = getToken();
720 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
721 if (t.cat() == catBegin) {
723 res += '{' + parse_verbatim_item() + '}';
732 docstring Parser::parse_verbatim_item()
736 if (nextToken().cat() == catBegin) {
737 Token t = getToken();
738 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
739 if (t.cat() == catBegin) {
741 res += '{' + parse_verbatim_item() + '}';
751 MathData Parser::parse(unsigned flags, mode_type mode)
753 MathData ar(buffer_);
754 parse(ar, flags, mode);
759 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
761 InsetMathGrid grid(buffer_, 1, 1);
762 parse1(grid, flags, mode, false);
763 array = grid.cell(0);
768 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
771 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
775 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
776 const mode_type mode, const bool numbered)
779 InsetMathGrid::row_type cellrow = 0;
780 InsetMathGrid::col_type cellcol = 0;
781 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
782 Buffer * buf = buffer_;
784 if (grid.asHullInset())
785 grid.asHullInset()->numbered(cellrow, numbered);
788 //lyxerr << " flags: " << flags << endl;
789 //lyxerr << " mode: " << mode << endl;
790 //lyxerr << "grid: " << grid << endl;
793 Token const & t = getToken();
796 lyxerr << "t: " << t << " flags: " << flags << endl;
797 lyxerr << "mode: " << mode << endl;
802 if (flags & FLAG_ITEM) {
804 if (t.cat() == catBegin) {
805 // skip the brace and collect everything to the next matching
807 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
811 // handle only this single token, leave the loop if done
816 if (flags & FLAG_BRACED) {
817 if (t.cat() == catSpace)
820 if (t.cat() != catBegin) {
821 error("opening brace expected");
825 // skip the brace and collect everything to the next matching
827 flags = FLAG_BRACE_LAST;
831 if (flags & FLAG_OPTION) {
832 if (t.cat() == catOther && t.character() == '[') {
834 parse(ar, FLAG_BRACK_LAST, mode);
837 // no option found, put back token and we are done
846 if (t.cat() == catMath) {
847 if (mode != InsetMath::MATH_MODE) {
848 // we are inside some text mode thingy, so opening new math is allowed
849 Token const & n = getToken();
850 if (n.cat() == catMath) {
851 // TeX's $$...$$ syntax for displayed math
852 if (mode == InsetMath::UNDECIDED_MODE) {
853 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
854 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
855 getToken(); // skip the second '$' token
857 // This is not an outer hull and display math is
858 // not allowed inside text mode environments.
859 error("bad math environment");
863 // simple $...$ stuff
865 if (mode == InsetMath::UNDECIDED_MODE) {
866 cell->push_back(MathAtom(new InsetMathHull(buf, hullSimple)));
867 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
869 // Don't create nested math hulls (bug #5392)
870 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
871 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
876 else if (flags & FLAG_SIMPLE) {
877 // this is the end of the formula
882 Token const & n = getToken();
883 if (n.cat() == catMath) {
884 error("something strange in the parser");
887 // This is inline math ($...$), but the parser thinks we are
888 // already in math mode and latex would issue an error, unless we
889 // are inside a text mode user macro. We have no way to tell, so
890 // let's play safe by using \ensuremath, as it will work in any case.
892 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
893 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
898 else if (t.cat() == catLetter)
899 cell->push_back(MathAtom(new InsetMathChar(t.character())));
901 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
902 if (cell->empty() || cell->back()->getChar() != ' ')
903 cell->push_back(MathAtom(new InsetMathChar(t.character())));
906 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
907 if (cell->empty() || cell->back()->getChar() != ' ')
908 cell->push_back(MathAtom(new InsetMathChar(' ')));
911 else if (t.cat() == catParameter) {
912 Token const & n = getToken();
913 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
916 else if (t.cat() == catActive)
917 cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), "")));
919 else if (t.cat() == catBegin) {
921 parse(ar, FLAG_BRACE_LAST, mode);
922 // do not create a BraceInset if they were written by LyX
923 // this helps to keep the annoyance of "a choose b" to a minimum
924 if (ar.size() == 1 && ar[0]->extraBraces())
927 cell->push_back(MathAtom(new InsetMathBrace(ar)));
930 else if (t.cat() == catEnd) {
931 if (flags & FLAG_BRACE_LAST)
933 error("found '}' unexpectedly");
934 //LASSERT(false, /**/);
935 //add(cell, '}', LM_TC_TEX);
938 else if (t.cat() == catAlign) {
939 //lyxerr << " column now " << (cellcol + 1)
940 // << " max: " << grid.ncols() << endl;
941 if (flags & FLAG_ALIGN)
943 if (addCol(grid, cellcol))
944 cell = &grid.cell(grid.index(cellrow, cellcol));
947 else if (t.cat() == catSuper || t.cat() == catSub) {
948 bool up = (t.cat() == catSuper);
949 // we need no new script inset if the last thing was a scriptinset,
950 // which has that script already not the same script already
952 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
953 else if (cell->back()->asScriptInset() &&
954 !cell->back()->asScriptInset()->has(up))
955 cell->back().nucleus()->asScriptInset()->ensure(up);
956 else if (cell->back()->asScriptInset())
957 cell->push_back(MathAtom(new InsetMathScript(buf, up)));
959 cell->back() = MathAtom(new InsetMathScript(buf, cell->back(), up));
960 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
961 // special handling of {}-bases
962 // Here we could remove the brace inset for things
963 // like {a'}^2 and add the braces back in
964 // InsetMathScript::write().
965 // We do not do it, since it is not possible to detect
966 // reliably whether the braces are needed because the
967 // nucleus contains more than one symbol, or whether
968 // they are needed for unknown commands like \xx{a}_0
969 // or \yy{a}{b}_0. This was done in revision 14819
970 // in an unreliable way. See this thread
971 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
973 // However, we remove empty braces because they look
974 // ugly on screen and we are sure that they were added
975 // by the write() method (and will be re-added on save).
976 if (p->nuc().size() == 1 &&
977 p->nuc().back()->asBraceInset() &&
978 p->nuc().back()->asBraceInset()->cell(0).empty())
981 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
988 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
989 //lyxerr << "finished reading option" << endl;
993 else if (t.cat() == catOther) {
994 char_type c = t.character();
995 if (isAsciiOrMathAlpha(c)
996 || mode_ & Parse::VERBATIM
997 || !(mode_ & Parse::USETEXT)
998 || mode == InsetMath::TEXT_MODE) {
999 cell->push_back(MathAtom(new InsetMathChar(c)));
1001 MathAtom at = createInsetMath("text", buf);
1002 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1003 while (nextToken().cat() == catOther
1004 && !isAsciiOrMathAlpha(nextToken().character())) {
1005 c = getToken().character();
1006 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1008 cell->push_back(at);
1012 else if (t.cat() == catComment) {
1015 Token const & t = getToken();
1016 if (t.cat() == catNewline)
1020 cell->push_back(MathAtom(new InsetMathComment(buf, s)));
1025 // control sequences
1028 else if (t.cs() == "lyxlock") {
1030 cell->back().nucleus()->lock(true);
1033 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
1035 if (t.cs() == "global")
1039 docstring name = getToken().cs();
1044 while (good() && nextToken().cat() != catBegin) {
1045 pars += getToken().cs();
1052 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1054 // is a version for display attached?
1057 if (nextToken().cat() == catBegin)
1058 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1060 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1061 name, nargs, 0, MacroTypeDef,
1062 vector<MathData>(), def, display)));
1064 if (buf && (mode_ & Parse::TRACKMACRO))
1065 buf->usermacros.insert(name);
1068 else if (t.cs() == "newcommand" ||
1069 t.cs() == "renewcommand" ||
1070 t.cs() == "newlyxcommand") {
1072 if (getToken().cat() != catBegin) {
1073 error("'{' in \\newcommand expected (1) ");
1076 docstring name = getToken().cs();
1077 if (getToken().cat() != catEnd) {
1078 error("'}' in \\newcommand expected");
1083 docstring const arg = getArg('[', ']');
1086 nargs = convert<int>(arg);
1088 // optional argument given?
1091 vector<MathData> optionalValues;
1092 while (nextToken().character() == '[') {
1094 optionalValues.push_back(MathData());
1095 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1100 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1102 // is a version for display attached?
1105 if (nextToken().cat() == catBegin)
1106 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1108 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1109 name, nargs, optionals, MacroTypeNewcommand,
1110 optionalValues, def, display)));
1112 if (buf && (mode_ & Parse::TRACKMACRO))
1113 buf->usermacros.insert(name);
1116 else if (t.cs() == "newcommandx" ||
1117 t.cs() == "renewcommandx") {
1118 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1121 if (nextToken().cat() == catBegin) {
1123 name = getToken().cs();
1124 if (getToken().cat() != catEnd) {
1125 error("'}' in \\newcommandx expected");
1129 name = getToken().cs();
1132 docstring const arg = getArg('[', ']');
1134 error("[num] in \\newcommandx expected");
1137 int nargs = convert<int>(arg);
1141 vector<MathData> optionalValues;
1142 if (nextToken().character() == '[') {
1146 // handle 'opt=value' options, separated by ','.
1148 while (nextToken().character() != ']' && good()) {
1149 if (nextToken().character() >= '1'
1150 && nextToken().character() <= '9') {
1151 // optional value -> get parameter number
1152 int n = getChar() - '0';
1154 error("Arity of \\newcommandx too low "
1155 "for given optional parameter.");
1160 if (getToken().character() != '=') {
1161 error("'=' and optional parameter value "
1162 "expected for \\newcommandx");
1167 int optNum = max(size_t(n), optionalValues.size());
1168 optionalValues.resize(optNum);
1169 optionalValues[n - 1].clear();
1170 while (nextToken().character() != ']'
1171 && nextToken().character() != ',') {
1173 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1174 optionalValues[n - 1].append(data);
1176 optionals = max(n, optionals);
1177 } else if (nextToken().cat() == catLetter) {
1178 // we in fact ignore every non-optional
1183 while (nextToken().cat() == catLetter)
1189 if (nextToken().character() == '=') {
1191 while (nextToken().character() != ']'
1192 && nextToken().character() != ',')
1193 parse(value, FLAG_ITEM,
1194 InsetMath::UNDECIDED_MODE);
1197 error("option for \\newcommandx expected");
1203 if (nextToken().character() == ',') {
1206 } else if (nextToken().character() != ']') {
1207 error("Expecting ',' or ']' in options "
1208 "of \\newcommandx");
1221 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1223 // is a version for display attached?
1226 if (nextToken().cat() == catBegin)
1227 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1229 cell->push_back(MathAtom(new MathMacroTemplate(buf,
1230 name, nargs, optionals, MacroTypeNewcommandx,
1231 optionalValues, def, display)));
1233 if (buf && (mode_ & Parse::TRACKMACRO))
1234 buf->usermacros.insert(name);
1237 else if (t.cs() == "(") {
1238 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1239 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE2, InsetMath::MATH_MODE);
1242 else if (t.cs() == "[") {
1243 if (mode != InsetMath::UNDECIDED_MODE) {
1244 error("bad math environment");
1247 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1248 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1251 else if (t.cs() == "protect")
1252 // ignore \\protect, will hopefully be re-added during output
1255 else if (t.cs() == "end") {
1256 if (flags & FLAG_END) {
1257 // eat environment name
1258 docstring const name = getArg('{', '}');
1259 if (environments_.empty())
1260 error("'found \\end{" + name +
1261 "}' without matching '\\begin{" +
1263 else if (name != environments_.back())
1264 error("'\\end{" + name +
1265 "}' does not match '\\begin{" +
1266 environments_.back() + "}'");
1268 environments_.pop_back();
1269 // Delete empty last row in matrix
1271 // If you abuse InsetMathGrid for
1272 // non-matrix like structures you
1273 // probably need to refine this test.
1274 // Right now we only have to test for
1275 // single line hull insets.
1276 if (grid.nrows() > 1 && innerHull(name))
1277 delEmptyLastRow(grid);
1281 error("found 'end' unexpectedly");
1284 else if (t.cs() == ")") {
1285 if (flags & FLAG_SIMPLE2)
1287 error("found '\\)' unexpectedly");
1290 else if (t.cs() == "]") {
1291 if (flags & FLAG_EQUATION)
1293 error("found '\\]' unexpectedly");
1296 else if (t.cs() == "\\") {
1297 if (flags & FLAG_ALIGN)
1300 if (nextToken().asInput() == "*") {
1302 added = addRow(grid, cellrow, docstring(), false);
1304 added = addRow(grid, cellrow, getArg('[', ']'));
1306 error("missing token after \\\\");
1309 if (grid.asHullInset())
1310 grid.asHullInset()->numbered(
1312 cell = &grid.cell(grid.index(cellrow,
1318 else if (t.cs() == "multicolumn") {
1319 // extract column count and insert dummy cells
1321 parse(count, FLAG_ITEM, mode);
1323 if (!extractNumber(count, cols)) {
1325 lyxerr << " can't extract number of cells from " << count << endl;
1327 // resize the table if necessary
1328 for (int i = 0; i < cols; ++i) {
1329 if (addCol(grid, cellcol)) {
1330 cell = &grid.cell(grid.index(
1332 // mark this as dummy
1333 grid.cellinfo(grid.index(
1334 cellrow, cellcol)).dummy_ = true;
1337 // the last cell is the real thing, not a dummy
1338 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1340 // read special alignment
1342 parse(align, FLAG_ITEM, mode);
1343 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1345 // parse the remaining contents into the "real" cell
1346 parse(*cell, FLAG_ITEM, mode);
1350 else if (t.cs() == "limits" || t.cs() == "nolimits") {
1351 CatCode const cat = nextToken().cat();
1352 if (cat == catSuper || cat == catSub)
1353 limits = t.cs() == "limits" ? 1 : -1;
1355 MathAtom at = createInsetMath(t.cs(), buf);
1356 cell->push_back(at);
1360 else if (t.cs() == "nonumber") {
1361 if (grid.asHullInset())
1362 grid.asHullInset()->numbered(cellrow, false);
1365 else if (t.cs() == "number") {
1366 if (grid.asHullInset())
1367 grid.asHullInset()->numbered(cellrow, true);
1370 else if (t.cs() == "hline") {
1371 grid.rowinfo(cellrow).lines_ ++;
1374 else if (t.cs() == "sqrt") {
1376 parse(ar, FLAG_OPTION, mode);
1378 cell->push_back(MathAtom(new InsetMathRoot(buf)));
1379 cell->back().nucleus()->cell(0) = ar;
1380 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1382 cell->push_back(MathAtom(new InsetMathSqrt(buf)));
1383 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1387 else if (t.cs() == "unit") {
1388 // Allowed formats \unit[val]{unit}
1390 parse(ar, FLAG_OPTION, mode);
1392 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT)));
1393 cell->back().nucleus()->cell(0) = ar;
1394 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1396 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNIT, 1)));
1397 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1401 else if (t.cs() == "unitfrac") {
1402 // Here allowed formats are \unitfrac[val]{num}{denom}
1404 parse(ar, FLAG_OPTION, mode);
1406 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC, 3)));
1407 cell->back().nucleus()->cell(2) = ar;
1409 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::UNITFRAC)));
1411 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1412 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1415 else if (t.cs() == "cfrac") {
1416 // allowed formats are \cfrac[pos]{num}{denom}
1417 docstring const arg = getArg('[', ']');
1418 //lyxerr << "got so far: '" << arg << "'" << endl;
1420 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACLEFT)));
1421 else if (arg == "r")
1422 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRACRIGHT)));
1423 else if (arg.empty() || arg == "c")
1424 cell->push_back(MathAtom(new InsetMathFrac(buf, InsetMathFrac::CFRAC)));
1426 error("found invalid optional argument");
1429 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1430 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1433 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1434 cell->push_back(createInsetMath(t.cs(), buf));
1435 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1436 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1439 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1440 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1441 cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
1442 docstring const opt = parse_verbatim_option();
1443 docstring const ref = parse_verbatim_item();
1445 cell->back().nucleus()->cell(1).push_back(
1446 MathAtom(new InsetMathString(opt)));
1448 cell->back().nucleus()->cell(0).push_back(
1449 MathAtom(new InsetMathString(ref)));
1452 else if (t.cs() == "left") {
1454 Token const & tl = getToken();
1455 // \| and \Vert are equivalent, and InsetMathDelim
1457 // FIXME: fix this in InsetMathDelim itself!
1458 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1460 parse(ar, FLAG_RIGHT, mode);
1464 Token const & tr = getToken();
1465 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1466 cell->push_back(MathAtom(new InsetMathDelim(buf, l, r, ar)));
1469 else if (t.cs() == "right") {
1470 if (flags & FLAG_RIGHT)
1472 //lyxerr << "got so far: '" << cell << "'" << endl;
1473 error("Unmatched right delimiter");
1477 else if (t.cs() == "begin") {
1478 docstring const name = getArg('{', '}');
1479 environments_.push_back(name);
1481 if (name == "array" || name == "subarray") {
1482 docstring const valign = parse_verbatim_option() + 'c';
1483 docstring const halign = parse_verbatim_item();
1484 cell->push_back(MathAtom(new InsetMathArray(buf, name,
1485 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1486 parse2(cell->back(), FLAG_END, mode, false);
1489 else if (name == "tabular") {
1490 docstring const valign = parse_verbatim_option() + 'c';
1491 docstring const halign = parse_verbatim_item();
1492 cell->push_back(MathAtom(new InsetMathTabular(buf, name,
1493 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1494 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1497 else if (name == "split" || name == "cases") {
1498 cell->push_back(createInsetMath(name, buf));
1499 parse2(cell->back(), FLAG_END, mode, false);
1502 else if (name == "alignedat") {
1503 docstring const valign = parse_verbatim_option() + 'c';
1504 // ignore this for a while
1506 cell->push_back(MathAtom(new InsetMathSplit(buf, name, (char)valign[0])));
1507 parse2(cell->back(), FLAG_END, mode, false);
1510 else if (name == "math") {
1511 cell->push_back(MathAtom(new InsetMathEnsureMath(buf)));
1512 parse(cell->back().nucleus()->cell(0), FLAG_END, InsetMath::MATH_MODE);
1515 else if (name == "equation" || name == "equation*"
1516 || name == "displaymath") {
1517 if (mode != InsetMath::UNDECIDED_MODE) {
1518 error("bad math environment");
1521 cell->push_back(MathAtom(new InsetMathHull(buf, hullEquation)));
1522 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1525 else if (name == "eqnarray" || name == "eqnarray*") {
1526 if (mode != InsetMath::UNDECIDED_MODE) {
1527 error("bad math environment");
1530 cell->push_back(MathAtom(new InsetMathHull(buf, hullEqnArray)));
1531 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1534 else if (name == "align" || name == "align*") {
1535 if (mode != InsetMath::UNDECIDED_MODE) {
1536 error("bad math environment");
1539 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlign)));
1540 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1543 else if (name == "flalign" || name == "flalign*") {
1544 if (mode != InsetMath::UNDECIDED_MODE) {
1545 error("bad math environment");
1548 cell->push_back(MathAtom(new InsetMathHull(buf, hullFlAlign)));
1549 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1552 else if (name == "alignat" || name == "alignat*") {
1553 if (mode != InsetMath::UNDECIDED_MODE) {
1554 error("bad math environment");
1557 // ignore this for a while
1559 cell->push_back(MathAtom(new InsetMathHull(buf, hullAlignAt)));
1560 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1563 else if (name == "xalignat" || name == "xalignat*") {
1564 if (mode != InsetMath::UNDECIDED_MODE) {
1565 error("bad math environment");
1568 // ignore this for a while
1570 cell->push_back(MathAtom(new InsetMathHull(buf, hullXAlignAt)));
1571 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1574 else if (name == "xxalignat") {
1575 if (mode != InsetMath::UNDECIDED_MODE) {
1576 error("bad math environment");
1579 // ignore this for a while
1581 cell->push_back(MathAtom(new InsetMathHull(buf, hullXXAlignAt)));
1582 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1585 else if (name == "multline" || name == "multline*") {
1586 if (mode != InsetMath::UNDECIDED_MODE) {
1587 error("bad math environment");
1590 cell->push_back(MathAtom(new InsetMathHull(buf, hullMultline)));
1591 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1594 else if (name == "gather" || name == "gather*") {
1595 if (mode != InsetMath::UNDECIDED_MODE) {
1596 error("bad math environment");
1599 cell->push_back(MathAtom(new InsetMathHull(buf, hullGather)));
1600 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1603 else if (latexkeys const * l = in_word_set(name)) {
1604 if (l->inset == "matrix") {
1605 cell->push_back(createInsetMath(name, buf));
1606 parse2(cell->back(), FLAG_END, mode, false);
1607 } else if (l->inset == "split") {
1608 docstring const valign = parse_verbatim_option() + 'c';
1609 cell->push_back(MathAtom(
1610 new InsetMathSplit(buf, name, (char)valign[0])));
1611 parse2(cell->back(), FLAG_END, mode, false);
1614 if (!(mode_ & Parse::QUIET)) {
1616 lyxerr << "found math environment `"
1618 << "' in symbols file with unsupported inset `"
1619 << to_utf8(l->inset)
1622 // create generic environment inset
1623 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1624 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1630 if (!(mode_ & Parse::QUIET)) {
1632 lyxerr << "found unknown math environment '"
1633 << to_utf8(name) << "'" << endl;
1635 // create generic environment inset
1636 cell->push_back(MathAtom(new InsetMathEnv(buf, name)));
1637 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1641 else if (t.cs() == "kern") {
1646 Token const & t = getToken();
1650 while (num_tokens--)
1655 if (isValidLength(to_utf8(s)))
1659 cell->push_back(MathAtom(new InsetMathKern));
1661 cell->push_back(MathAtom(new InsetMathKern(s)));
1664 else if (t.cs() == "label") {
1665 // FIXME: This is swallowed in inline formulas
1666 docstring label = parse_verbatim_item();
1669 if (grid.asHullInset()) {
1670 grid.asHullInset()->label(cellrow, label);
1672 cell->push_back(createInsetMath(t.cs(), buf));
1673 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1677 else if (t.cs() == "choose" || t.cs() == "over"
1678 || t.cs() == "atop" || t.cs() == "brace"
1679 || t.cs() == "brack") {
1680 MathAtom at = createInsetMath(t.cs(), buf);
1681 at.nucleus()->cell(0) = *cell;
1683 parse(at.nucleus()->cell(1), flags, mode);
1684 cell->push_back(at);
1688 else if (t.cs() == "color") {
1689 docstring const color = parse_verbatim_item();
1690 cell->push_back(MathAtom(new InsetMathColor(buf, true, color)));
1691 parse(cell->back().nucleus()->cell(0), flags, mode);
1695 else if (t.cs() == "textcolor") {
1696 docstring const color = parse_verbatim_item();
1697 cell->push_back(MathAtom(new InsetMathColor(buf, false, color)));
1698 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1701 else if (t.cs() == "normalcolor") {
1702 cell->push_back(createInsetMath(t.cs(), buf));
1703 parse(cell->back().nucleus()->cell(0), flags, mode);
1707 else if (t.cs() == "substack") {
1708 cell->push_back(createInsetMath(t.cs(), buf));
1709 parse2(cell->back(), FLAG_ITEM, mode, false);
1710 // Delete empty last row if present
1711 InsetMathGrid & subgrid =
1712 *(cell->back().nucleus()->asGridInset());
1713 if (subgrid.nrows() > 1)
1714 delEmptyLastRow(subgrid);
1717 else if (t.cs() == "xymatrix") {
1718 odocstringstream os;
1719 while (good() && nextToken().cat() != catBegin)
1720 os << getToken().asInput();
1721 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1722 parse2(cell->back(), FLAG_ITEM, mode, false);
1723 // Delete empty last row if present
1724 InsetMathGrid & subgrid =
1725 *(cell->back().nucleus()->asGridInset());
1726 if (subgrid.nrows() > 1)
1727 delEmptyLastRow(subgrid);
1730 else if (t.cs() == "Diagram") {
1731 odocstringstream os;
1732 while (good() && nextToken().cat() != catBegin)
1733 os << getToken().asInput();
1734 cell->push_back(createInsetMath(t.cs() + os.str(), buf));
1735 parse2(cell->back(), FLAG_ITEM, mode, false);
1738 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1739 cell->push_back(createInsetMath(t.cs(), buf));
1740 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1741 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1742 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1745 else if (t.cs() == "tag") {
1746 if (nextToken().character() == '*') {
1748 cell->push_back(createInsetMath(t.cs() + '*', buf));
1750 cell->push_back(createInsetMath(t.cs(), buf));
1751 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1754 else if (t.cs() == "hspace") {
1755 bool const prot = nextToken().character() == '*';
1758 docstring const name = t.cs();
1759 docstring const arg = parse_verbatim_item();
1761 if (prot && arg == "\\fill")
1762 cell->push_back(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
1763 else if (isValidLength(to_utf8(arg), &length))
1764 cell->push_back(MathAtom(new InsetMathSpace(length, prot)));
1766 // Since the Length class cannot use length variables
1767 // we must not create an InsetMathSpace.
1768 cell->push_back(MathAtom(new MathMacro(buf, name)));
1770 mathed_parse_cell(ar, '{' + arg + '}', mode_);
1776 else if (t.cs() == "infer") {
1778 parse(ar, FLAG_OPTION, mode);
1779 cell->push_back(createInsetMath(t.cs(), buf));
1780 parse2(cell->back(), FLAG_ITEM, mode, false);
1784 else if (1 && t.cs() == "ar") {
1785 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1786 // try to read target
1787 parse(p->cell(0), FLAG_OTPTION, mode);
1788 // try to read label
1789 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1790 p->up_ = nextToken().cat() == catSuper;
1792 parse(p->cell(1), FLAG_ITEM, mode);
1793 //lyxerr << "read label: " << p->cell(1) << endl;
1796 cell->push_back(MathAtom(p.release()));
1797 //lyxerr << "read cell: " << cell << endl;
1801 else if (t.cs() == "lyxmathsym") {
1803 if (getToken().cat() != catBegin) {
1804 error("'{' expected in \\" + t.cs());
1809 CatCode cat = nextToken().cat();
1810 while (good() && (count || cat != catEnd)) {
1811 if (cat == catBegin)
1813 else if (cat == catEnd)
1815 cmd += getToken().asInput();
1816 cat = nextToken().cat();
1818 if (getToken().cat() != catEnd) {
1819 error("'}' expected in \\" + t.cs());
1824 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1825 for (size_t i = 0; i < cmd.size(); ++i)
1826 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1828 char_type c = rem[0];
1829 cell->push_back(MathAtom(new InsetMathChar(c)));
1830 cmd = rem.substr(1);
1834 } while (cmd.size());
1837 else if (t.cs().size()) {
1838 bool const no_mhchem =
1839 (t.cs() == "ce" || t.cs() == "cf")
1840 && buf && buf->params().use_mhchem ==
1841 BufferParams::package_off;
1843 bool const is_user_macro = no_mhchem ||
1844 (buf && (mode_ & Parse::TRACKMACRO
1845 ? buf->usermacros.count(t.cs()) != 0
1846 : buf->getMacro(t.cs(), false) != 0));
1848 latexkeys const * l = in_word_set(t.cs());
1849 if (l && !is_user_macro) {
1850 if (l->inset == "big") {
1852 docstring const delim = getToken().asInput();
1853 if (InsetMathBig::isBigInsetDelim(delim))
1854 cell->push_back(MathAtom(
1855 new InsetMathBig(t.cs(), delim)));
1857 cell->push_back(createInsetMath(t.cs(), buf));
1862 else if (l->inset == "font") {
1863 cell->push_back(createInsetMath(t.cs(), buf));
1864 parse(cell->back().nucleus()->cell(0),
1865 FLAG_ITEM, asMode(mode, l->extra));
1868 else if (l->inset == "oldfont") {
1869 cell->push_back(createInsetMath(t.cs(), buf));
1870 parse(cell->back().nucleus()->cell(0),
1871 flags | FLAG_ALIGN, asMode(mode, l->extra));
1872 if (prevToken().cat() != catAlign &&
1873 prevToken().cs() != "\\")
1878 else if (l->inset == "style") {
1879 cell->push_back(createInsetMath(t.cs(), buf));
1880 parse(cell->back().nucleus()->cell(0),
1881 flags | FLAG_ALIGN, mode);
1882 if (prevToken().cat() != catAlign &&
1883 prevToken().cs() != "\\")
1889 MathAtom at = createInsetMath(t.cs(), buf);
1890 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1891 parse(at.nucleus()->cell(i),
1892 FLAG_ITEM, asMode(mode, l->extra));
1893 cell->push_back(at);
1898 bool is_unicode_symbol = false;
1899 if (mode == InsetMath::TEXT_MODE && !is_user_macro) {
1901 docstring cmd = prevToken().asInput();
1902 CatCode cat = nextToken().cat();
1903 if (cat == catBegin) {
1905 while (good() && (count || cat != catEnd)) {
1906 cat = nextToken().cat();
1907 cmd += getToken().asInput();
1909 if (cat == catBegin)
1911 else if (cat == catEnd)
1917 Encodings::fromLaTeXCommand(cmd, is_combining);
1919 if (cat == catLetter)
1921 cmd += getToken().asInput();
1923 if (cat == catLetter)
1925 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1928 is_unicode_symbol = true;
1929 cell->push_back(MathAtom(new InsetMathChar(c)));
1931 while (num_tokens--)
1935 if (!is_unicode_symbol) {
1936 MathAtom at = is_user_macro ?
1937 MathAtom(new MathMacro(buf, t.cs()))
1938 : createInsetMath(t.cs(), buf);
1939 InsetMath::mode_type m = mode;
1940 //if (m == InsetMath::UNDECIDED_MODE)
1941 //lyxerr << "default creation: m1: " << m << endl;
1942 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1943 m = at->currentMode();
1944 //lyxerr << "default creation: m2: " << m << endl;
1945 InsetMath::idx_type start = 0;
1946 // this fails on \bigg[...\bigg]
1948 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1951 // at.nucleus()->cell(0) = opt;
1953 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1954 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1957 cell->push_back(at);
1963 if (flags & FLAG_LEAVE) {
1964 flags &= ~FLAG_LEAVE;
1973 } // anonymous namespace
1976 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1978 return Parser(str, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1979 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1983 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1985 return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
1986 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1990 bool mathed_parse_normal(Buffer * buf, MathAtom & t, docstring const & str,
1993 return Parser(str, f, buf).parse(t);
1997 bool mathed_parse_normal(Buffer * buf, MathAtom & t, Lexer & lex,
2000 return Parser(lex, f, buf).parse(t);
2004 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str,
2007 return Parser(str, f, &grid.buffer()).parse1(grid, 0, f & Parse::TEXTMODE ?
2008 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
2014 fill(theCatcode, theCatcode + 128, catOther);
2015 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
2016 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
2018 theCatcode[int('\\')] = catEscape;
2019 theCatcode[int('{')] = catBegin;
2020 theCatcode[int('}')] = catEnd;
2021 theCatcode[int('$')] = catMath;
2022 theCatcode[int('&')] = catAlign;
2023 theCatcode[int('\n')] = catNewline;
2024 theCatcode[int('#')] = catParameter;
2025 theCatcode[int('^')] = catSuper;
2026 theCatcode[int('_')] = catSub;
2027 theCatcode[int(0x7f)] = catIgnore;
2028 theCatcode[int(' ')] = catSpace;
2029 theCatcode[int('\t')] = catSpace;
2030 theCatcode[int('\r')] = catNewline;
2031 theCatcode[int('~')] = catActive;
2032 theCatcode[int('%')] = catComment;