]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathParser.cpp
Add \makeat switches to babel settings if necessary.
[lyx.git] / src / mathed / MathParser.cpp
index f86a762be83e865f76c78ca856fc44021f416125..9752320095312a75ff1b857d007ee0e679b50c27 100644 (file)
@@ -42,6 +42,7 @@ following hack as starting point to write some macros:
 #include "InsetMathArray.h"
 #include "InsetMathBig.h"
 #include "InsetMathBrace.h"
+#include "InsetMathCancelto.h"
 #include "InsetMathChar.h"
 #include "InsetMathColor.h"
 #include "InsetMathComment.h"
@@ -252,7 +253,7 @@ bool innerHull(docstring const & name)
        return name == "array" || name == "cases" || name == "aligned"
                || name == "alignedat" || name == "gathered" || name == "split"
                || name == "subarray" || name == "tabular" || name == "matrix"
-               || name.substr(1) == "matrix";
+               || name == "smallmatrix" || name.substr(1) == "matrix";
 }
 
 
@@ -535,7 +536,6 @@ char_type Parser::getChar()
 {
        if (!good()) {
                error("The input stream is not well...");
-               putback();
                return 0;
        }
        return tokens_[pos_++].character();
@@ -544,9 +544,12 @@ char_type Parser::getChar()
 
 docstring Parser::getArg(char_type left, char_type right)
 {
+       docstring result;
        skipSpaces();
 
-       docstring result;
+       if (!good())
+               return result;
+
        char_type c = getChar();
 
        if (c != left)
@@ -914,7 +917,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                }
 
                else if (t.cat() == catActive)
-                       cell->push_back(MathAtom(new InsetMathChar(t.character())));
+                       cell->push_back(MathAtom(new InsetMathSpace(string(1, t.character()), "")));
 
                else if (t.cat() == catBegin) {
                        MathData ar;
@@ -1015,7 +1018,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                Token const & t = getToken();
                                if (t.cat() == catNewline)
                                        break;
-                               s += t.asString();
+                               s += t.asInput();
                        }
                        cell->push_back(MathAtom(new InsetMathComment(buf, s)));
                        skipSpaces();
@@ -1060,6 +1063,9 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        cell->push_back(MathAtom(new MathMacroTemplate(buf,
                                name, nargs, 0, MacroTypeDef,
                                vector<MathData>(), def, display)));
+
+                       if (buf && (mode_ & Parse::TRACKMACRO))
+                               buf->usermacros.insert(name);
                }
                
                else if (t.cs() == "newcommand" ||
@@ -1105,6 +1111,9 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        cell->push_back(MathAtom(new MathMacroTemplate(buf,
                                name, nargs, optionals, MacroTypeNewcommand,
                                optionalValues, def, display)));
+
+                       if (buf && (mode_ & Parse::TRACKMACRO))
+                               buf->usermacros.insert(name);
                }
                
                else if (t.cs() == "newcommandx" ||
@@ -1223,6 +1232,9 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        cell->push_back(MathAtom(new MathMacroTemplate(buf,
                                name, nargs, optionals, MacroTypeNewcommandx,
                                optionalValues, def, display)));
+
+                       if (buf && (mode_ & Parse::TRACKMACRO))
+                               buf->usermacros.insert(name);
                }
 
                else if (t.cs() == "(") {
@@ -1375,6 +1387,14 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        }
                }
 
+               else if (t.cs() == "cancelto") {
+                       MathData ar;
+                       parse(ar, FLAG_ITEM, mode);
+                               cell->push_back(MathAtom(new InsetMathCancelto(buf)));
+                               cell->back().nucleus()->cell(1) = ar;
+                               parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
+               }
+
                else if (t.cs() == "unit") {
                        // Allowed formats \unit[val]{unit}
                        MathData ar;
@@ -1742,12 +1762,17 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        parse(cell->back().nucleus()->cell(0), FLAG_ITEM, InsetMath::TEXT_MODE);
                }
 
-               else if (t.cs() == "hspace" && nextToken().character() != '*') {
+               else if (t.cs() == "hspace") {
+                       bool const prot =  nextToken().character() == '*';
+                       if (prot)
+                               getToken();
                        docstring const name = t.cs();
                        docstring const arg = parse_verbatim_item();
                        Length length;
-                       if (isValidLength(to_utf8(arg), &length))
-                               cell->push_back(MathAtom(new InsetMathSpace(length)));
+                       if (prot && arg == "\\fill")
+                               cell->push_back(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
+                       else if (isValidLength(to_utf8(arg), &length))
+                               cell->push_back(MathAtom(new InsetMathSpace(length, prot)));
                        else {
                                // Since the Length class cannot use length variables
                                // we must not create an InsetMathSpace.
@@ -1805,12 +1830,15 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                error("'}' expected in \\" + t.cs());
                                return success_;
                        }
+                       bool termination;
                        docstring rem;
                        do {
-                               cmd = Encodings::fromLaTeXCommand(cmd, rem);
+                               cmd = Encodings::fromLaTeXCommand(cmd,
+                                       Encodings::MATH_CMD | Encodings::TEXT_CMD,
+                                       termination, rem);
                                for (size_t i = 0; i < cmd.size(); ++i)
                                        cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
-                               if (rem.size()) {
+                               if (!rem.empty()) {
                                        char_type c = rem[0];
                                        cell->push_back(MathAtom(new InsetMathChar(c)));
                                        cmd = rem.substr(1);
@@ -1821,14 +1849,17 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                }
 
                else if (t.cs().size()) {
-                       latexkeys const * l = in_word_set(t.cs());
-
-                       if (buf && (mode_ & Parse::TRACKMACRO) && l)
-                               buf->updateMacros();
+                       bool const no_mhchem =
+                               (t.cs() == "ce" || t.cs() == "cf")
+                               && buf && buf->params().use_package("mhchem") ==
+                                               BufferParams::package_off;
 
-                       bool const is_user_macro =
-                               (buf && buf->getMacro(t.cs(), false) != 0);
+                       bool const is_user_macro = no_mhchem ||
+                               (buf && (mode_ & Parse::TRACKMACRO
+                                        ? buf->usermacros.count(t.cs()) != 0
+                                        : buf->getMacro(t.cs(), false) != 0));
 
+                       latexkeys const * l = in_word_set(t.cs());
                        if (l && !is_user_macro) {
                                if (l->inset == "big") {
                                        skipSpaces();
@@ -1896,8 +1927,10 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                                }
                                        }
                                        bool is_combining;
-                                       char_type c =
-                                               Encodings::fromLaTeXCommand(cmd, is_combining);
+                                       bool termination;
+                                       char_type c = Encodings::fromLaTeXCommand(cmd,
+                                               Encodings::MATH_CMD | Encodings::TEXT_CMD,
+                                               is_combining, termination);
                                        if (is_combining) {
                                                if (cat == catLetter)
                                                        cmd += '{';
@@ -1905,9 +1938,26 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                                ++num_tokens;
                                                if (cat == catLetter)
                                                        cmd += '}';
-                                               c = Encodings::fromLaTeXCommand(cmd, is_combining);
+                                               c = Encodings::fromLaTeXCommand(cmd,
+                                                       Encodings::MATH_CMD | Encodings::TEXT_CMD,
+                                                       is_combining, termination);
                                        }
                                        if (c) {
+                                               if (termination) {
+                                                       if (nextToken().cat() == catBegin) {
+                                                               getToken();
+                                                               if (nextToken().cat() == catEnd) {
+                                                                       getToken();
+                                                                       num_tokens += 2;
+                                                               } else
+                                                                       putback();
+                                                       } else {
+                                                               while (nextToken().cat() == catSpace) {
+                                                                       getToken();
+                                                                       ++num_tokens;
+                                                               }
+                                                       }
+                                               }
                                                is_unicode_symbol = true;
                                                cell->push_back(MathAtom(new InsetMathChar(c)));
                                        } else {