From 18a610d4d6ba137eadee1a8088a03b72e6eaeffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Fri, 3 Aug 2001 09:54:48 +0000 Subject: [PATCH] cloase a leak; remove a few casts; cosmetics git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2410 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/BUGS | 13 +++ src/mathed/ChangeLog | 3 + src/mathed/formula.C | 85 ++++++++++--------- src/mathed/formula.h | 8 +- src/mathed/formulabase.C | 54 ++++-------- src/mathed/formulabase.h | 12 +-- src/mathed/formulamacro.C | 145 +++++++++++++++++++------------- src/mathed/formulamacro.h | 10 ++- src/mathed/math_cursor.C | 2 +- src/mathed/math_inset.C | 37 +++++--- src/mathed/math_inset.h | 13 ++- src/mathed/math_macrotable.C | 4 +- src/mathed/math_macrotable.h | 4 +- src/mathed/math_macrotemplate.C | 8 +- src/mathed/math_parser.C | 108 +++++++++++++++++------- src/mathed/math_parser.h | 13 ++- 16 files changed, 310 insertions(+), 209 deletions(-) diff --git a/src/mathed/BUGS b/src/mathed/BUGS index ce3d276abf..419221a859 100644 --- a/src/mathed/BUGS +++ b/src/mathed/BUGS @@ -32,6 +32,10 @@ General hints for bug reports: ---------------------------------------------------------------------- +"R. Lahaye" +- Mathematical \frac with subscripts, for example: + \begin_inset Formula $\frac{\alpha _{1} }{\beta _{2} }$ + is wrongly displayed Dekel: @@ -355,3 +359,12 @@ Jean-Marc: Angus: - make math lables editable + +Tuukka: + +- An inline equation does not get a number, so putting a label there is not + overly sensible... + Ok. In that case show a message in the bottom similarly as the message + "you cannot type two spaces this way". Eg. "You can insert math labels + only in display mode" + diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index c4615360f1..fef8d59e5b 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -5,6 +5,9 @@ * math_matrixinset.C: fix mis-alignment of eqnarray columns + * formula*.[Ch]: seperation of the "pimpl" MathInset * into + MathMatrixInset * and MathMacroTemplate * to save a few casts + 2001-07-25 André Pönitz * formulabase.C: re-enable 'space enlargement' feature diff --git a/src/mathed/formula.C b/src/mathed/formula.C index 940ef7c223..5802cfa4df 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -51,22 +51,28 @@ extern MathCursor * mathcursor; InsetFormula::InsetFormula() - : InsetFormulaBase(new MathMatrixInset) + : par_(new MathMatrixInset) {} InsetFormula::InsetFormula(MathInsetTypes t) - : InsetFormulaBase(new MathMatrixInset(t)) + : par_(new MathMatrixInset(t)) {} -InsetFormula::InsetFormula(string const & s) - : InsetFormulaBase(mathed_parse(s)) +InsetFormula::InsetFormula(string const & s) + : par_(mathed_parse_normal(s)) { metrics(); } +InsetFormula::~InsetFormula() +{ + delete par_; +} + + Inset * InsetFormula::clone(Buffer const &, bool) const { return new InsetFormula(*this); @@ -82,14 +88,14 @@ void InsetFormula::write(ostream & os) const int InsetFormula::latex(ostream & os, bool fragile, bool) const { - par()->write(os, fragile); + par_->write(os, fragile); return 1; } int InsetFormula::ascii(ostream & os, int) const { - par()->write(os, false); + par_->write(os, false); return 1; } @@ -108,7 +114,7 @@ int InsetFormula::docBook(ostream & os) const void InsetFormula::read(LyXLex & lex) { - par(mathed_parse(lex)); + par(mathed_parse_normal(lex)); metrics(); } @@ -123,9 +129,9 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &, Painter & pain = bv->painter(); metrics(); - int w = par()->width(); - int h = par()->height(); - int a = par()->ascent(); + int w = par_->width(); + int h = par_->height(); + int a = par_->ascent(); pain.fillRectangle(x, y - a, w, h, LColor::mathbg); if (mathcursor && mathcursor->formula() == this) { @@ -133,8 +139,8 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &, pain.rectangle(x, y - a, w, h, LColor::mathframe); } - par()->draw(pain, x, y); - xx += par()->width(); + par_->draw(pain, x, y); + xx += par_->width(); setCursorVisible(false); } @@ -142,12 +148,13 @@ void InsetFormula::draw(BufferView * bv, LyXFont const &, void InsetFormula::metrics() const { - const_cast(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT); + const_cast(par_) + -> metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT); } vector const InsetFormula::getLabelList() const { - return par()->getLabelList(); + return par_->getLabelList(); } @@ -174,9 +181,9 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, //lyxerr << "toggling all numbers\n"; if (display()) { bv->lockedInsetStoreUndo(Undo::INSERT); - bool old = par()->numberedType(); - for (int row = 0; row < par()->nrows(); ++row) - par()->numbered(row, !old); + bool old = par_->numberedType(); + for (int row = 0; row < par_->nrows(); ++row) + par_->numbered(row, !old); bv->owner()->message(old ? _("No number") : _("Number")); updateLocal(bv, true); } @@ -189,9 +196,9 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, if (display()) { bv->lockedInsetStoreUndo(Undo::INSERT); int row = mathcursor->row(); - bool old = par()->numbered(row); + bool old = par_->numbered(row); bv->owner()->message(old ? _("No number") : _("Number")); - par()->numbered(row, !old); + par_->numbered(row, !old); updateLocal(bv, true); } break; @@ -202,7 +209,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, bv->lockedInsetStoreUndo(Undo::INSERT); int row = mathcursor->row(); - string old_label = par()->label(row); + string old_label = par_->label(row); string new_label = arg; if (new_label.empty()) { @@ -223,13 +230,13 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, if (!new_label.empty()) { lyxerr << "setting label to '" << new_label << "'\n"; - par()->numbered(row, true); + par_->numbered(row, true); } if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label)) bv->redraw(); - par()->label(row, new_label); + par_->label(row, new_label); updateLocal(bv, true); break; @@ -247,7 +254,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, int x; int y; mathcursor->getPos(x, y); - par()->mutate(arg); + par_->mutate(arg); mathcursor->setPos(x, y); mathcursor->normalize(); updateLocal(bv, true); @@ -259,10 +266,10 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, int x; int y; mathcursor->getPos(x, y); - if (par()->getType() == LM_OT_SIMPLE) - par()->mutate(LM_OT_EQUATION); + if (par_->getType() == LM_OT_SIMPLE) + par_->mutate(LM_OT_EQUATION); else - par()->mutate(LM_OT_SIMPLE); + par_->mutate(LM_OT_SIMPLE); mathcursor->setPos(x, y); mathcursor->normalize(); updateLocal(bv, true); @@ -273,7 +280,7 @@ InsetFormula::localDispatch(BufferView * bv, kb_action action, { string const clip = bv->getClipboard(); if (!clip.empty()) - par(mathed_parse(clip)); + par(mathed_parse_normal(clip)); break; } @@ -290,33 +297,33 @@ void InsetFormula::handleExtern(const string & arg, BufferView *) //string outfile = lyx::tempName("maple.out"); string outfile = "/tmp/lyx2" + arg + ".out"; ostringstream os; - par()->writeNormal(os); + par_->writeNormal(os); string code = os.str().c_str(); string script = "lyx2" + arg + " '" + code + "' " + outfile; lyxerr << "calling: " << script << endl; Systemcalls cmd(Systemcalls::System, script, 0); ifstream is(outfile.c_str()); - par(mathed_parse(is)); + par(mathed_parse_normal(is)); metrics(); } bool InsetFormula::display() const { - return par()->getType() != LM_OT_SIMPLE; + return par_->getType() != LM_OT_SIMPLE; } -MathMatrixInset * InsetFormula::par() const +MathInset * InsetFormula::par() const { - return static_cast(par_); + return par_; } -void InsetFormula::par(MathInset * p) +void InsetFormula::par(MathMatrixInset * p) { delete par_; - par_ = p ? p : new MathMatrixInset; + par_ = p ? static_cast(p) : new MathMatrixInset; } @@ -328,7 +335,7 @@ Inset::Code InsetFormula::lyxCode() const void InsetFormula::validate(LaTeXFeatures & features) const { - par()->validate(features); + par_->validate(features); } bool InsetFormula::insetAllowed(Inset::Code code) const @@ -339,24 +346,24 @@ bool InsetFormula::insetAllowed(Inset::Code code) const int InsetFormula::ascent(BufferView *, LyXFont const &) const { - return par()->ascent() + 1; + return par_->ascent() + 1; } int InsetFormula::descent(BufferView *, LyXFont const &) const { - return par()->descent() + 1; + return par_->descent() + 1; } int InsetFormula::width(BufferView *, LyXFont const &) const { metrics(); - return par()->width(); + return par_->width(); } MathInsetTypes InsetFormula::getType() const { - return par()->getType();; + return par_->getType();; } diff --git a/src/mathed/formula.h b/src/mathed/formula.h index 69a34579eb..30be0a5d2a 100644 --- a/src/mathed/formula.h +++ b/src/mathed/formula.h @@ -36,6 +36,8 @@ public: /// explicit InsetFormula(string const &); /// + ~InsetFormula(); + /// int ascent(BufferView *, LyXFont const &) const; /// int descent(BufferView *, LyXFont const &) const; @@ -75,7 +77,7 @@ public: /// void handleExtern(string const & arg, BufferView * bv); /// - MathMatrixInset * par() const; + MathInset * par() const; /// bool display() const; /// @@ -84,6 +86,8 @@ public: MathInsetTypes getType() const; private: /// Safe setting of contents - void par(MathInset *); + void par(MathMatrixInset *); + /// + MathMatrixInset * par_; }; #endif diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 972a0296bf..b595619cb8 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -50,7 +50,6 @@ using std::vector; extern char const * latex_special_chars; -int greek_kb_flag = 0; extern char const * latex_mathenv[]; MathCursor * mathcursor = 0; @@ -116,8 +115,7 @@ MathArrayInset * matrixpar(int & idx) -InsetFormulaBase::InsetFormulaBase(MathInset * par) - : par_(par) +InsetFormulaBase::InsetFormulaBase() { #ifdef WITH_WARNINGS #warning This is needed as long the math parser is not re-entrant @@ -127,22 +125,12 @@ InsetFormulaBase::InsetFormulaBase(MathInset * par) } -InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f) - : UpdatableInset(f), par_(f.par_->clone()) -{} - - -InsetFormulaBase::~InsetFormulaBase() -{ - delete par_; -} - - void InsetFormulaBase::read(Buffer const *, LyXLex & lex) { read(lex); } + void InsetFormulaBase::write(Buffer const *, ostream & os) const { write(os); @@ -204,6 +192,7 @@ void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int) void InsetFormulaBase::edit(BufferView * bv, bool front) { #warning Please have a look if this is right (Jug) +#warning Does not look wrong... although I do not know what it is supposed to do (Andre) edit(bv, front ? 0 : 1, 0, 0); } @@ -225,8 +214,8 @@ void InsetFormulaBase::insetUnlock(BufferView * bv) void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const { mathcursor->getPos(x, y); - x -= par_->xo(); - y -= par_->yo(); + x -= par()->xo(); + y -= par()->yo(); } @@ -241,8 +230,8 @@ void InsetFormulaBase::toggleInsetCursor(BufferView * bv) int x; int y; mathcursor->getPos(x, y); - //x -= par_->xo(); - y -= par_->yo(); + //x -= par()->xo(); + y -= par()->yo(); int asc; int desc; math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc); @@ -260,8 +249,8 @@ void InsetFormulaBase::showInsetCursor(BufferView * bv, bool) int x; int y; mathcursor->getPos(x, y); - x -= par_->xo(); - y -= par_->yo(); + x -= par()->xo(); + y -= par()->yo(); int asc; int desc; math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc); @@ -299,19 +288,13 @@ void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty) } -void InsetFormulaBase::metrics() const -{ - const_cast(par_)->metrics(LM_ST_TEXT); -} - - void InsetFormulaBase::insetButtonRelease(BufferView * bv, int x, int y, int /*button*/) { if (mathcursor) { hideInsetCursor(bv); - x += par_->xo(); - y += par_->yo(); + x += par()->xo(); + y += par()->yo(); mathcursor->setPos(x, y); showInsetCursor(bv); if (sel_flag) { @@ -343,14 +326,14 @@ void InsetFormulaBase::insetMotionNotify(BufferView * bv, if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) { sel_flag = true; hideInsetCursor(bv); - mathcursor->setPos(sel_x + par_->xo(), sel_y + par_->yo()); + mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo()); mathcursor->selStart(); showInsetCursor(bv); mathcursor->getPos(sel_x, sel_y); } else if (sel_flag) { hideInsetCursor(bv); - x += par_->xo(); - y += par_->yo(); + x += par()->xo(); + y += par()->yo(); mathcursor->setPos(x, y); showInsetCursor(bv); mathcursor->getPos(x, y); @@ -376,6 +359,8 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action, //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action // << " arg: '" << arg << "' cursor: " << mathcursor << "\n"; + static int greek_kb_flag = 0; + if (!mathcursor) return UNDISPATCHED; @@ -499,7 +484,7 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action, int y1; istringstream is(arg.c_str()); is >> x >> y; - par_->getXY(x1, y1); + par()->getXY(x1, y1); mathcursor->setPos(x1 + x, y1 + y); updateLocal(bv, false); } @@ -926,11 +911,6 @@ LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const return font; } -MathInset * InsetFormulaBase::par() const -{ - return par_; -} - void mathDispatchCreation(BufferView * bv, string const & arg, bool display) { diff --git a/src/mathed/formulabase.h b/src/mathed/formulabase.h index 80b56dc8df..dfcbd0c8f6 100644 --- a/src/mathed/formulabase.h +++ b/src/mathed/formulabase.h @@ -32,12 +32,8 @@ class MathInset; /// class InsetFormulaBase : public UpdatableInset { public: - /// - InsetFormulaBase(InsetFormulaBase const & p); /// - explicit InsetFormulaBase(MathInset *); - /// - virtual ~InsetFormulaBase(); + InsetFormulaBase(); /// virtual int ascent(BufferView *, LyXFont const &) const = 0; /// @@ -120,14 +116,12 @@ public: /// virtual std::vector const getLabelList() const; /// - MathInset * par() const; + virtual MathInset * par() const = 0; /// - virtual void metrics() const; + virtual void metrics() const = 0; protected: /// virtual void updateLocal(BufferView * bv, bool mark_dirty); - /// - MathInset * par_; private: /// unimplemented void operator=(const InsetFormulaBase &); diff --git a/src/mathed/formulamacro.C b/src/mathed/formulamacro.C index 688b920063..efd6c696a2 100644 --- a/src/mathed/formulamacro.C +++ b/src/mathed/formulamacro.C @@ -43,19 +43,32 @@ using std::ostream; extern MathCursor * mathcursor; InsetFormulaMacro::InsetFormulaMacro() - : InsetFormulaBase(new MathMacroTemplate("unknown", 0)) + : tmacro_(new MathMacroTemplate("unknown", 0)) {} InsetFormulaMacro::InsetFormulaMacro(string nm, int na) - : InsetFormulaBase(new MathMacroTemplate(nm, na)) + : tmacro_(new MathMacroTemplate(nm, na)) { - MathMacroTable::insertTemplate(tmacro()); + MathMacroTable::insertTemplate(tmacro_); +} + + +InsetFormulaMacro::~InsetFormulaMacro() +{ +#ifdef WITH_WARNINGS +#warning Need to unregister from MathMacroTable. +#endif + // Instead of unregister an delete leak this until it gets fixed + //delete tmacro_; } Inset * InsetFormulaMacro::clone(Buffer const &, bool) const { +#ifdef WITH_WARNINGS +#warning This should not be needed in reality... +#endif return new InsetFormulaMacro(*this); } @@ -63,20 +76,20 @@ Inset * InsetFormulaMacro::clone(Buffer const &, bool) const void InsetFormulaMacro::write(ostream & os) const { os << "FormulaMacro "; - tmacro()->write(os, false); + tmacro().write(os, false); } int InsetFormulaMacro::latex(ostream & os, bool fragile, bool /*free_spacing*/) const { - tmacro()->write(os, fragile); + tmacro().write(os, fragile); return 2; } int InsetFormulaMacro::ascii(ostream & os, int) const { - tmacro()->write(os, false); + tmacro().write(os, false); return 0; } @@ -96,35 +109,91 @@ int InsetFormulaMacro::docBook(ostream & os) const void InsetFormulaMacro::read(LyXLex & lex) { // Awful hack... - delete par_; - par_ = mathed_parse(lex); - MathMacroTable::insertTemplate(tmacro()); - par_->metrics(LM_ST_TEXT); + delete tmacro_; + tmacro_ = mathed_parse_macro(lex); + MathMacroTable::insertTemplate(tmacro_); + metrics(); } string InsetFormulaMacro::prefix() const { - return string(" ") + _("Macro: ") + tmacro()->name() + ": "; + return string(" ") + _("Macro: ") + tmacro().name() + ": "; } int InsetFormulaMacro::ascent(BufferView *, LyXFont const &) const { - return tmacro()->ascent() + 5; + return tmacro().ascent() + 5; } int InsetFormulaMacro::descent(BufferView *, LyXFont const &) const { - return tmacro()->descent() + 5; + return tmacro().descent() + 5; } int InsetFormulaMacro::width(BufferView *, LyXFont const & f) const { - tmacro()->metrics(LM_ST_TEXT); - return 10 + lyxfont::width(prefix(), f) + tmacro()->width(); + metrics(); + return 10 + lyxfont::width(prefix(), f) + tmacro().width(); +} + + + +UpdatableInset::RESULT +InsetFormulaMacro::localDispatch(BufferView * bv, + kb_action action, string const & arg) +{ + RESULT result = DISPATCHED; + switch (action) { + case LFUN_MATH_MACROARG: { + int const i = lyx::atoi(arg); + lyxerr << "inserting macro arg " << i << "\n"; + if (i > 0 && i <= tmacro().numargs()) { + mathcursor->insert(new MathMacroArgument(i)); + updateLocal(bv, true); + } else { + lyxerr << "not in range 0.." << tmacro().numargs() << "\n"; + } + break; + } + + default: + result = InsetFormulaBase::localDispatch(bv, action, arg); + } + return result; +} + + +MathMacroTemplate const & InsetFormulaMacro::tmacro() const +{ + return *tmacro_; +} + + +Inset::Code InsetFormulaMacro::lyxCode() const +{ + return Inset::MATHMACRO_CODE; +} + + +MathInsetTypes InsetFormulaMacro::getType() const +{ + return LM_OT_MACRO; +} + + +MathInset * InsetFormulaMacro::par() const +{ + return const_cast(tmacro_); +} + + +void InsetFormulaMacro::metrics() const +{ + par()->metrics(LM_ST_TEXT); } @@ -152,51 +221,9 @@ void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f, x += width(bv, font); // formula - float t = tmacro()->width() + 5; + float t = tmacro().width() + 5; x -= t; - tmacro()->draw(pain, int(x), baseline); + par()->draw(pain, int(x), baseline); x += t; } - -UpdatableInset::RESULT -InsetFormulaMacro::localDispatch(BufferView * bv, - kb_action action, string const & arg) -{ - RESULT result = DISPATCHED; - switch (action) { - case LFUN_MATH_MACROARG: { - int const i = lyx::atoi(arg); - lyxerr << "inserting macro arg " << i << "\n"; - if (i > 0 && i <= tmacro()->numargs()) { - mathcursor->insert(new MathMacroArgument(i)); - updateLocal(bv, true); - } else { - lyxerr << "not in range 0.." << tmacro()->numargs() << "\n"; - } - break; - } - - default: - result = InsetFormulaBase::localDispatch(bv, action, arg); - } - return result; -} - - -MathMacroTemplate * InsetFormulaMacro::tmacro() const -{ - return static_cast(par_); -} - - -Inset::Code InsetFormulaMacro::lyxCode() const -{ - return Inset::MATHMACRO_CODE; -} - - -MathInsetTypes InsetFormulaMacro::getType() const -{ - return LM_OT_MACRO; -} diff --git a/src/mathed/formulamacro.h b/src/mathed/formulamacro.h index 4616faaab5..a52f641923 100644 --- a/src/mathed/formulamacro.h +++ b/src/mathed/formulamacro.h @@ -37,6 +37,8 @@ public: /// explicit InsetFormulaMacro(string name, int na); /// + ~InsetFormulaMacro(); + /// int ascent(BufferView *, LyXFont const &) const; /// int descent(BufferView *, LyXFont const &) const; @@ -66,11 +68,17 @@ public: RESULT localDispatch(BufferView *, kb_action, string const &); /// MathInsetTypes getType() const; + /// + MathInset * par() const; + /// + void metrics() const; private: + /// + MathMacroTemplate const & tmacro() const; /// prefix in inset string prefix() const; /// - MathMacroTemplate * tmacro() const; + MathMacroTemplate const * tmacro_; }; #endif diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 073716d3b4..c788404cd4 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -588,7 +588,7 @@ bool MathCursor::toggleLimits() { if (!prevIsInset()) return false; - MathInset * p = prevInset(); + MathScriptInset * p = prevScriptInset(); int old = p->limits(); p->limits(old < 0 ? 1 : -1); return old != p->limits(); diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index 36362c28b3..aee6d232c9 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -28,7 +28,7 @@ int MathInset::workwidth; MathInset::MathInset(int nargs, string const & name) : name_(name), width_(0), ascent_(0), descent_(0), - size_(LM_ST_DISPLAY), cells_(nargs), xo_(0), yo_(0) + size_(LM_ST_DISPLAY), code_(LM_TC_MIN), cells_(nargs), xo_(0), yo_(0) {} @@ -56,16 +56,6 @@ int MathInset::height() const } -int MathInset::limits() const -{ - return false; -} - - -void MathInset::limits(int) -{} - - string const & MathInset::name() const { return name_; @@ -89,6 +79,7 @@ void MathInset::size(MathStyles s) size_ = s; } + std::ostream & operator<<(std::ostream & os, MathInset const & inset) { inset.write(os, false); @@ -126,24 +117,24 @@ int MathInset::nargs() const } - MathXArray & MathInset::xcell(int i) { return cells_[i]; } + MathXArray const & MathInset::xcell(int i) const { return cells_[i]; } - MathArray & MathInset::cell(int i) { return cells_[i].data_; } + MathArray const & MathInset::cell(int i) const { return cells_[i].data_; @@ -158,6 +149,7 @@ void MathInset::substitute(MathArray & array, MathMacro const & m) const array.push_back(p); } + void MathInset::metrics(MathStyles st) { size_ = st; @@ -165,6 +157,7 @@ void MathInset::metrics(MathStyles st) xcell(i).metrics(st); } + void MathInset::draw(Painter & pain, int x, int y) { xo_ = x; @@ -205,6 +198,7 @@ bool MathInset::idxLeft(int & idx, int & pos) const return idxPrev(idx, pos); } + bool MathInset::idxUp(int &, int &) const { return false; @@ -226,6 +220,7 @@ bool MathInset::idxFirst(int & i, int & pos) const return true; } + bool MathInset::idxLast(int & i, int & pos) const { if (nargs() == 0) @@ -266,12 +261,14 @@ bool MathInset::idxFirstDown(int &, int &) const return false; } + void MathInset::idxDelete(int &, bool & popit, bool & deleteit) { popit = false; deleteit = false; } + void MathInset::idxDeleteRange(int, int) {} @@ -349,12 +346,14 @@ bool MathInset::covers(int x, int y) const y <= yo_ + descent_; } + void MathInset::validate(LaTeXFeatures & features) const { for (int i = 0; i < nargs(); ++i) cell(i).validate(features); } + std::vector MathInset::idxBetween(int from, int to) const { std::vector res; @@ -362,3 +361,15 @@ std::vector MathInset::idxBetween(int from, int to) const res.push_back(i); return res; } + + +MathTextCodes MathInset::code() const +{ + return code_; +} + + +void MathInset::code(MathTextCodes t) +{ + code_ = t; +} diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index b36c67a91e..f054bb21d5 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -70,10 +70,6 @@ public: /// virtual int height() const; /// - virtual int limits() const; - /// - virtual void limits(int); - /// string const & name() const; /// virtual void setName(string const & n); @@ -181,6 +177,7 @@ public: virtual bool isArray() const { return false; } /// virtual bool isActive() const { return nargs() > 0; } + /// identifies insets that display scripts directly above and below /// @@ -195,6 +192,12 @@ public: /// static int workwidth; + + /// the inherited text style + MathTextCodes code() const; + /// + void code(MathTextCodes t); + protected: /// usually the LaTeX name of the thingy string name_; @@ -208,6 +211,8 @@ protected: void size(MathStyles s); /// the used font size MathStyles size_; + /// the inherited text style + MathTextCodes code_; protected: /// diff --git a/src/mathed/math_macrotable.C b/src/mathed/math_macrotable.C index 55928ab397..e054d218f9 100644 --- a/src/mathed/math_macrotable.C +++ b/src/mathed/math_macrotable.C @@ -32,9 +32,9 @@ void MathMacroTable::dump() } -void MathMacroTable::insertTemplate(MathMacroTemplate * p) +void MathMacroTable::insertTemplate(MathMacroTemplate const * p) { - macro_table[p->name()] = p; + macro_table[p->name()] = const_cast(p); } diff --git a/src/mathed/math_macrotable.h b/src/mathed/math_macrotable.h index bb4e725984..8c8ff4618a 100644 --- a/src/mathed/math_macrotable.h +++ b/src/mathed/math_macrotable.h @@ -17,9 +17,7 @@ class MathMacroTemplate; struct MathMacroTable { public: /// - static void updateTemplate(MathMacroTemplate *); - /// - static void insertTemplate(MathMacroTemplate *); + static void insertTemplate(MathMacroTemplate const *); /// static MathMacroTemplate & provideTemplate(string const &); /// diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index a1f50c6682..75655868f5 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -7,13 +7,13 @@ #include "debug.h" -MathMacroTemplate::MathMacroTemplate() : - MathInset(1, "undefined"), numargs_(0) +MathMacroTemplate::MathMacroTemplate() + : MathInset(1), numargs_(0) {} -MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs) : - MathInset(1, nm), numargs_(numargs) +MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs) + : MathInset(1, nm), numargs_(numargs) {} diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index f9f98675d1..09dfc423c2 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -85,7 +85,7 @@ void mathed_parse_into(MathArray & array, unsigned flags); unsigned char getuchar(std::istream * is) { - char c; + char c = 0; is->get(c); if (!is->good()) { lyxerr << "The input stream is not well..." << endl; @@ -124,7 +124,6 @@ union { string yytext; int yylineno; istream * yyis; -MathTextCodes yyvarcode; @@ -251,10 +250,7 @@ int yylex() unsigned char c = getuchar(yyis); //lyxerr << "reading byte: '" << c << "' code: " << lexcode[c] << endl; - if (yyvarcode == LM_TC_TEXTRM && c == ' ') { - yylval.i = ' '; - return LM_TK_ALPHA; - } else if (lexcode[c] == LexNewLine) { + if (lexcode[c] == LexNewLine) { ++yylineno; continue; } else if (lexcode[c] == LexComment) { @@ -266,7 +262,7 @@ int yylex() || lexcode[c] == LexMathSpace) { yylval.i = c; return LM_TK_STR; - } else if (lexcode[c] == LexAlpha) { + } else if (lexcode[c] == LexAlpha || lexcode[c] == LexSpace) { yylval.i = c; return LM_TK_ALPHA; } else if (lexcode[c] == LexBOP) { @@ -328,8 +324,11 @@ int yylex() yytext += c; c = getuchar(yyis); } - if (yyis->good()) + while (lexcode[c] == LexSpace && yyis->good()) + c = getuchar(yyis); + if (yyis->good() && lexcode[c] != LexSpace) yyis->putback(c); + //lyxerr[Debug::MATHED] << "reading: text '" << yytext << "'\n"; latexkeys const * l = in_word_set(yytext); if (!l) @@ -425,23 +424,28 @@ void mathed_parse_lines(MathInset * inset, int col, } -MathInset * mathed_parse() +MathMacroTemplate * mathed_parse_macro() { - MathInset * p = 0; + if (yylex() != LM_TK_NEWCOMMAND) { + lyxerr << "\\newcommand expected\n"; + return 0; + } + + string name = lexArg('{').substr(1); + string arg = lexArg('['); + int narg = arg.empty() ? 0 : atoi(arg.c_str()); + MathMacroTemplate * p = new MathMacroTemplate(name, narg); + mathed_parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST); + return p; +} + + +MathMatrixInset * mathed_parse_normal() +{ + MathMatrixInset * p = 0; int t = yylex(); switch (t) { - case LM_TK_NEWCOMMAND: { - string const name = lexArg('{').substr(1); - string const arg = lexArg('['); - int narg = arg.empty() ? 0 : atoi(arg.c_str()); - p = new MathMacroTemplate(name, narg); - mathed_parse_into(p->cell(0), FLAG_BRACE | FLAG_BRACE_LAST); - //lyxerr[Debug::MATHED] << "LM_TK_NEWCOMMAND: name: " - // << name << " nargs: " << narg << "\n"; - break; - } - case LM_TK_MATH: case LM_TK_BEGIN: { int i = yylval.i; @@ -522,7 +526,7 @@ void mathed_parse_into(MathArray & array, unsigned flags) static int plevel = -1; ++plevel; - yyvarcode = LM_TC_VAR; + MathTextCodes yyvarcode = LM_TC_VAR; int t = yylex(); bool panic = false; @@ -559,12 +563,16 @@ void mathed_parse_into(MathArray & array, unsigned flags) switch (t) { case LM_TK_ALPHA: - array.push_back(yylval.i, yyvarcode); + if (!isspace(yylval.i) || yyvarcode == LM_TC_TEXTRM) + array.push_back(yylval.i, yyvarcode); break; - case LM_TK_ARGUMENT: - array.push_back(new MathMacroArgument(yylval.i)); + case LM_TK_ARGUMENT: { + MathMacroArgument * p = new MathMacroArgument(yylval.i); + p->code(yyvarcode); + array.push_back(p); break; + } case LM_TK_SPECIAL: array.push_back(yylval.i, LM_TC_SPECIAL); @@ -846,27 +854,44 @@ MathArray mathed_parse_cell(string const & str) } -MathInset * mathed_parse(string const & str) +MathMacroTemplate * mathed_parse_macro(string const & str) { istringstream is(str.c_str()); - return mathed_parse(is); + return mathed_parse_macro(is); } -MathInset * mathed_parse(istream & is) +MathMatrixInset * mathed_parse_normal(string const & str) +{ + istringstream is(str.c_str()); + return mathed_parse_normal(is); +} + + + +MathMatrixInset * mathed_parse_normal(istream & is) { yyis = &is; yylineno = 0; - return mathed_parse(); + return mathed_parse_normal(); } -MathInset * mathed_parse(LyXLex & lex) +MathMacroTemplate * mathed_parse_macro(istream & is) +{ + yyis = &is; + yylineno = 0; + return mathed_parse_macro(); +} + + + +MathMatrixInset * mathed_parse_normal(LyXLex & lex) { yyis = &lex.getStream(); yylineno = lex.GetLineNo(); - MathInset * p = mathed_parse(); + MathMatrixInset * p = mathed_parse_normal(); // Update line number lex.setLineNo(yylineno); @@ -883,4 +908,25 @@ MathInset * mathed_parse(LyXLex & lex) return p; } +MathMacroTemplate * mathed_parse_macro(LyXLex & lex) +{ + yyis = &lex.getStream(); + yylineno = lex.GetLineNo(); + + MathMacroTemplate * p = mathed_parse_macro(); + + // Update line number + lex.setLineNo(yylineno); + + // reading of end_inset + while (lex.IsOK()) { + lex.nextToken(); + if (lex.GetString() == "\\end_inset") + break; + lyxerr[Debug::MATHED] << "InsetFormula::Read: Garbage before \\end_inset," + " or missing \\end_inset!" << endl; + } + + return p; +} //]}) diff --git a/src/mathed/math_parser.h b/src/mathed/math_parser.h index 90b558fe88..9b356fbec4 100644 --- a/src/mathed/math_parser.h +++ b/src/mathed/math_parser.h @@ -29,7 +29,8 @@ #include "symbol_def.h" class MathArray; -class MathInset; +class MathMatrixInset; +class MathMacroTemplate; class LyXLex; /// @@ -120,8 +121,12 @@ latexkeys const * in_word_set(string const & str); latexkeys const * lm_get_key_by_id(unsigned int id, short tc); -MathInset * mathed_parse(string const &); -MathInset * mathed_parse(std::istream &); -MathInset * mathed_parse(LyXLex &); +MathMatrixInset * mathed_parse_normal(string const &); +MathMatrixInset * mathed_parse_normal(std::istream &); +MathMatrixInset * mathed_parse_normal(LyXLex &); + +MathMacroTemplate * mathed_parse_macro(string const &); +MathMacroTemplate * mathed_parse_macro(std::istream &); +MathMacroTemplate * mathed_parse_macro(LyXLex &); #endif