]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathParser.cpp
* \newcommandx support for the math parser
[lyx.git] / src / mathed / MathParser.cpp
index 1d27f65f3e6a9f88e6e64588f793863aa3f2277a..4856924590d3c49f2278c888646eb1a850b80641 100644 (file)
@@ -63,28 +63,18 @@ following hack as starting point to write some macros:
 #include "MathSupport.h"
 
 #include "Lexer.h"
-#include "debug.h"
 
+#include "support/debug.h"
 #include "support/convert.h"
 #include "support/docstream.h"
 
 #include <sstream>
 
-
-namespace lyx {
-
-using std::endl;
-using std::fill;
-
-using std::string;
-using std::ios;
-using std::istream;
-using std::ostream;
-using std::vector;
-
-
 //#define FILEDEBUG
 
+using namespace std;
+
+namespace lyx {
 
 namespace {
 
@@ -112,7 +102,7 @@ bool stared(docstring const & s)
  * environments like "equation" that have a fixed number of rows.
  */
 bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
-           docstring const & vskip, bool allow_pagebreak = true)
+           docstring const & vskip, bool allow_newpage_ = true)
 {
        ++cellrow;
        if (cellrow == grid.nrows()) {
@@ -129,14 +119,14 @@ bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
                        lyxerr << "ignoring extra row";
                        if (!vskip.empty())
                                lyxerr << " with extra space " << to_utf8(vskip);
-                       if (!allow_pagebreak)
+                       if (!allow_newpage_)
                                lyxerr << " with no page break allowed";
                        lyxerr << '.' << endl;
                        return false;
                }
        }
        grid.vcrskip(Length(to_utf8(vskip)), cellrow - 1);
-       grid.rowinfo(cellrow - 1).allow_pagebreak_ = allow_pagebreak;
+       grid.rowinfo(cellrow - 1).allow_newpage_ = allow_newpage_;
        return true;
 }
 
@@ -317,7 +307,7 @@ public:
        ///
        Parser(Lexer & lex);
        /// Only use this for reading from .lyx file format, for the reason
-       /// see Parser::tokenize(std::istream &).
+       /// see Parser::tokenize(istream &).
        Parser(istream & is);
        ///
        Parser(docstring const & str);
@@ -902,16 +892,28 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                cell->back().nucleus()->lock(true);
                }
 
-               else if (t.cs() == "def" ||
+               else if ((t.cs() == "global" && nextToken().cs() == "def") ||
+                       t.cs() == "def" ||
                        t.cs() == "newcommand" ||
-                       t.cs() == "renewcommand")
+                       t.cs() == "renewcommand" ||
+                       t.cs() == "newlyxcommand" ||
+                       t.cs() == "newcommandx" ||
+                       t.cs() == "renewcommandx")
                {
-                       docstring const type = t.cs();
+                       MacroType type = MacroTypeNewcommand;
+                       if (t.cs() == "global") {
+                               getToken();
+                               type = MacroTypeDef;
+                       }
+                       if (t.cs() == "def")
+                               type = MacroTypeDef;
+                       if (t.cs() == "newcommandx" || t.cs() == "renewcommandx")
+                               type = MacroTypeNewcommandx;
                        docstring name;
                        int nargs = 0;
                        int optionals = 0;
-                       std::vector<MathData> optionalValues;
-                       if (t.cs() == "def") {
+                       vector<MathData> optionalValues;
+                       if (type == MacroTypeDef) {
                                // get name
                                name = getToken().cs();
 
@@ -924,7 +926,100 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                nargs /= 2;
                                //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
 
-                       } else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
+                       } else if (type == MacroTypeNewcommandx) {
+                               // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
+                               // name
+                               if (nextToken().cat() == catBegin) {
+                                       getToken();
+                                       name = getToken().cs();
+                                       if (getToken().cat() != catEnd) {
+                                               error("'}' in \\newcommandx expected");
+                                               return;
+                                       }
+                               } else
+                                       name = getToken().cs();
+                               
+                               // arity
+                               docstring const arg = getArg('[', ']');
+                               if (arg.empty()) {
+                                       error("[num] in \\newcommandx expected");
+                                       return;
+                               }
+                               nargs = convert<int>(arg);
+                               
+                               // options
+                               if (nextToken().character() == '[') {
+                                       // skip '['
+                                       getToken();
+                                       
+                                       // handle 'opt=value' options, separated by ','.
+                                       skipSpaces();
+                                       while (nextToken().character() != ']' && good()) {
+                                               if (nextToken().character() >= '1'
+                                                   && nextToken().character() <= '9') {
+                                                       // optional value -> get parameter number
+                                                       int n = getChar() - '0';
+                                                       if (n > nargs) {
+                                                               error("Arity of \\newcommandx too low for given optional parameter.");
+                                                               return;
+                                                       }
+                                                       
+                                                       // skip '='
+                                                       if (getToken().character() != '=') {
+                                                               error("'=' and optional parameter value expected for \\newcommandx");
+                                                               return;
+                                                       }
+                                                       
+                                                       // get value
+                                                       optionalValues.resize(max(size_t(n), optionalValues.size()));
+                                                       optionalValues[n - 1].clear();
+                                                       while (nextToken().character() != ']'
+                                                              && nextToken().character() != ',') {
+                                                               MathData data;
+                                                               parse(data, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
+                                                               optionalValues[n - 1].append(data);
+                                                       }
+                                                       optionals = max(n, optionals);
+                                               } else if (nextToken().cat() == catLetter) {
+                                                       // we in fact ignore every non-optional
+                                                       // parameters
+                                                       
+                                                       // get option name
+                                                       docstring opt;
+                                                       while (nextToken().cat() == catLetter)
+                                                               opt += getChar();
+                                                       
+                                                       // value?
+                                                       skipSpaces();
+                                                       MathData value;
+                                                       if (nextToken().character() == '=') {
+                                                               getToken();
+                                                               while (nextToken().character() != ']'
+                                                                       && nextToken().character() != ',')
+                                                                       parse(value, FLAG_ITEM, InsetMath::UNDECIDED_MODE);
+                                                       }
+                                               } else {
+                                                       error("option for \\newcommandx expected");
+                                                       return;
+                                               }
+
+                                               // skip komma
+                                               skipSpaces();
+                                               if (nextToken().character() == ',') {
+                                                       getChar();
+                                                       skipSpaces();
+                                               } else if (nextToken().character() != ']') {
+                                                       error("Expecting ',' or ']' in options of \\newcommandx");
+                                                       return;
+                                               }
+                                       }
+                                       
+                                       // skip ']'
+                                       if (!good())
+                                               return;
+                                       getToken();
+                               }
+                       } else {
                                if (getToken().cat() != catBegin) {
                                        error("'{' in \\newcommand expected (1) ");
                                        return;
@@ -960,8 +1055,8 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        if (nextToken().cat() == catBegin)
                                parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
 
-                       cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, optionals, type, 
-                                                                                                                                                                                                                optionalValues, def, display)));
+                       cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
+                               optionals, type, optionalValues, def, display)));
                }
 
                else if (t.cs() == "(") {
@@ -1273,7 +1368,7 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                               << to_utf8(l->inset) << "'." << endl;
                                        // create generic environment inset
                                        cell->push_back(MathAtom(new InsetMathEnv(name)));
-                                       parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
+                                       parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
                                }
                        }
 
@@ -1283,7 +1378,7 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                        << "'" << endl;
                                // create generic environment inset
                                cell->push_back(MathAtom(new InsetMathEnv(name)));
-                               parse(cell->back().nucleus()->cell(0), FLAG_ITEM, mode);
+                               parse(cell->back().nucleus()->cell(0), FLAG_END, mode);
                        }
                }