From 11cee62d3515396bcfdcc618a868f33be6fe3bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Fri, 4 Jul 2003 15:55:18 +0000 Subject: [PATCH] allow 'renewcommand' and 'def' for math macros git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7241 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/formulabase.C | 6 ++++-- src/mathed/formulamacro.C | 5 +++-- src/mathed/formulamacro.h | 2 +- src/mathed/math_macrotemplate.C | 31 ++++++++++++++++++------------- src/mathed/math_macrotemplate.h | 4 +++- src/mathed/math_parser.C | 11 ++++++++--- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 1ad521a45e..aed61cef26 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -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; diff --git a/src/mathed/formulamacro.C b/src/mathed/formulamacro.C index da8943bacf..390643dcf8 100644 --- a/src/mathed/formulamacro.C +++ b/src/mathed/formulamacro.C @@ -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))); } diff --git a/src/mathed/formulamacro.h b/src/mathed/formulamacro.h index 41fd9bbf6e..1e4ce5d923 100644 --- a/src/mathed/formulamacro.h +++ b/src/mathed/formulamacro.h @@ -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); /// diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index 3f03eeeb8a..f200c46cd5 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -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) << '}'; } diff --git a/src/mathed/math_macrotemplate.h b/src/mathed/math_macrotemplate.h index 405eeb3311..8807cfe859 100644 --- a/src/mathed/math_macrotemplate.h +++ b/src/mathed/math_macrotemplate.h @@ -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 diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 06713ed70f..cc098d3ec6 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -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() == "(") {