* \newcommandx support for the math parser

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23619 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Stefan Schimanski 2008-03-10 12:50:10 +00:00
parent ec1f626284
commit 5d48c7ef6a
2 changed files with 107 additions and 4 deletions

View File

@ -31,6 +31,7 @@ class Paragraph;
enum MacroType { enum MacroType {
MacroTypeNewcommand, MacroTypeNewcommand,
MacroTypeNewcommandx,
MacroTypeDef MacroTypeDef
}; };

View File

@ -892,14 +892,23 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
cell->back().nucleus()->lock(true); 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() == "newcommand" ||
t.cs() == "renewcommand" || t.cs() == "renewcommand" ||
t.cs() == "newlyxcommand") t.cs() == "newlyxcommand" ||
t.cs() == "newcommandx" ||
t.cs() == "renewcommandx")
{ {
MacroType type = MacroTypeNewcommand; MacroType type = MacroTypeNewcommand;
if (t.cs() == "global") {
getToken();
type = MacroTypeDef;
}
if (t.cs() == "def") if (t.cs() == "def")
type = MacroTypeDef; type = MacroTypeDef;
if (t.cs() == "newcommandx" || t.cs() == "renewcommandx")
type = MacroTypeNewcommandx;
docstring name; docstring name;
int nargs = 0; int nargs = 0;
int optionals = 0; int optionals = 0;
@ -917,6 +926,99 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
nargs /= 2; nargs /= 2;
//lyxerr << "read \\def parameter list '" << pars << "'" << endl; //lyxerr << "read \\def parameter list '" << pars << "'" << endl;
} 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 { } else {
if (getToken().cat() != catBegin) { if (getToken().cat() != catBegin) {
error("'{' in \\newcommand expected (1) "); error("'{' in \\newcommand expected (1) ");
@ -953,8 +1055,8 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags,
if (nextToken().cat() == catBegin) if (nextToken().cat() == catBegin)
parse(display, FLAG_ITEM, InsetMath::MATH_MODE); parse(display, FLAG_ITEM, InsetMath::MATH_MODE);
cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, optionals, type, cell->push_back(MathAtom(new MathMacroTemplate(name, nargs,
optionalValues, def, display))); optionals, type, optionalValues, def, display)));
} }
else if (t.cs() == "(") { else if (t.cs() == "(") {