From: André Pönitz Date: Tue, 13 Apr 2004 13:54:58 +0000 (+0000) Subject: partial revert of macro stuff X-Git-Tag: 1.6.10~15318 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=0ea524145a00c57df49ab1bebafdc74b19a23813;p=features.git partial revert of macro stuff git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8648 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/buffer.C b/src/buffer.C index 5c4139974a..e6e5d02a63 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -641,6 +641,9 @@ bool Buffer::readFile(LyXLex & lex, string const & filename, par_type pit) } bool the_end = readBody(lex); + //lyxerr << "removing " << MacroTable::localMacros().size() + // << " temporary macro entries" << endl; + //MacroTable::localMacros().clear(); params().setPaperStuff(); #ifdef WITH_WARNINGS @@ -1499,6 +1502,7 @@ bool Buffer::hasMacro(string const & name) const void Buffer::insertMacro(string const & name, MacroData const & data) { + MacroTable::globalMacros().insert(name, data); pimpl_->macros.insert(name, data); } diff --git a/src/cursor.C b/src/cursor.C index 9ae8bee4cb..d8cc1ba53a 100644 --- a/src/cursor.C +++ b/src/cursor.C @@ -870,12 +870,7 @@ void LCursor::macroModeClose() if (macro && macro->getInsetName() == name) lyxerr << "can't enter recursive macro" << endl; - plainInsert(createMathInset(name)); - if (buffer().hasMacro(name)) { - MacroData const & tmpl = buffer().getMacro(name); - for (int i = 0; i < tmpl.numargs(); ++i) - cell().insert(pos(), MathAtom(new MathBraceInset)); - } + niceInsert(createMathInset(name)); } diff --git a/src/factory.C b/src/factory.C index e42cb32101..3d478132f0 100644 --- a/src/factory.C +++ b/src/factory.C @@ -469,6 +469,15 @@ InsetBase * readInset(LyXLex & lex, Buffer const & buf) } inset->read(buf, lex); + +#warning hack.. + if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) { + MathMacroTemplate const * tmpl = + static_cast(inset.get()); + MacroTable::globalMacros().insert + (tmpl->name(), tmpl->asMacroData()); + lyxerr << "creating local macro " << tmpl->name() << endl; + } } return inset.release(); diff --git a/src/mathed/math_data.C b/src/mathed/math_data.C index 2a8f630119..b9e3e43d5a 100644 --- a/src/mathed/math_data.C +++ b/src/mathed/math_data.C @@ -249,10 +249,11 @@ void MathArray::metrics(MetricsInfo & mi) const dim_.wid = 0; Dimension d; - BufferView & bv = *mi.base.bv; - Buffer const & buf = *bv.buffer(); + //BufferView & bv = *mi.base.bv; + //Buffer const & buf = *bv.buffer(); for (size_t i = 0, n = size(); i != n; ++i) { MathAtom const & at = operator[](i); +#if 0 MathMacro const * mac = at->asMacro(); if (mac && buf.hasMacro(mac->name())) { MacroData const & tmpl = buf.getMacro(mac->name()); @@ -272,6 +273,7 @@ void MathArray::metrics(MetricsInfo & mi) const continue; } } +#endif at->metrics(mi, d); dim_ += d; } @@ -300,10 +302,11 @@ void MathArray::draw(PainterInfo & pi, int x, int y) const || x >= pi.pain.paperWidth()) return; - BufferView & bv = *pi.base.bv; - Buffer const & buf = *bv.buffer(); + //BufferView & bv = *pi.base.bv; for (size_t i = 0, n = size(); i != n; ++i) { MathAtom const & at = operator[](i); +#if 0 + Buffer const & buf = *bv.buffer(); // special macro handling MathMacro const * mac = at->asMacro(); if (mac && buf.hasMacro(mac->name())) { @@ -318,6 +321,7 @@ void MathArray::draw(PainterInfo & pi, int x, int y) const continue; } } +#endif at->drawSelection(pi, x, y); at->draw(pi, x, y); x += at->width(); diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index ba2d1d3522..acaa0ca69e 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -320,7 +320,14 @@ MathAtom createMathInset(string const & s) if (s == "dfrac") return MathAtom(new MathDfracInset); - return MathAtom(new MathMacro(s)); + if (MacroTable::globalMacros().has(s)) + return MathAtom(new MathMacro(s, + MacroTable::globalMacros().get(s).numargs())); + //if (MacroTable::localMacros().has(s)) + // return MathAtom(new MathMacro(s, + // MacroTable::localMacros().get(s).numargs())); + + return MathAtom(new MathUnknownInset(s)); } diff --git a/src/mathed/math_macro.C b/src/mathed/math_macro.C index d0d91646e1..f1e1f495e7 100644 --- a/src/mathed/math_macro.C +++ b/src/mathed/math_macro.C @@ -28,9 +28,8 @@ using std::auto_ptr; using std::endl; - -MathMacro::MathMacro(string const & name) - : name_(name) +MathMacro::MathMacro(string const & name, int numargs) + : MathNestInset(numargs), name_(name) {} @@ -46,53 +45,62 @@ string MathMacro::name() const } -void MathMacro::setExpansion(MathArray const & exp, MathArray const & arg) const -{ - expanded_ = exp; - args_ = arg; -} - - void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const { - LyXFont font = mi.base.font; - augmentFont(font, "lyxtex"); - mathed_string_dim(font, "\\" + name(), dim); - dim_ = dim; -} - - -void MathMacro::metricsExpanded(MetricsInfo & mi, Dimension & dim) const -{ - args_.metrics(mi); - expanded_.metrics(mi, dim); - dim.wid -= args_.size() ? args_.width() : 0; + if (!MacroTable::globalMacros().has(name())) { + mathed_string_dim(mi.base.font, "Unknown: " + name(), dim); + } else if (editing(mi.base.bv)) { + asArray(MacroTable::globalMacros().get(name()).def(), tmpl_); + LyXFont font = mi.base.font; + augmentFont(font, "lyxtex"); + tmpl_.metrics(mi, dim); + dim.wid += mathed_string_width(font, name()) + 10; + int ww = mathed_string_width(font, "#1: "); + for (idx_type i = 0; i < nargs(); ++i) { + MathArray const & c = cell(i); + c.metrics(mi); + dim.wid = max(dim.wid, c.width() + ww); + dim.des += c.height() + 10; + } + } else { + MacroTable::globalMacros().get(name()).expand(cells_, expanded_); + expanded_.metrics(mi, dim); + } + metricsMarkers2(dim); dim_ = dim; } void MathMacro::draw(PainterInfo & pi, int x, int y) const { - LyXFont font = pi.base.font; - augmentFont(font, "lyxtex"); - drawStr(pi, font, x, y, "\\" + name()); - setPosCache(pi, x, y); -} - - -void MathMacro::drawExpanded(PainterInfo & pi, int x, int y) const -{ - expanded_.draw(pi, x, y); + if (!MacroTable::globalMacros().has(name())) { + drawStrRed(pi, x, y, "Unknown: " + name()); + } else if (editing(pi.base.bv)) { + LyXFont font = pi.base.font; + augmentFont(font, "lyxtex"); + int h = y - dim_.ascent() + 2 + tmpl_.ascent(); + drawStr(pi, font, x + 3, h, name()); + int const w = mathed_string_width(font, name()); + tmpl_.draw(pi, x + w + 12, h); + h += tmpl_.descent(); + Dimension ldim; + mathed_string_dim(font, "#1: ", ldim); + for (idx_type i = 0; i < nargs(); ++i) { + MathArray const & c = cell(i); + h += max(c.ascent(), ldim.asc) + 5; + c.draw(pi, x + ldim.wid, h); + char str[] = "#1:"; + str[1] += static_cast(i); + drawStr(pi, font, x + 3, h, str); + h += max(c.descent(), ldim.des) + 5; + } + } else { + expanded_.draw(pi, x, y); + } drawMarkers2(pi, x, y); } -int MathMacro::widthExpanded() const -{ - return expanded_.width(); -} - - void MathMacro::validate(LaTeXFeatures & features) const { if (name() == "binom" || name() == "mathcircumflex") @@ -123,8 +131,6 @@ void MathMacro::octave(OctaveStream & os) const void MathMacro::updateExpansion() const { -#warning FIXME - //expand(); //expanded_.substitute(*this); } diff --git a/src/mathed/math_macro.h b/src/mathed/math_macro.h index a1637a7607..8056c99175 100644 --- a/src/mathed/math_macro.h +++ b/src/mathed/math_macro.h @@ -15,30 +15,24 @@ #include "math_nestinset.h" #include "math_data.h" +#include "math_nestinset.h" +#include "math_macrotable.h" /// This class contains the data for a macro. -class MathMacro : public MathDimInset { +class MathMacro : public MathNestInset { public: /// A macro can be built from an existing template - explicit MathMacro(std::string const &); + MathMacro(std::string const & name, int numargs); /// std::auto_ptr clone() const; /// - MathMacro * asMacro() { return this; } - /// - MathMacro const * asMacro() const { return this; } - /// void draw(PainterInfo & pi, int x, int y) const; /// void drawExpanded(PainterInfo & pi, int x, int y) const; /// void metrics(MetricsInfo & mi, Dimension & dim) const; /// - void metricsExpanded(MetricsInfo & mi, Dimension & dim) const; - /// - int widthExpanded() const; - /// std::string name() const; /// void setExpansion(MathArray const & exp, MathArray const & args) const; @@ -60,13 +54,15 @@ public: private: /// void updateExpansion() const; - /// + void expand() const; + + /// name of macro std::string name_; - /// + /// the unexpanded macro defintition + mutable MathArray tmpl_; + /// the matcro substituted with our args mutable MathArray expanded_; - /// - mutable MathArray args_; }; diff --git a/src/mathed/math_macrotable.C b/src/mathed/math_macrotable.C index 952b4e4861..fe5d668965 100644 --- a/src/mathed/math_macrotable.C +++ b/src/mathed/math_macrotable.C @@ -41,7 +41,7 @@ MacroData::MacroData(string const & def, int numargs, string const & disp) {} -void MacroData::expand(MathArray const & args, MathArray & to) const +void MacroData::expand(vector const & args, MathArray & to) const { MathSqrtInset inset; // Hack. Any inset with a cell would do. asArray(disp_.empty() ? def_ : disp_, inset.cell(0)); @@ -56,12 +56,8 @@ void MacroData::expand(MathArray const & args, MathArray & to) const //it.cell().insert(it.pos(), it.nextInset()->asMathInset() int n = static_cast(it.nextInset())->number(); if (n <= args.size()) { - if (args[n - 1]->asBraceInset()) { - it.cell().erase(it.pos()); - it.cell().insert(it.pos(), args[n - 1]->cell(0)); - } else { - it.cell()[it.pos()] = args[n - 1]; - } + it.cell().erase(it.pos()); + it.cell().insert(it.pos(), args[n - 1]); } } //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl; @@ -78,30 +74,38 @@ MacroTable & MacroTable::globalMacros() } +// The local table. +//MacroTable & MacroTable::localMacros() +//{ +// static MacroTable theLocalMacros; +// return theLocalMacros; +//} + + bool MacroTable::has(string const & name) const { - return macros_.find(name) != macros_.end(); + return find(name) != end(); } MacroData const & MacroTable::get(string const & name) const { - table_type::const_iterator it = macros_.find(name); - BOOST_ASSERT(it != macros_.end()); + const_iterator it = find(name); + BOOST_ASSERT(it != end()); return it->second; } void MacroTable::insert(string const & name, MacroData const & data) { - lyxerr << "MathMacroTable::insert: " << name << endl; - macros_[name] = data; + //lyxerr << "MacroTable::insert: " << name << endl; + operator[](name) = data; } void MacroTable::insert(string const & def) { - //lyxerr << "MathMacroTable::insert, def: " << def << endl; + //lyxerr << "MacroTable::insert, def: " << def << endl; istringstream is(def); MathMacroTemplate mac(is); insert(mac.name(), mac.asMacroData()); @@ -111,8 +115,7 @@ void MacroTable::insert(string const & def) void MacroTable::dump() { lyxerr << "\n------------------------------------------" << endl; - table_type::const_iterator it; - for (it = macros_.begin(); it != macros_.end(); ++it) + for (const_iterator it = begin(); it != end(); ++it) lyxerr << it->first << " [" << it->second.def() << "] : " << " [" << it->second.disp() << "] : " diff --git a/src/mathed/math_macrotable.h b/src/mathed/math_macrotable.h index 1dfac3d717..8f5abfb57e 100644 --- a/src/mathed/math_macrotable.h +++ b/src/mathed/math_macrotable.h @@ -18,6 +18,7 @@ class MathArray; + /// class MacroData { public: @@ -32,7 +33,7 @@ public: /// int numargs() const { return numargs_; } /// replace #1,#2,... by given MathAtom 0,1,.. - void expand(MathArray const & from, MathArray & to) const; + void expand(std::vector const & from, MathArray & to) const; private: /// @@ -48,7 +49,8 @@ private: // either because they implement a feature of standard LaTeX or some // hack to display certain contents nicely. -class MacroTable { +class MacroTable : public std::map +{ public: /// Parse full "\def..." or "\newcommand..." or ... void insert(std::string const & definition); @@ -63,13 +65,8 @@ public: /// the global list static MacroTable & globalMacros(); - -private: - /// - typedef std::map table_type; - - /// - table_type macros_; + /// the local list hack + //static MacroTable & localMacros(); }; #endif diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index 708b554be8..697a58a635 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -70,7 +70,7 @@ auto_ptr MathMacroTemplate::clone() const } -void MathMacroTemplate::edit(LCursor & cur, bool left) +void MathMacroTemplate::edit(LCursor & cur, bool) { lyxerr << "MathMacroTemplate: edit left/right" << endl; cur.push(*this); diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index f5fd396069..89ed19a208 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -191,7 +191,6 @@ ostream & operator<<(ostream & os, Token const & t) class Parser { - public: /// typedef MathInset::mode_type mode_type; diff --git a/src/mathed/math_unknowninset.C b/src/mathed/math_unknowninset.C index dd1284027f..c304b4b9e2 100644 --- a/src/mathed/math_unknowninset.C +++ b/src/mathed/math_unknowninset.C @@ -84,6 +84,7 @@ void MathUnknownInset::maple(MapleStream & os) const os << name_; } + void MathUnknownInset::mathematica(MathematicaStream & os) const { os << name_;