Well... as I said I have (in theory...) no time until Friday to even
spend a thought on this problem. So I'll just revert most of this stuff
and have a look at it later.

I'll try to create a diff and attach this to this mail, would be nice if
someone could attach it to one of the macor related bugs on bugzilla
lest it gets lost.

The version I commit now should have old behaviour (i.e. all bug and
niceties present), but already decouples macro definition and usage, so
this is about half of the needed work in either direection.

Andre'
? .lyxfunc.C.swo
? .lyxlength.h.swp
? 1
? 1.diff
? insets/C
? mathed/1.diff
Index: buffer.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/buffer.C,v
retrieving revision 1.572
diff -u -p -r1.572 buffer.C
--- buffer.C    13 Apr 2004 06:27:25 -0000      1.572
+++ buffer.C    13 Apr 2004 13:46:44 -0000
@@ -641,6 +641,9 @@ bool Buffer::readFile(LyXLex & lex, stri
        }
 
        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 & nam
 
 void Buffer::insertMacro(string const & name, MacroData const & data)
 {
+       MacroTable::globalMacros().insert(name, data);
        pimpl_->macros.insert(name, data);
 }
 
Index: cursor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.C,v
retrieving revision 1.96
diff -u -p -r1.96 cursor.C
--- cursor.C    13 Apr 2004 12:47:48 -0000      1.96
+++ cursor.C    13 Apr 2004 13:46:54 -0000
@@ -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));      
 }
 
 
Index: factory.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/factory.C,v
retrieving revision 1.90
diff -u -p -r1.90 factory.C
--- factory.C   13 Apr 2004 06:27:26 -0000      1.90
+++ factory.C   13 Apr 2004 13:47:02 -0000
@@ -469,6 +469,15 @@ InsetBase * readInset(LyXLex & lex, Buff
                }
 
                inset->read(buf, lex);
+       
+#warning hack..        
+               if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
+                       MathMacroTemplate const * tmpl =
+                               static_cast<MathMacroTemplate*>(inset.get());
+                       MacroTable::globalMacros().insert
+                               (tmpl->name(), tmpl->asMacroData());
+                       lyxerr << "creating local macro " << tmpl->name() << endl;
+               }
        }
 
        return inset.release();
Index: mathed/math_data.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_data.C,v
retrieving revision 1.50
diff -u -p -r1.50 math_data.C
--- mathed/math_data.C  13 Apr 2004 06:27:28 -0000      1.50
+++ mathed/math_data.C  13 Apr 2004 13:48:23 -0000
@@ -249,10 +249,11 @@ void MathArray::metrics(MetricsInfo & mi
 
        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
                                continue;
                        }
                }
+#endif
                at->metrics(mi, d);
                dim_ += d;
        }
@@ -300,10 +302,11 @@ void MathArray::draw(PainterInfo & pi, i
                || 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, i
                                continue;
                        }
                }
+#endif
                at->drawSelection(pi, x, y);
                at->draw(pi, x, y);
                x += at->width();
Index: mathed/math_factory.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_factory.C,v
retrieving revision 1.96
diff -u -p -r1.96 math_factory.C
--- mathed/math_factory.C       13 Apr 2004 06:27:28 -0000      1.96
+++ mathed/math_factory.C       13 Apr 2004 13:48:27 -0000
@@ -320,7 +320,14 @@ MathAtom createMathInset(string const & 
        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));
 }
 
 
Index: mathed/math_macro.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_macro.C,v
retrieving revision 1.132
diff -u -p -r1.132 math_macro.C
--- mathed/math_macro.C 13 Apr 2004 06:27:29 -0000      1.132
+++ mathed/math_macro.C 13 Apr 2004 13:48:30 -0000
@@ -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<char>(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
 
 void MathMacro::updateExpansion() const
 {
-#warning FIXME
-       //expand();
        //expanded_.substitute(*this);
 }
 
Index: mathed/math_macro.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_macro.h,v
retrieving revision 1.99
diff -u -p -r1.99 math_macro.h
--- mathed/math_macro.h 13 Apr 2004 06:27:29 -0000      1.99
+++ mathed/math_macro.h 13 Apr 2004 13:48:30 -0000
@@ -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<InsetBase> 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_;
 };
 
 
Index: mathed/math_macrotable.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_macrotable.C,v
retrieving revision 1.70
diff -u -p -r1.70 math_macrotable.C
--- mathed/math_macrotable.C    13 Apr 2004 06:27:29 -0000      1.70
+++ mathed/math_macrotable.C    13 Apr 2004 13:48:30 -0000
@@ -41,7 +41,7 @@ MacroData::MacroData(string const & def,
 {}
 
 
-void MacroData::expand(MathArray const & args, MathArray & to) const
+void MacroData::expand(vector<MathArray> 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 &
                //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
                int n = static_cast<MathMacroArgument*>(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 & d
 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() << "] : "
Index: mathed/math_macrotable.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_macrotable.h,v
retrieving revision 1.21
diff -u -p -r1.21 math_macrotable.h
--- mathed/math_macrotable.h    13 Apr 2004 06:27:29 -0000      1.21
+++ mathed/math_macrotable.h    13 Apr 2004 13:48:32 -0000
@@ -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<MathArray> 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<std::string, MacroData>
+{
 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<std::string, MacroData> table_type;
-
-       ///
-       table_type macros_;
+       /// the local list hack
+       //static MacroTable & localMacros();
 };
 
 #endif
Index: mathed/math_macrotemplate.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_macrotemplate.C,v
retrieving revision 1.68
diff -u -p -r1.68 math_macrotemplate.C
--- mathed/math_macrotemplate.C 13 Apr 2004 06:27:29 -0000      1.68
+++ mathed/math_macrotemplate.C 13 Apr 2004 13:48:32 -0000
@@ -70,7 +70,7 @@ auto_ptr<InsetBase> MathMacroTemplate::c
 }
 
 
-void MathMacroTemplate::edit(LCursor & cur, bool left)
+void MathMacroTemplate::edit(LCursor & cur, bool)
 {
        lyxerr << "MathMacroTemplate: edit left/right" << endl;
        cur.push(*this);
Index: mathed/math_parser.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_parser.C,v
retrieving revision 1.297
diff -u -p -r1.297 math_parser.C
--- mathed/math_parser.C        13 Apr 2004 06:27:29 -0000      1.297
+++ mathed/math_parser.C        13 Apr 2004 13:48:43 -0000
@@ -191,7 +191,6 @@ ostream & operator<<(ostream & os, Token
 
 
 class Parser {
-
 public:
        ///
        typedef  MathInset::mode_type mode_type;
Index: mathed/math_unknowninset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_unknowninset.C,v
retrieving revision 1.28
diff -u -p -r1.28 math_unknowninset.C
--- mathed/math_unknowninset.C  3 Feb 2004 11:21:08 -0000       1.28
+++ mathed/math_unknowninset.C  13 Apr 2004 13:48:43 -0000
@@ -84,6 +84,7 @@ void MathUnknownInset::maple(MapleStream
        os << name_;
 }
 
+
 void MathUnknownInset::mathematica(MathematicaStream & os) const
 {
        os << name_;

Reply via email to