allow 'renewcommand' and 'def' for math macros

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7241 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-07-04 15:55:18 +00:00
parent fe390e9da1
commit 11cee62d35
6 changed files with 37 additions and 22 deletions

View File

@ -957,8 +957,10 @@ void mathDispatch(FuncRequest const & cmd)
else {
string s = cmd.argument;
string const s1 = token(s, ' ', 1);
int const na = s1.empty() ? 0 : atoi(s1);
openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
int const nargs = s1.empty() ? 0 : atoi(s1);
string const s2 = token(s, ' ', 2);
string const type = s2.empty() ? "newcommand" : s2;
openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
}
break;

View File

@ -50,10 +50,11 @@ InsetFormulaMacro::InsetFormulaMacro()
}
InsetFormulaMacro::InsetFormulaMacro(string const & name, int nargs)
InsetFormulaMacro::InsetFormulaMacro
(string const & name, int nargs, string const & type)
{
setInsetName(name);
MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs)));
MathMacroTable::create(MathAtom(new MathMacroTemplate(name, nargs, type)));
}

View File

@ -29,7 +29,7 @@ public:
///
InsetFormulaMacro();
/// construct a macro hull from its name and the number of arguments
explicit InsetFormulaMacro(string const & name, int nargs);
explicit InsetFormulaMacro(string const & name, int nargs, string const & t);
/// constructs a mocro from its LaTeX definition
explicit InsetFormulaMacro(string const & s);
///

View File

@ -7,13 +7,13 @@
MathMacroTemplate::MathMacroTemplate()
: MathNestInset(2), numargs_(0), name_()
: MathNestInset(2), numargs_(0), name_(), type_("newcommand")
{}
MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
MathArray const & ar1, MathArray const & ar2)
: MathNestInset(2), numargs_(numargs), name_(nm)
string const & type, MathArray const & ar1, MathArray const & ar2)
: MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
{
if (numargs_ > 9)
lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
@ -86,21 +86,26 @@ void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
}
void MathMacroTemplate::write(WriteStream & os) const
{
if (os.latex()) {
os << "\n\\newcommand{\\" << name_.c_str() << '}';
if (numargs_ > 0)
os << '[' << numargs_ << ']';
os << '{' << cell(0) << "}\n";
if (type_ == "def") {
os << "\n\\def\\" << name_.c_str();
for (int i = 1; i <= numargs_; ++i)
os << '#' << i;
} else {
// writing .lyx
os << "\n\\newcommand{\\" << name_.c_str() << '}';
// newcommand or renewcommand
os << "\n\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
if (numargs_ > 0)
os << '[' << numargs_ << ']';
os << '{' << cell(0) << '}';
// write special .tex export only if necessary
}
os << '{' << cell(0) << "}";
if (os.latex()) {
// writing .tex. done.
os << "\n";
} else {
// writing .lyx, write special .tex export only if necessary
if (!cell(1).empty())
os << "\n{" << cell(1) << '}';
}

View File

@ -20,7 +20,7 @@ public:
///
MathMacroTemplate();
///
MathMacroTemplate(string const & name, int nargs,
MathMacroTemplate(string const & name, int nargs, string const & type,
MathArray const & = MathArray(), MathArray const & = MathArray());
///
explicit MathMacroTemplate(std::istream & is);
@ -47,6 +47,8 @@ private:
int numargs_;
///
string name_;
/// newcommand or renewcommand or def
string type_;
};
#endif

View File

@ -792,7 +792,11 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
cell->back().nucleus()->lock(true);
}
else if (t.cs() == "def" || t.cs() == "newcommand") {
else if (t.cs() == "def" ||
t.cs() == "newcommand" ||
t.cs() == "renewcommand")
{
string const type = t.cs();
string name;
int nargs = 0;
if (t.cs() == "def") {
@ -808,7 +812,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
nargs /= 2;
//lyxerr << "read \\def parameter list '" << pars << "'\n";
} else { // t.cs() == "newcommand"
} else { // t.cs() == "newcommand" || t.cs() == "renewcommand"
if (getToken().cat() != catBegin) {
error("'{' in \\newcommand expected (1) \n");
@ -845,7 +849,8 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
if (nextToken().cat() == catBegin)
parse(ar2, FLAG_ITEM, MathInset::MATH_MODE);
cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, ar1, ar2)));
cell->push_back(MathAtom(new MathMacroTemplate(name, nargs, type,
ar1, ar2)));
}
else if (t.cs() == "(") {