3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 If someone desperately needs partial "structures" (such as a few
14 cells of an array inset or similar) (s)he could uses the
15 following hack as starting point to write some macros:
20 \def\makeamptab{\catcode`\&=4\relax}
21 \def\makeampletter{\catcode`\&=11\relax}
22 \def\b{\makeampletter\expandafter\makeamptab\bi}
40 #include "MathParser.h"
42 #include "InsetMathArray.h"
43 #include "InsetMathBig.h"
44 #include "InsetMathBrace.h"
45 #include "InsetMathChar.h"
46 #include "InsetMathColor.h"
47 #include "InsetMathComment.h"
48 #include "InsetMathDelim.h"
49 #include "InsetMathEnsureMath.h"
50 #include "InsetMathEnv.h"
51 #include "InsetMathFrac.h"
52 #include "InsetMathKern.h"
53 #include "MathMacro.h"
54 #include "InsetMathPar.h"
55 #include "InsetMathRef.h"
56 #include "InsetMathRoot.h"
57 #include "InsetMathScript.h"
58 #include "InsetMathSpace.h"
59 #include "InsetMathSplit.h"
60 #include "InsetMathSqrt.h"
61 #include "InsetMathTabular.h"
62 #include "MathMacroTemplate.h"
63 #include "MathFactory.h"
64 #include "MathMacroArgument.h"
65 #include "MathSupport.h"
70 #include "support/debug.h"
71 #include "support/convert.h"
72 #include "support/docstream.h"
84 InsetMath::mode_type asMode(InsetMath::mode_type oldmode, docstring const & str)
86 //lyxerr << "handling mode: '" << str << "'" << endl;
87 if (str == "mathmode")
88 return InsetMath::MATH_MODE;
89 if (str == "textmode" || str == "forcetext")
90 return InsetMath::TEXT_MODE;
95 bool stared(docstring const & s)
97 size_t const n = s.size();
98 return n && s[n - 1] == '*';
102 docstring const repl(docstring const & oldstr, char_type const c,
103 docstring const & macro, bool textmode = false)
109 for (i = 0, j = 0; i < oldstr.size(); ++i) {
110 if (c == oldstr[i]) {
111 newstr.append(oldstr, j, i - j);
112 newstr.append(macro);
114 if (macro.size() > 2 && j < oldstr.size())
115 newstr += (textmode && oldstr[j] == ' ' ? '\\' : ' ');
123 newstr.append(oldstr, j, i - j);
128 docstring escapeSpecialChars(docstring const & str, bool textmode)
130 docstring const backslash = textmode ? from_ascii("\\textbackslash")
131 : from_ascii("\\backslash");
132 docstring const caret = textmode ? from_ascii("\\textasciicircum")
133 : from_ascii("\\mathcircumflex");
134 docstring const tilde = textmode ? from_ascii("\\textasciitilde")
135 : from_ascii("\\sim");
137 return repl(repl(repl(repl(repl(repl(repl(repl(repl(repl(str,
138 '\\', backslash, textmode),
139 '^', caret, textmode),
140 '~', tilde, textmode),
141 '_', from_ascii("\\_")),
142 '$', from_ascii("\\$")),
143 '#', from_ascii("\\#")),
144 '&', from_ascii("\\&")),
145 '%', from_ascii("\\%")),
146 '{', from_ascii("\\{")),
147 '}', from_ascii("\\}"));
152 * Add the row \p cellrow to \p grid.
153 * \returns wether the row could be added. Adding a row can fail for
154 * environments like "equation" that have a fixed number of rows.
156 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
157 docstring const & vskip, bool allow_newpage_ = true)
160 if (cellrow == grid.nrows()) {
161 //lyxerr << "adding row " << cellrow << endl;
162 grid.addRow(cellrow - 1);
163 if (cellrow == grid.nrows()) {
164 // We can't add a row to this grid, so let's
165 // append the content of this cell to the previous
167 // This does not happen in well formed .lyx files,
168 // but LyX versions 1.3.x and older could create
169 // such files and tex2lyx can still do that.
171 lyxerr << "ignoring extra row";
173 lyxerr << " with extra space " << to_utf8(vskip);
175 lyxerr << " with no page break allowed";
176 lyxerr << '.' << endl;
180 grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
181 grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
187 * Add the column \p cellcol to \p grid.
188 * \returns wether the column could be added. Adding a column can fail for
189 * environments like "eqnarray" that have a fixed number of columns.
191 bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
194 if (cellcol == grid.ncols()) {
195 //lyxerr << "adding column " << cellcol << endl;
196 grid.addCol(cellcol);
197 if (cellcol == grid.ncols()) {
198 // We can't add a column to this grid, so let's
199 // append the content of this cell to the previous
201 // This does not happen in well formed .lyx files,
202 // but LyX versions 1.3.x and older could create
203 // such files and tex2lyx can still do that.
205 lyxerr << "ignoring extra column." << endl;
214 * Check wether the last row is empty and remove it if yes.
215 * Otherwise the following code
223 * will result in a grid with 3 rows (+ the dummy row that is always present),
224 * because the last '\\' opens a new row.
226 void delEmptyLastRow(InsetMathGrid & grid)
228 InsetMathGrid::row_type const row = grid.nrows() - 1;
229 for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
230 if (!grid.cell(grid.index(row, col)).empty())
233 // Copy the row information of the empty row (which would contain the
234 // last hline in the example above) to the dummy row and delete the
236 grid.rowinfo(row + 1) = grid.rowinfo(row);
241 // These are TeX's catcodes
243 catEscape, // 0 backslash
253 catSpace, // 10 space
254 catLetter, // 11 a-zA-Z
255 catOther, // 12 none of the above
258 catInvalid // 15 <delete>
261 CatCode theCatcode[128];
264 inline CatCode catcode(char_type c)
266 /* The only characters that are not catOther lie in the pure ASCII
267 * range. Therefore theCatcode has only 128 entries.
268 * TeX itself deals with 8bit characters, so if needed this table
269 * could be enlarged to 256 entries.
270 * Any larger value does not make sense, since the fact that we use
271 * unicode internally does not change Knuth's TeX engine.
272 * Apart from that a table for the full 21bit UCS4 range would waste
273 * too much memory. */
277 return theCatcode[c];
282 FLAG_ALIGN = 1 << 0, // next & or \\ ends the parsing process
283 FLAG_BRACE_LAST = 1 << 1, // next closing brace ends the parsing
284 FLAG_RIGHT = 1 << 2, // next \\right ends the parsing process
285 FLAG_END = 1 << 3, // next \\end ends the parsing process
286 FLAG_BRACK_LAST = 1 << 4, // next closing bracket ends the parsing
287 FLAG_TEXTMODE = 1 << 5, // we are in a box
288 FLAG_ITEM = 1 << 6, // read a (possibly braced) token
289 FLAG_LEAVE = 1 << 7, // leave the loop at the end
290 FLAG_SIMPLE = 1 << 8, // next $ leaves the loop
291 FLAG_EQUATION = 1 << 9, // next \] leaves the loop
292 FLAG_SIMPLE2 = 1 << 10, // next \) leaves the loop
293 FLAG_OPTION = 1 << 11, // read [...] style option
294 FLAG_BRACED = 1 << 12 // read {...} style argument
299 // Helper class for parsing
305 Token() : cs_(), char_(0), cat_(catIgnore) {}
307 Token(char_type c, CatCode cat) : cs_(), char_(c), cat_(cat) {}
309 explicit Token(docstring const & cs) : cs_(cs), char_(0), cat_(catIgnore) {}
312 docstring const & cs() const { return cs_; }
314 CatCode cat() const { return cat_; }
316 char_type character() const { return char_; }
318 docstring asString() const { return cs_.size() ? cs_ : docstring(1, char_); }
320 docstring asInput() const { return cs_.size() ? '\\' + cs_ : docstring(1, char_); }
332 ostream & operator<<(ostream & os, Token const & t)
335 docstring const & cs = t.cs();
336 // FIXME: For some strange reason, the stream operator instanciate
337 // a new Token before outputting the contents of t.cs().
338 // Because of this the line
340 // below becomes recursive.
341 // In order to avoid that we return early:
344 os << '\\' << to_utf8(cs);
346 else if (t.cat() == catLetter)
349 os << '[' << t.character() << ',' << t.cat() << ']';
357 typedef InsetMath::mode_type mode_type;
359 typedef Parse::flags parse_mode;
362 Parser(Lexer & lex, parse_mode mode);
363 /// Only use this for reading from .lyx file format, for the reason
364 /// see Parser::tokenize(istream &).
365 Parser(istream & is, parse_mode mode);
367 Parser(docstring const & str, parse_mode mode);
370 bool parse(MathAtom & at);
372 bool parse(MathData & array, unsigned flags, mode_type mode);
374 bool parse1(InsetMathGrid & grid, unsigned flags, mode_type mode,
377 MathData parse(unsigned flags, mode_type mode);
379 int lineno() const { return lineno_; }
385 void parse2(MathAtom & at, unsigned flags, mode_type mode, bool numbered);
386 /// get arg delimited by 'left' and 'right'
387 docstring getArg(char_type left, char_type right);
391 void error(string const & msg);
392 void error(docstring const & msg) { error(to_utf8(msg)); }
393 /// dump contents to screen
395 /// Only use this for reading from .lyx file format (see
396 /// implementation for reason)
397 void tokenize(istream & is);
399 void tokenize(docstring const & s);
401 void skipSpaceTokens(idocstream & is, char_type c);
403 void push_back(Token const & t);
407 Token const & prevToken() const;
409 Token const & nextToken() const;
411 Token const & getToken();
412 /// skips spaces if any
415 void lex(docstring const & s);
419 docstring parse_verbatim_item();
421 docstring parse_verbatim_option();
426 vector<Token> tokens_;
429 /// Stack of active environments
430 vector<docstring> environments_;
438 Parser::Parser(Lexer & lexer, parse_mode mode)
439 : lineno_(lexer.lineNumber()), pos_(0), mode_(mode), success_(true)
441 tokenize(lexer.getStream());
446 Parser::Parser(istream & is, parse_mode mode)
447 : lineno_(0), pos_(0), mode_(mode), success_(true)
453 Parser::Parser(docstring const & str, parse_mode mode)
454 : lineno_(0), pos_(0), mode_(mode), success_(true)
460 void Parser::push_back(Token const & t)
462 tokens_.push_back(t);
466 void Parser::pop_back()
472 Token const & Parser::prevToken() const
474 static const Token dummy;
475 return pos_ > 0 ? tokens_[pos_ - 1] : dummy;
479 Token const & Parser::nextToken() const
481 static const Token dummy;
482 return good() ? tokens_[pos_] : dummy;
486 Token const & Parser::getToken()
488 static const Token dummy;
489 //lyxerr << "looking at token " << tokens_[pos_] << " pos: " << pos_ << endl;
490 return good() ? tokens_[pos_++] : dummy;
494 void Parser::skipSpaces()
496 while (nextToken().cat() == catSpace || nextToken().cat() == catNewline)
501 void Parser::putback()
507 bool Parser::good() const
509 return pos_ < tokens_.size();
513 char_type Parser::getChar()
516 error("The input stream is not well...");
520 return tokens_[pos_++].character();
524 docstring Parser::getArg(char_type left, char_type right)
529 char_type c = getChar();
534 while ((c = getChar()) != right && good())
541 void Parser::skipSpaceTokens(idocstream & is, char_type c)
543 // skip trailing spaces
544 while (catcode(c) == catSpace || catcode(c) == catNewline)
547 //lyxerr << "putting back: " << c << endl;
552 void Parser::tokenize(istream & is)
554 // eat everything up to the next \end_inset or end of stream
555 // and store it in s for further tokenization
560 if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") {
561 s = s.substr(0, s.size() - 10);
565 // Remove the space after \end_inset
566 if (is.get(c) && c != ' ')
570 tokenize(from_utf8(s));
574 void Parser::tokenize(docstring const & buffer)
576 idocstringstream is(mode_ & Parse::VERBATIM
577 ? escapeSpecialChars(buffer, mode_ & Parse::TEXTMODE)
578 : buffer, ios::in | ios::binary);
582 //lyxerr << "reading c: " << c << endl;
584 switch (catcode(c)) {
588 if (catcode(c) == catNewline)
589 ; //push_back(Token("par"));
591 push_back(Token('\n', catNewline));
599 while (is.get(c) && catcode(c) != catNewline)
609 error("unexpected end of input");
612 if (catcode(c) == catLetter) {
614 while (is.get(c) && catcode(c) == catLetter)
616 skipSpaceTokens(is, c);
625 push_back(Token(c, catcode(c)));
627 skipSpaceTokens(is, c);
632 if (!(mode_ & Parse::QUIET))
633 lyxerr << "ignoring a char: " << int(c) << endl;
638 push_back(Token(c, catcode(c)));
648 void Parser::dump() const
650 lyxerr << "\nTokens: ";
651 for (unsigned i = 0; i < tokens_.size(); ++i) {
654 lyxerr << tokens_[i];
656 lyxerr << " pos: " << pos_ << endl;
660 void Parser::error(string const & msg)
663 if (!(mode_ & Parse::QUIET)) {
664 lyxerr << "Line ~" << lineno_ << ": Math parse error: "
671 bool Parser::parse(MathAtom & at)
675 parse(ar, false, InsetMath::UNDECIDED_MODE);
676 if (ar.size() != 1 || ar.front()->getType() == hullNone) {
677 if (!(mode_ & Parse::QUIET))
678 lyxerr << "unusual contents found: " << ar << endl;
679 at = MathAtom(new InsetMathPar(ar));
680 //if (at->nargs() > 0)
681 // at.nucleus()->cell(0) = ar;
683 // lyxerr << "unusual contents found: " << ar << endl;
691 docstring Parser::parse_verbatim_option()
695 if (nextToken().character() == '[') {
696 Token t = getToken();
697 for (Token t = getToken(); t.character() != ']' && good(); t = getToken()) {
698 if (t.cat() == catBegin) {
700 res += '{' + parse_verbatim_item() + '}';
709 docstring Parser::parse_verbatim_item()
713 if (nextToken().cat() == catBegin) {
714 Token t = getToken();
715 for (Token t = getToken(); t.cat() != catEnd && good(); t = getToken()) {
716 if (t.cat() == catBegin) {
718 res += '{' + parse_verbatim_item() + '}';
728 MathData Parser::parse(unsigned flags, mode_type mode)
731 parse(ar, flags, mode);
736 bool Parser::parse(MathData & array, unsigned flags, mode_type mode)
738 InsetMathGrid grid(1, 1);
739 parse1(grid, flags, mode, false);
740 array = grid.cell(0);
745 void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
748 parse1(*(at.nucleus()->asGridInset()), flags, mode, numbered);
752 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
753 const mode_type mode, const bool numbered)
756 InsetMathGrid::row_type cellrow = 0;
757 InsetMathGrid::col_type cellcol = 0;
758 MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
760 if (grid.asHullInset())
761 grid.asHullInset()->numbered(cellrow, numbered);
764 //lyxerr << " flags: " << flags << endl;
765 //lyxerr << " mode: " << mode << endl;
766 //lyxerr << "grid: " << grid << endl;
769 Token const & t = getToken();
772 lyxerr << "t: " << t << " flags: " << flags << endl;
773 lyxerr << "mode: " << mode << endl;
778 if (flags & FLAG_ITEM) {
780 if (t.cat() == catBegin) {
781 // skip the brace and collect everything to the next matching
783 parse1(grid, FLAG_BRACE_LAST, mode, numbered);
787 // handle only this single token, leave the loop if done
792 if (flags & FLAG_BRACED) {
793 if (t.cat() == catSpace)
796 if (t.cat() != catBegin) {
797 error("opening brace expected");
801 // skip the brace and collect everything to the next matching
803 flags = FLAG_BRACE_LAST;
807 if (flags & FLAG_OPTION) {
808 if (t.cat() == catOther && t.character() == '[') {
810 parse(ar, FLAG_BRACK_LAST, mode);
813 // no option found, put back token and we are done
822 if (t.cat() == catMath) {
823 if (mode != InsetMath::MATH_MODE) {
824 // we are inside some text mode thingy, so opening new math is allowed
825 Token const & n = getToken();
826 if (n.cat() == catMath) {
827 // TeX's $$...$$ syntax for displayed math
828 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
829 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
830 getToken(); // skip the second '$' token
832 // simple $...$ stuff
834 if (mode == InsetMath::UNDECIDED_MODE) {
835 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
836 parse2(cell->back(), FLAG_SIMPLE, InsetMath::MATH_MODE, false);
838 // Don't create nested math hulls (bug #5392)
839 cell->push_back(MathAtom(new InsetMathEnsureMath));
840 parse(cell->back().nucleus()->cell(0), FLAG_SIMPLE, InsetMath::MATH_MODE);
845 else if (flags & FLAG_SIMPLE) {
846 // this is the end of the formula
851 error("something strange in the parser");
856 else if (t.cat() == catLetter)
857 cell->push_back(MathAtom(new InsetMathChar(t.character())));
859 else if (t.cat() == catSpace && mode != InsetMath::MATH_MODE) {
860 if (cell->empty() || cell->back()->getChar() != ' ')
861 cell->push_back(MathAtom(new InsetMathChar(t.character())));
864 else if (t.cat() == catNewline && mode != InsetMath::MATH_MODE) {
865 if (cell->empty() || cell->back()->getChar() != ' ')
866 cell->push_back(MathAtom(new InsetMathChar(' ')));
869 else if (t.cat() == catParameter) {
870 Token const & n = getToken();
871 cell->push_back(MathAtom(new MathMacroArgument(n.character()-'0')));
874 else if (t.cat() == catActive)
875 cell->push_back(MathAtom(new InsetMathChar(t.character())));
877 else if (t.cat() == catBegin) {
879 parse(ar, FLAG_BRACE_LAST, mode);
880 // do not create a BraceInset if they were written by LyX
881 // this helps to keep the annoyance of "a choose b" to a minimum
882 if (ar.size() == 1 && ar[0]->extraBraces())
885 cell->push_back(MathAtom(new InsetMathBrace(ar)));
888 else if (t.cat() == catEnd) {
889 if (flags & FLAG_BRACE_LAST)
891 error("found '}' unexpectedly");
892 //LASSERT(false, /**/);
893 //add(cell, '}', LM_TC_TEX);
896 else if (t.cat() == catAlign) {
897 //lyxerr << " column now " << (cellcol + 1)
898 // << " max: " << grid.ncols() << endl;
899 if (flags & FLAG_ALIGN)
901 if (addCol(grid, cellcol))
902 cell = &grid.cell(grid.index(cellrow, cellcol));
905 else if (t.cat() == catSuper || t.cat() == catSub) {
906 bool up = (t.cat() == catSuper);
907 // we need no new script inset if the last thing was a scriptinset,
908 // which has that script already not the same script already
910 cell->push_back(MathAtom(new InsetMathScript(up)));
911 else if (cell->back()->asScriptInset() &&
912 !cell->back()->asScriptInset()->has(up))
913 cell->back().nucleus()->asScriptInset()->ensure(up);
914 else if (cell->back()->asScriptInset())
915 cell->push_back(MathAtom(new InsetMathScript(up)));
917 cell->back() = MathAtom(new InsetMathScript(cell->back(), up));
918 InsetMathScript * p = cell->back().nucleus()->asScriptInset();
919 // special handling of {}-bases
920 // Here we could remove the brace inset for things
921 // like {a'}^2 and add the braces back in
922 // InsetMathScript::write().
923 // We do not do it, since it is not possible to detect
924 // reliably whether the braces are needed because the
925 // nucleus contains more than one symbol, or whether
926 // they are needed for unknown commands like \xx{a}_0
927 // or \yy{a}{b}_0. This was done in revision 14819
928 // in an unreliable way. See this thread
929 // http://www.mail-archive.com/lyx-devel%40lists.lyx.org/msg104917.html
931 parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
938 else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
939 //lyxerr << "finished reading option" << endl;
943 else if (t.cat() == catOther) {
944 char_type c = t.character();
945 if (isAsciiOrMathAlpha(c)
946 || mode_ & Parse::VERBATIM
947 || !(mode_ & Parse::USETEXT)
948 || mode == InsetMath::TEXT_MODE) {
949 cell->push_back(MathAtom(new InsetMathChar(c)));
951 MathAtom at = createInsetMath("text");
952 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
953 while (nextToken().cat() == catOther
954 && !isAsciiOrMathAlpha(nextToken().character())) {
955 c = getToken().character();
956 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
962 else if (t.cat() == catComment) {
965 Token const & t = getToken();
966 if (t.cat() == catNewline)
970 cell->push_back(MathAtom(new InsetMathComment(s)));
978 else if (t.cs() == "lyxlock") {
980 cell->back().nucleus()->lock(true);
983 else if ((t.cs() == "global" && nextToken().cs() == "def") ||
985 if (t.cs() == "global")
989 docstring name = getToken().cs();
994 while (good() && nextToken().cat() != catBegin) {
995 pars += getToken().cs();
1002 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1004 // is a version for display attached?
1007 if (nextToken().cat() == catBegin)
1008 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1010 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1011 0, MacroTypeDef, vector<MathData>(), def, display)));
1014 else if (t.cs() == "newcommand" ||
1015 t.cs() == "renewcommand" ||
1016 t.cs() == "newlyxcommand") {
1018 if (getToken().cat() != catBegin) {
1019 error("'{' in \\newcommand expected (1) ");
1022 docstring name = getToken().cs();
1023 if (getToken().cat() != catEnd) {
1024 error("'}' in \\newcommand expected");
1029 docstring const arg = getArg('[', ']');
1032 nargs = convert<int>(arg);
1034 // optional argument given?
1037 vector<MathData> optionalValues;
1038 while (nextToken().character() == '[') {
1040 optionalValues.push_back(MathData());
1041 parse(optionalValues[optionals], FLAG_BRACK_LAST, mode);
1046 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1048 // is a version for display attached?
1051 if (nextToken().cat() == catBegin)
1052 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1054 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1055 optionals, MacroTypeNewcommand, optionalValues, def, display)));
1059 else if (t.cs() == "newcommandx" ||
1060 t.cs() == "renewcommandx") {
1061 // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
1064 if (nextToken().cat() == catBegin) {
1066 name = getToken().cs();
1067 if (getToken().cat() != catEnd) {
1068 error("'}' in \\newcommandx expected");
1072 name = getToken().cs();
1075 docstring const arg = getArg('[', ']');
1077 error("[num] in \\newcommandx expected");
1080 int nargs = convert<int>(arg);
1084 vector<MathData> optionalValues;
1085 if (nextToken().character() == '[') {
1089 // handle 'opt=value' options, separated by ','.
1091 while (nextToken().character() != ']' && good()) {
1092 if (nextToken().character() >= '1'
1093 && nextToken().character() <= '9') {
1094 // optional value -> get parameter number
1095 int n = getChar() - '0';
1097 error("Arity of \\newcommandx too low "
1098 "for given optional parameter.");
1103 if (getToken().character() != '=') {
1104 error("'=' and optional parameter value "
1105 "expected for \\newcommandx");
1110 int optNum = max(size_t(n), optionalValues.size());
1111 optionalValues.resize(optNum);
1112 optionalValues[n - 1].clear();
1113 while (nextToken().character() != ']'
1114 && nextToken().character() != ',') {
1116 parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1117 optionalValues[n - 1].append(data);
1119 optionals = max(n, optionals);
1120 } else if (nextToken().cat() == catLetter) {
1121 // we in fact ignore every non-optional
1126 while (nextToken().cat() == catLetter)
1132 if (nextToken().character() == '=') {
1134 while (nextToken().character() != ']'
1135 && nextToken().character() != ',')
1136 parse(value, FLAG_ITEM,
1137 InsetMath::UNDECIDED_MODE);
1140 error("option for \\newcommandx expected");
1146 if (nextToken().character() == ',') {
1149 } else if (nextToken().character() != ']') {
1150 error("Expecting ',' or ']' in options "
1151 "of \\newcommandx");
1164 parse(def, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
1166 // is a version for display attached?
1169 if (nextToken().cat() == catBegin)
1170 parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
1172 cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
1173 optionals, MacroTypeNewcommandx, optionalValues, def,
1177 else if (t.cs() == "(") {
1178 if (mode == InsetMath::MATH_MODE) {
1179 error("bad math environment");
1182 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1183 parse2(cell->back(), FLAG_SIMPLE2, InsetMath::MATH_MODE, false);
1186 else if (t.cs() == "[") {
1187 if (mode != InsetMath::UNDECIDED_MODE) {
1188 error("bad math environment");
1191 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1192 parse2(cell->back(), FLAG_EQUATION, InsetMath::MATH_MODE, false);
1195 else if (t.cs() == "protect")
1196 // ignore \\protect, will hopefully be re-added during output
1199 else if (t.cs() == "end") {
1200 if (flags & FLAG_END) {
1201 // eat environment name
1202 docstring const name = getArg('{', '}');
1203 if (environments_.empty())
1204 error("'found \\end{" + name +
1205 "}' without matching '\\begin{" +
1207 else if (name != environments_.back())
1208 error("'\\end{" + name +
1209 "}' does not match '\\begin{" +
1210 environments_.back() + "}'");
1212 environments_.pop_back();
1213 // Delete empty last row in matrix
1215 // If you abuse InsetMathGrid for
1216 // non-matrix like structures you
1217 // probably need to refine this test.
1218 // Right now we only have to test for
1219 // single line hull insets.
1220 if (grid.nrows() > 1)
1221 delEmptyLastRow(grid);
1225 error("found 'end' unexpectedly");
1228 else if (t.cs() == ")") {
1229 if (flags & FLAG_SIMPLE2)
1231 error("found '\\)' unexpectedly");
1234 else if (t.cs() == "]") {
1235 if (flags & FLAG_EQUATION)
1237 error("found '\\]' unexpectedly");
1240 else if (t.cs() == "\\") {
1241 if (flags & FLAG_ALIGN)
1244 if (nextToken().asInput() == "*") {
1246 added = addRow(grid, cellrow, docstring(), false);
1248 added = addRow(grid, cellrow, getArg('[', ']'));
1250 error("missing token after \\\\");
1253 if (grid.asHullInset())
1254 grid.asHullInset()->numbered(
1256 cell = &grid.cell(grid.index(cellrow,
1262 else if (t.cs() == "multicolumn") {
1263 // extract column count and insert dummy cells
1265 parse(count, FLAG_ITEM, mode);
1267 if (!extractNumber(count, cols)) {
1269 lyxerr << " can't extract number of cells from " << count << endl;
1271 // resize the table if necessary
1272 for (int i = 0; i < cols; ++i) {
1273 if (addCol(grid, cellcol)) {
1274 cell = &grid.cell(grid.index(
1276 // mark this as dummy
1277 grid.cellinfo(grid.index(
1278 cellrow, cellcol)).dummy_ = true;
1281 // the last cell is the real thing, not a dummy
1282 grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
1284 // read special alignment
1286 parse(align, FLAG_ITEM, mode);
1287 //grid.cellinfo(grid.index(cellrow, cellcol)).align_ = extractString(align);
1289 // parse the remaining contents into the "real" cell
1290 parse(*cell, FLAG_ITEM, mode);
1294 else if (t.cs() == "limits")
1297 else if (t.cs() == "nolimits")
1300 else if (t.cs() == "nonumber") {
1301 if (grid.asHullInset())
1302 grid.asHullInset()->numbered(cellrow, false);
1305 else if (t.cs() == "number") {
1306 if (grid.asHullInset())
1307 grid.asHullInset()->numbered(cellrow, true);
1310 else if (t.cs() == "hline") {
1311 grid.rowinfo(cellrow).lines_ ++;
1314 else if (t.cs() == "sqrt") {
1316 parse(ar, FLAG_OPTION, mode);
1318 cell->push_back(MathAtom(new InsetMathRoot));
1319 cell->back().nucleus()->cell(0) = ar;
1320 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1322 cell->push_back(MathAtom(new InsetMathSqrt));
1323 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1327 else if (t.cs() == "unit") {
1328 // Allowed formats \unit[val]{unit}
1330 parse(ar, FLAG_OPTION, mode);
1332 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT)));
1333 cell->back().nucleus()->cell(0) = ar;
1334 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1336 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNIT, 1)));
1337 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1341 else if (t.cs() == "unitfrac") {
1342 // Here allowed formats are \unitfrac[val]{num}{denom}
1344 parse(ar, FLAG_OPTION, mode);
1346 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC, 3)));
1347 cell->back().nucleus()->cell(2) = ar;
1349 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::UNITFRAC)));
1351 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1352 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1355 else if (t.cs() == "cfrac") {
1356 // allowed formats are \cfrac[pos]{num}{denom}
1357 docstring const arg = getArg('[', ']');
1358 //lyxerr << "got so far: '" << arg << "'" << endl;
1360 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRACLEFT)));
1361 else if (arg == "r")
1362 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRACRIGHT)));
1363 else if (arg.empty() || arg == "c")
1364 cell->push_back(MathAtom(new InsetMathFrac(InsetMathFrac::CFRAC)));
1366 error("found invalid optional argument");
1369 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1370 parse(cell->back().nucleus()->cell(1), FLAG_ITEM, mode);
1373 else if (t.cs() == "xrightarrow" || t.cs() == "xleftarrow") {
1374 cell->push_back(createInsetMath(t.cs()));
1375 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1376 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1379 else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
1380 || t.cs() == "pageref" || t.cs() == "vpageref" || t.cs() == "vref") {
1381 cell->push_back(MathAtom(new InsetMathRef(t.cs())));
1382 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, mode);
1383 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
1386 else if (t.cs() == "left") {
1388 Token const & tl = getToken();
1389 // \| and \Vert are equivalent, and InsetMathDelim
1391 // FIXME: fix this in InsetMathDelim itself!
1392 docstring const l = tl.cs() == "|" ? from_ascii("Vert") : tl.asString();
1394 parse(ar, FLAG_RIGHT, mode);
1398 Token const & tr = getToken();
1399 docstring const r = tr.cs() == "|" ? from_ascii("Vert") : tr.asString();
1400 cell->push_back(MathAtom(new InsetMathDelim(l, r, ar)));
1403 else if (t.cs() == "right") {
1404 if (flags & FLAG_RIGHT)
1406 //lyxerr << "got so far: '" << cell << "'" << endl;
1407 error("Unmatched right delimiter");
1411 else if (t.cs() == "begin") {
1412 docstring const name = getArg('{', '}');
1413 environments_.push_back(name);
1415 if (name == "array" || name == "subarray") {
1416 docstring const valign = parse_verbatim_option() + 'c';
1417 docstring const halign = parse_verbatim_item();
1418 cell->push_back(MathAtom(new InsetMathArray(name,
1419 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1420 parse2(cell->back(), FLAG_END, mode, false);
1423 else if (name == "tabular") {
1424 docstring const valign = parse_verbatim_option() + 'c';
1425 docstring const halign = parse_verbatim_item();
1426 cell->push_back(MathAtom(new InsetMathTabular(name,
1427 InsetMathGrid::guessColumns(halign), 1, (char)valign[0], halign)));
1428 parse2(cell->back(), FLAG_END, InsetMath::TEXT_MODE, false);
1431 else if (name == "split" || name == "cases") {
1432 cell->push_back(createInsetMath(name));
1433 parse2(cell->back(), FLAG_END, mode, false);
1436 else if (name == "alignedat") {
1437 docstring const valign = parse_verbatim_option() + 'c';
1438 // ignore this for a while
1440 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1441 parse2(cell->back(), FLAG_END, mode, false);
1444 else if (name == "math") {
1445 if (mode == InsetMath::MATH_MODE) {
1446 error("bad math environment");
1449 cell->push_back(MathAtom(new InsetMathHull(hullSimple)));
1450 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, true);
1453 else if (name == "equation" || name == "equation*"
1454 || name == "displaymath") {
1455 if (mode != InsetMath::UNDECIDED_MODE) {
1456 error("bad math environment");
1459 cell->push_back(MathAtom(new InsetMathHull(hullEquation)));
1460 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, (name == "equation"));
1463 else if (name == "eqnarray" || name == "eqnarray*") {
1464 if (mode != InsetMath::UNDECIDED_MODE) {
1465 error("bad math environment");
1468 cell->push_back(MathAtom(new InsetMathHull(hullEqnArray)));
1469 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1472 else if (name == "align" || name == "align*") {
1473 if (mode != InsetMath::UNDECIDED_MODE) {
1474 error("bad math environment");
1477 cell->push_back(MathAtom(new InsetMathHull(hullAlign)));
1478 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1481 else if (name == "flalign" || name == "flalign*") {
1482 if (mode != InsetMath::UNDECIDED_MODE) {
1483 error("bad math environment");
1486 cell->push_back(MathAtom(new InsetMathHull(hullFlAlign)));
1487 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1490 else if (name == "alignat" || name == "alignat*") {
1491 if (mode != InsetMath::UNDECIDED_MODE) {
1492 error("bad math environment");
1495 // ignore this for a while
1497 cell->push_back(MathAtom(new InsetMathHull(hullAlignAt)));
1498 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1501 else if (name == "xalignat" || name == "xalignat*") {
1502 if (mode != InsetMath::UNDECIDED_MODE) {
1503 error("bad math environment");
1506 // ignore this for a while
1508 cell->push_back(MathAtom(new InsetMathHull(hullXAlignAt)));
1509 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1512 else if (name == "xxalignat") {
1513 if (mode != InsetMath::UNDECIDED_MODE) {
1514 error("bad math environment");
1517 // ignore this for a while
1519 cell->push_back(MathAtom(new InsetMathHull(hullXXAlignAt)));
1520 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1523 else if (name == "multline" || name == "multline*") {
1524 if (mode != InsetMath::UNDECIDED_MODE) {
1525 error("bad math environment");
1528 cell->push_back(MathAtom(new InsetMathHull(hullMultline)));
1529 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1532 else if (name == "gather" || name == "gather*") {
1533 if (mode != InsetMath::UNDECIDED_MODE) {
1534 error("bad math environment");
1537 cell->push_back(MathAtom(new InsetMathHull(hullGather)));
1538 parse2(cell->back(), FLAG_END, InsetMath::MATH_MODE, !stared(name));
1541 else if (latexkeys const * l = in_word_set(name)) {
1542 if (l->inset == "matrix") {
1543 cell->push_back(createInsetMath(name));
1544 parse2(cell->back(), FLAG_END, mode, false);
1545 } else if (l->inset == "split") {
1546 docstring const valign = parse_verbatim_option() + 'c';
1547 cell->push_back(MathAtom(new InsetMathSplit(name, (char)valign[0])));
1548 parse2(cell->back(), FLAG_END, mode, false);
1551 if (!(mode_ & Parse::QUIET)) {
1553 lyxerr << "found math environment `"
1555 << "' in symbols file with unsupported inset `"
1556 << to_utf8(l->inset)
1559 // create generic environment inset
1560 cell->push_back(MathAtom(new InsetMathEnv(name)));
1561 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1567 if (!(mode_ & Parse::QUIET)) {
1569 lyxerr << "found unknown math environment '"
1570 << to_utf8(name) << "'" << endl;
1572 // create generic environment inset
1573 cell->push_back(MathAtom(new InsetMathEnv(name)));
1574 parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
1578 else if (t.cs() == "kern") {
1583 Token const & t = getToken();
1587 while (num_tokens--)
1592 if (isValidLength(to_utf8(s)))
1596 cell->push_back(MathAtom(new InsetMathKern));
1598 cell->push_back(MathAtom(new InsetMathKern(s)));
1601 else if (t.cs() == "label") {
1602 // FIXME: This is swallowed in inline formulas
1603 docstring label = parse_verbatim_item();
1606 if (grid.asHullInset()) {
1607 grid.asHullInset()->label(cellrow, label);
1609 cell->push_back(createInsetMath(t.cs()));
1610 cell->push_back(MathAtom(new InsetMathBrace(ar)));
1614 else if (t.cs() == "choose" || t.cs() == "over"
1615 || t.cs() == "atop" || t.cs() == "brace"
1616 || t.cs() == "brack") {
1617 MathAtom at = createInsetMath(t.cs());
1618 at.nucleus()->cell(0) = *cell;
1620 parse(at.nucleus()->cell(1), flags, mode);
1621 cell->push_back(at);
1625 else if (t.cs() == "color") {
1626 docstring const color = parse_verbatim_item();
1627 cell->push_back(MathAtom(new InsetMathColor(true, color)));
1628 parse(cell->back().nucleus()->cell(0), flags, mode);
1632 else if (t.cs() == "textcolor") {
1633 docstring const color = parse_verbatim_item();
1634 cell->push_back(MathAtom(new InsetMathColor(false, color)));
1635 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1638 else if (t.cs() == "normalcolor") {
1639 cell->push_back(createInsetMath(t.cs()));
1640 parse(cell->back().nucleus()->cell(0), flags, mode);
1644 else if (t.cs() == "substack") {
1645 cell->push_back(createInsetMath(t.cs()));
1646 parse2(cell->back(), FLAG_ITEM, mode, false);
1649 else if (t.cs() == "xymatrix") {
1650 odocstringstream os;
1651 while (good() && nextToken().cat() != catBegin)
1652 os << getToken().asInput();
1653 cell->push_back(createInsetMath(t.cs() + os.str()));
1654 parse2(cell->back(), FLAG_ITEM, mode, false);
1657 else if (t.cs() == "framebox" || t.cs() == "makebox") {
1658 cell->push_back(createInsetMath(t.cs()));
1659 parse(cell->back().nucleus()->cell(0), FLAG_OPTION, InsetMath::TEXT_MODE);
1660 parse(cell->back().nucleus()->cell(1), FLAG_OPTION, InsetMath::TEXT_MODE);
1661 parse(cell->back().nucleus()->cell(2), FLAG_ITEM, InsetMath::TEXT_MODE);
1664 else if (t.cs() == "tag") {
1665 if (nextToken().character() == '*') {
1667 cell->push_back(createInsetMath(t.cs() + '*'));
1669 cell->push_back(createInsetMath(t.cs()));
1670 parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
1673 else if (t.cs() == "hspace" && nextToken().character() != '*') {
1674 docstring const name = t.cs();
1675 docstring const arg = parse_verbatim_item();
1677 if (isValidLength(to_utf8(arg), &length))
1678 cell->push_back(MathAtom(new InsetMathSpace(length)));
1680 // Since the Length class cannot use length variables
1681 // we must not create an InsetMathSpace.
1682 cell->push_back(MathAtom(new MathMacro(name)));
1684 mathed_parse_cell(ar, '{' + arg + '}');
1690 else if (t.cs() == "infer") {
1692 parse(ar, FLAG_OPTION, mode);
1693 cell->push_back(createInsetMath(t.cs()));
1694 parse2(cell->back(), FLAG_ITEM, mode, false);
1698 else if (1 && t.cs() == "ar") {
1699 auto_ptr<InsetMathXYArrow> p(new InsetMathXYArrow);
1700 // try to read target
1701 parse(p->cell(0), FLAG_OTPTION, mode);
1702 // try to read label
1703 if (nextToken().cat() == catSuper || nextToken().cat() == catSub) {
1704 p->up_ = nextToken().cat() == catSuper;
1706 parse(p->cell(1), FLAG_ITEM, mode);
1707 //lyxerr << "read label: " << p->cell(1) << endl;
1710 cell->push_back(MathAtom(p.release()));
1711 //lyxerr << "read cell: " << cell << endl;
1715 else if (t.cs() == "lyxmathsym") {
1717 if (getToken().cat() != catBegin) {
1718 error("'{' expected in \\" + t.cs());
1723 CatCode cat = nextToken().cat();
1724 while (good() && (count || cat != catEnd)) {
1725 if (cat == catBegin)
1727 else if (cat == catEnd)
1729 cmd += getToken().asInput();
1730 cat = nextToken().cat();
1732 if (getToken().cat() != catEnd) {
1733 error("'}' expected in \\" + t.cs());
1738 cmd = Encodings::fromLaTeXCommand(cmd, rem);
1739 for (size_t i = 0; i < cmd.size(); ++i)
1740 cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
1742 char_type c = rem[0];
1743 cell->push_back(MathAtom(new InsetMathChar(c)));
1744 cmd = rem.substr(1);
1748 } while (cmd.size());
1751 else if (t.cs().size()) {
1752 latexkeys const * l = in_word_set(t.cs());
1754 if (l->inset == "big") {
1756 docstring const delim = getToken().asInput();
1757 if (InsetMathBig::isBigInsetDelim(delim))
1758 cell->push_back(MathAtom(
1759 new InsetMathBig(t.cs(), delim)));
1761 cell->push_back(createInsetMath(t.cs()));
1766 else if (l->inset == "font") {
1767 cell->push_back(createInsetMath(t.cs()));
1768 parse(cell->back().nucleus()->cell(0),
1769 FLAG_ITEM, asMode(mode, l->extra));
1772 else if (l->inset == "oldfont") {
1773 cell->push_back(createInsetMath(t.cs()));
1774 parse(cell->back().nucleus()->cell(0),
1775 flags | FLAG_ALIGN, asMode(mode, l->extra));
1776 if (prevToken().cat() != catAlign &&
1777 prevToken().cs() != "\\")
1782 else if (l->inset == "style") {
1783 cell->push_back(createInsetMath(t.cs()));
1784 parse(cell->back().nucleus()->cell(0),
1785 flags | FLAG_ALIGN, mode);
1786 if (prevToken().cat() != catAlign &&
1787 prevToken().cs() != "\\")
1793 MathAtom at = createInsetMath(t.cs());
1794 for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
1795 parse(at.nucleus()->cell(i),
1796 FLAG_ITEM, asMode(mode, l->extra));
1797 cell->push_back(at);
1802 bool is_unicode_symbol = false;
1803 if (mode == InsetMath::TEXT_MODE) {
1805 docstring cmd = prevToken().asInput();
1806 CatCode cat = nextToken().cat();
1807 if (cat == catBegin) {
1809 while (good() && (count || cat != catEnd)) {
1810 cat = nextToken().cat();
1811 cmd += getToken().asInput();
1813 if (cat == catBegin)
1815 else if (cat == catEnd)
1821 Encodings::fromLaTeXCommand(cmd, is_combining);
1823 if (cat == catLetter)
1825 cmd += getToken().asInput();
1827 if (cat == catLetter)
1829 c = Encodings::fromLaTeXCommand(cmd, is_combining);
1832 is_unicode_symbol = true;
1833 cell->push_back(MathAtom(new InsetMathChar(c)));
1835 while (num_tokens--)
1839 if (!is_unicode_symbol) {
1840 MathAtom at = createInsetMath(t.cs());
1841 InsetMath::mode_type m = mode;
1842 //if (m == InsetMath::UNDECIDED_MODE)
1843 //lyxerr << "default creation: m1: " << m << endl;
1844 if (at->currentMode() != InsetMath::UNDECIDED_MODE)
1845 m = at->currentMode();
1846 //lyxerr << "default creation: m2: " << m << endl;
1847 InsetMath::idx_type start = 0;
1848 // this fails on \bigg[...\bigg]
1850 //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
1853 // at.nucleus()->cell(0) = opt;
1855 for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
1856 parse(at.nucleus()->cell(i), FLAG_ITEM, m);
1859 cell->push_back(at);
1865 if (flags & FLAG_LEAVE) {
1866 flags &= ~FLAG_LEAVE;
1875 } // anonymous namespace
1878 bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
1880 return Parser(str, f).parse(ar, 0, f & Parse::TEXTMODE ?
1881 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1885 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
1887 return Parser(is, f).parse(ar, 0, f & Parse::TEXTMODE ?
1888 InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
1892 bool mathed_parse_normal(MathAtom & t, docstring const & str, Parse::flags f)
1894 return Parser(str, f).parse(t);
1898 bool mathed_parse_normal(MathAtom & t, Lexer & lex, Parse::flags f)
1900 return Parser(lex, f).parse(t);
1904 bool mathed_parse_normal(InsetMathGrid & grid, docstring const & str, Parse::flags f)
1906 return Parser(str, f).parse1(grid, 0, f & Parse::TEXTMODE ?
1907 InsetMath::TEXT_MODE : InsetMath::MATH_MODE, false);
1913 fill(theCatcode, theCatcode + 128, catOther);
1914 fill(theCatcode + 'a', theCatcode + 'z' + 1, catLetter);
1915 fill(theCatcode + 'A', theCatcode + 'Z' + 1, catLetter);
1917 theCatcode[int('\\')] = catEscape;
1918 theCatcode[int('{')] = catBegin;
1919 theCatcode[int('}')] = catEnd;
1920 theCatcode[int('$')] = catMath;
1921 theCatcode[int('&')] = catAlign;
1922 theCatcode[int('\n')] = catNewline;
1923 theCatcode[int('#')] = catParameter;
1924 theCatcode[int('^')] = catSuper;
1925 theCatcode[int('_')] = catSub;
1926 theCatcode[int(0x7f)] = catIgnore;
1927 theCatcode[int(' ')] = catSpace;
1928 theCatcode[int('\t')] = catSpace;
1929 theCatcode[int('\r')] = catNewline;
1930 theCatcode[int('~')] = catActive;
1931 theCatcode[int('%')] = catComment;