]> git.lyx.org Git - lyx.git/commitdiff
partial revert of macro stuff
authorAndré Pönitz <poenitz@gmx.net>
Tue, 13 Apr 2004 13:54:58 +0000 (13:54 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Tue, 13 Apr 2004 13:54:58 +0000 (13:54 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8648 a592a061-630c-0410-9148-cb99ea01b6c8

12 files changed:
src/buffer.C
src/cursor.C
src/factory.C
src/mathed/math_data.C
src/mathed/math_factory.C
src/mathed/math_macro.C
src/mathed/math_macro.h
src/mathed/math_macrotable.C
src/mathed/math_macrotable.h
src/mathed/math_macrotemplate.C
src/mathed/math_parser.C
src/mathed/math_unknowninset.C

index 5c4139974a969722d7b09e61165637525327e81a..e6e5d02a63cadc38f3243dd9b2aef4b5975395a1 100644 (file)
@@ -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);
 }
 
index 9ae8bee4cb669d68d86eba25d2d531692169d462..d8cc1ba53a760d2670e17386c4d9cb9a5a3aabda 100644 (file)
@@ -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 e42cb3210149d7d482f4d045327014d67d89a69c..3d478132f0dbefe6b7101ae5d6f9c6ddd5d68a30 100644 (file)
@@ -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<MathMacroTemplate*>(inset.get());
+                       MacroTable::globalMacros().insert
+                               (tmpl->name(), tmpl->asMacroData());
+                       lyxerr << "creating local macro " << tmpl->name() << endl;
+               }
        }
 
        return inset.release();
index 2a8f630119ed8dde94346256b0069d1825a91a4b..b9e3e43d5aa8a68b4a4e17a4e931bd0135b98b57 100644 (file)
@@ -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();
index ba2d1d35229ca5f0d3f3906e22a549ca9b16b0aa..acaa0ca69ea37fe276a16ff1c0869bba4fdf3116 100644 (file)
@@ -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));
 }
 
 
index d0d91646e1b48737faa63f8ed0ea3eca40dc7d15..f1e1f495e79c65f3e572988eeaec8fa818a4f98d 100644 (file)
@@ -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) const
 
 void MathMacro::updateExpansion() const
 {
-#warning FIXME
-       //expand();
        //expanded_.substitute(*this);
 }
 
index a1637a7607de4447a88cadbccd060dfc72c12128..8056c99175c658e89db9a94df580c5fc1405fe0b 100644 (file)
 
 #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 952b4e4861e1c85780fd294715680a65accd8891..fe5d668965b65fc51286976ac90942144a1bb0ac 100644 (file)
@@ -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<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 & args, MathArray & to) 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 & 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() << "] : "
index 1dfac3d71751536f88e27a275dfe57e9fd62e7d1..8f5abfb57e4f860b3c4c6eadd3661bda36b3da8b 100644 (file)
@@ -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 708b554be8b557a26d8abfe913a839800a29310a..697a58a635d24116a6acd89d7a26cf9e5f2d1b6b 100644 (file)
@@ -70,7 +70,7 @@ auto_ptr<InsetBase> 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);
index f5fd3960691ea0190b722a019ed5b5dba84e9cf4..89ed19a2088fd6c75bd5bdc615bdc71883bda6a6 100644 (file)
@@ -191,7 +191,6 @@ ostream & operator<<(ostream & os, Token const & t)
 
 
 class Parser {
-
 public:
        ///
        typedef  MathInset::mode_type mode_type;
index dd1284027fecb92811963f7bf72b9bec715c5470..c304b4b9e2a9f7e2bd8264b12fc23784a1c5beaf 100644 (file)
@@ -84,6 +84,7 @@ void MathUnknownInset::maple(MapleStream & os) const
        os << name_;
 }
 
+
 void MathUnknownInset::mathematica(MathematicaStream & os) const
 {
        os << name_;