]> git.lyx.org Git - features.git/commitdiff
mathed64.diff
authorLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 24 Apr 2001 16:13:38 +0000 (16:13 +0000)
committerLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 24 Apr 2001 16:13:38 +0000 (16:13 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@1950 a592a061-630c-0410-9148-cb99ea01b6c8

30 files changed:
src/mathed/array.C
src/mathed/array.h
src/mathed/formula.C
src/mathed/formula.h
src/mathed/formulamacro.C
src/mathed/formulamacro.h
src/mathed/macro_support.h
src/mathed/math_cursor.C
src/mathed/math_cursor.h
src/mathed/math_inset.C
src/mathed/math_inset.h
src/mathed/math_iter.C
src/mathed/math_iter.h
src/mathed/math_macro.C
src/mathed/math_macro.h
src/mathed/math_macroarg.C
src/mathed/math_macroarg.h
src/mathed/math_macrotable.C
src/mathed/math_macrotable.h
src/mathed/math_macrotemplate.C
src/mathed/math_macrotemplate.h
src/mathed/math_parinset.C
src/mathed/math_parinset.h
src/mathed/math_parser.C
src/mathed/math_rowst.C
src/mathed/math_rowst.h
src/mathed/math_sqrtinset.C
src/mathed/math_xiter.C
src/mathed/math_xiter.h
src/mathed/support.h

index 47034105ec6960909d42d6bf17434824dd9300a5..0cec697b09b2cac159cfd75d439c0ba75e19f167 100644 (file)
@@ -5,9 +5,11 @@
 #pragma implementation
 #endif
 
+#include "debug.h"
 #include "array.h"
 #include "math_iter.h"
 #include "math_inset.h"
+#include "math_macro.h"
 
 #include "support/LOstream.h"
 
@@ -79,6 +81,29 @@ void MathedArray::deep_copy()
        }
 }
 
+void MathedArray::substitute(MathMacro * m)
+{
+       if (m->nargs() == 0)
+               return;
+
+       MathedIter it(this);
+       while (it.OK()) {
+               if (it.IsInset()) {
+                       MathedInset * inset = it.GetInset();
+                       if (inset->GetType() == LM_OT_MACRO_ARG) {
+                               int n = static_cast<MathMacroArgument *>(inset)->number() - 1;
+                               //lyxerr << "substituting an argument inset: " << n << "\n";
+                               inset = m->arg(n)->Clone();
+                       } else {
+                               inset->substitute(m);
+                               //lyxerr << "substituting in an ordinary inset\n";
+                       }
+                       raw_pointer_insert(inset, it.getPos() + 1);
+               }
+               it.Next();
+       }
+}
+
 
 MathedArray & MathedArray::operator=(MathedArray const & array)
 {
@@ -284,7 +309,7 @@ byte & MathedArray::operator[](int i)
 }
 
 
-void MathedArray::dump(ostream & os) const
+void MathedArray::dump2(ostream & os) const
 {
        buffer_type::const_iterator cit = bf_.begin();
        buffer_type::const_iterator end = bf_.end();
@@ -294,4 +319,28 @@ void MathedArray::dump(ostream & os) const
        os << endl;
 }
 
+void MathedArray::dump(ostream & os) const
+{
+       MathedIter it( const_cast<MathedArray*>(this) );
+       while (it.OK()) {
+               if (it.IsInset()) {
+                       MathedInset * inset = it.GetInset();
+                       os << "<inset: " << inset << ">";
+               } 
+               else if (it.IsTab())
+                       os << "<tab>";
+               else if (it.IsCR())
+                       os << "<cr>";
+               else if (it.IsScript())
+                       os << "<script>";
+               else if (it.IsFont())
+                       os << "<font: " << int(it.at()) << ">";
+               else if (it.at() >= 32 && it.at() < 127)
+                       os << it.at();
+               else
+                       os << "<unknown: " << int(it.at()) << ">";
+               it.Next();
+       }
+}
+
        
index f60f84066783768b33a3cfafa0cf7ccedbbf97ad..79f89e67213ef1b24055de8b48874de76c01831c 100644 (file)
@@ -22,6 +22,7 @@
 #include "mathed/support.h"
 
 class MathedInset;
+class MathMacro;
 
 #ifdef __GNUG__
 #pragma interface
@@ -113,8 +114,12 @@ public:
        void need_size(int needed);
        ///
        void dump(std::ostream &) const;
+       ///
+       void dump2(std::ostream &) const;
        /// creates copies of all embedded insets
        void deep_copy();
+       ///
+       void substitute(MathMacro *);
 private:
        /// Buffer
        buffer_type bf_;
@@ -138,4 +143,12 @@ private:
        /// Last position inserted.
        int last_;
 };
+
+inline 
+ostream & operator<<(ostream & os, MathedArray const & ar)
+{
+       ar.dump(os);
+       return os;
+}
+
 #endif
index 3713e1bd2de0e9a557ef882f627405b5da22d25c..0ab0ea917eff19e7378cc0d730152d6ccb7fa7a9 100644 (file)
@@ -64,6 +64,10 @@ extern char const * latex_mathenv[];
 // this is only used by Whichfont and mathed_init_fonts (Lgb)
 LyXFont * Math_Fonts = 0;
 
+
+MathedCursor * mathcursor = 0;
+
+
 namespace {
 
 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
@@ -83,8 +87,6 @@ void mathedValidate(LaTeXFeatures & features, MathParInset * par);
 } // namespaces
 
 
-MathedCursor * InsetFormula::mathcursor = 0;
-
 
 LyXFont WhichFont(short type, int size)
 {
@@ -235,21 +237,22 @@ InsetFormula::InsetFormula(MathParInset * p)
                new MathParInset(*p);
        //   mathcursor = 0;
 
-       disp_flag_ = (par->GetType()>0);
+       disp_flag_ = par->GetType() > 0;
 }
 
 
 InsetFormula::~InsetFormula()
 {
-       delete par;
+#ifdef WITH_WARNINGS
+#warning leak this for a while...
+#endif
+       //delete par;
 }
 
 
 Inset * InsetFormula::Clone(Buffer const &) const
 {
-       InsetFormula * f = new InsetFormula(par);
-       f->label_ = label_;
-       return f;
+       return new InsetFormula(par);
 }
 
 
@@ -262,11 +265,7 @@ void InsetFormula::Write(Buffer const * buf, ostream & os) const
 
 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
 {
-       //#ifdef WITH_WARNINGS
-       //#warning Alejandro, the number of lines is not returned in this case
-       // This problem will disapear at 0.13.
-       //#endif
-       return mathed_write(par, os, fragile, label_);
+       return mathed_write(par, os, fragile, par->label()); 
 }
 
 
@@ -308,22 +307,25 @@ void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
 
        mathed_parser_file(is, lex.GetLineNo());
 
-       // Silly hack to read labels.
-       mathed_label.erase();
-
        MathedArray ar;
-       mathed_parse(ar, 0, &par);
+       mathed_parse(ar, par, 0);
+       if (par->isMatrix()) {
+               //lyxerr << "################## InsetFormula::Read: IsMatrix\n";
+               static_cast<MathMatrixInset*>(par)->setData(ar);
+       }
+       else {
+               par->setData(ar);
+               //lyxerr << "################## InsetFormula::Read: keine Matrix\n";
+       }
+
+       //lyxerr << "InsetFormula::Read: par: " << par << " " << par->GetData() << endl;
+
        par->Metrics();
        disp_flag_ = (par->GetType() > 0);
 
        // Update line number
        lex.setLineNo(mathed_parser_lineno());
 
-       if (!mathed_label.empty()) {
-               label_ = mathed_label;
-               mathed_label.erase();
-       }
-
        // reading of end_inset in the inset!!!
        while (lex.IsOK()) {
                lex.nextToken();
@@ -393,15 +395,13 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & f,
 
                if (is_singlely_numbered(par->GetType())) {
                        string str;
-                       if (!label_.empty())
-                               str = string("(") + label_ + ")";
+                       if (!par->label().empty())
+                               str = string("(") + par->label() + ")";
                        else
                                str = string("(#)");
                        pain.text(int(x + 20), baseline, str, wfont);
                } else {
-                       MathMatrixInset * mt =
-                               static_cast<MathMatrixInset*>(par);
-                       MathedRowContainer::iterator crow = mt->getRowSt().begin();
+                       MathedRowContainer::iterator crow = par->getRowSt().begin();
                        while (crow) {
                                int const y = baseline + crow->getBaseline();
                                if (crow->isNumbered()) {
@@ -459,12 +459,12 @@ void InsetFormula::InsetUnlock(BufferView * bv)
 
 
 // Now a symbol can be inserted only if the inset is locked
-void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
+void InsetFormula::InsertSymbol(BufferView *, string const & s)
 {
        if (s.empty() || !mathcursor)
                return;
        mathcursor->Interpret(s);
-       UpdateLocal(bv);
+       // Andre: UpdateLocal(bv);
 }
 
 
@@ -547,9 +547,14 @@ void InsetFormula::display(bool dspf)
                        }
                        par->SetType(LM_OT_MIN);
                        par->SetStyle(LM_ST_TEXT);
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                        if (!label_.empty()) {
                                label_.erase();
                        }
+*/
                }
                disp_flag_ = dspf;
        }
@@ -558,23 +563,10 @@ void InsetFormula::display(bool dspf)
 
 vector<string> const InsetFormula::getLabelList() const
 {
-       //#ifdef WITH_WARNINGS
-       //#warning This is dirty, I know. Ill clean it at 0.11
-       // Correction, the only way to clean this is with a new kernel: 0.13.
-       //#endif
-
        vector<string> label_list;
-
-       if (is_multi_numbered(par->GetType())) {
-               MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
-               MathedRowContainer::iterator crow = mt->getRowSt().begin();
-               while (crow) {
-                       if (!crow->getLabel().empty())
-                               label_list.push_back(crow->getLabel());
-                       ++crow;
-               }
-       } else if (!label_.empty())
-               label_list.push_back(label_);
+       MathedRowContainer::iterator crow = par->getRowSt().begin();
+       for ( ; crow; ++crow)
+               label_list.push_back(crow->getLabel());
 
        return label_list;
 }
@@ -671,7 +663,14 @@ UpdatableInset::RESULT
 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                            string const & arg)
 {
+       //lyxerr << "InsetFormula::LocalDispatch: act: " << action
+       //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
        //   extern char *dispatch_result;
+
+       if (!mathcursor) {
+               return UNDISPATCHED;
+       }
+
        MathedTextCodes varcode = LM_TC_MIN;
        bool was_macro = mathcursor->InMacroMode();
        bool sel = false;
@@ -695,6 +694,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Right(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -705,6 +705,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Left(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -715,6 +716,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Up(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -725,6 +727,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                result = DISPATCH_RESULT(mathcursor->Down(sel));
                if (!sel && (result == DISPATCHED))
                        result = DISPATCHED_NOUPDATE;
+               UpdateLocal(bv);
                break;
 
 
@@ -741,6 +744,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_DELETE_LINE_FORWARD:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->DelLine();
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -749,11 +753,17 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                bv->lockedInsetStoreUndo(Undo::INSERT);
                byte c = arg.empty() ? '1' : arg[0];
                mathcursor->Insert(c, LM_TC_CR);
-               if (!label_.empty()) {
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
+               if (!par->label().empty()) {
                        mathcursor->setLabel(label_);
                        label_.erase();
                }
+*/
                par = mathcursor->GetPar();
+               // Andre:
                UpdateLocal(bv);
        }
        break;
@@ -767,6 +777,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_TABINSERT:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                mathcursor->Insert('T', LM_TC_TAB);
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -810,12 +821,14 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        mathcursor->MacroModeClose();
                bv->lockedInsetStoreUndo(Undo::INSERT);
                mathcursor->SelPaste();
+               // Andre:
                UpdateLocal(bv);
                break;
 
        case LFUN_CUT:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->SelCut();
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -880,24 +893,29 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        short type = par->GetType();
                        if (is_numbered(type)) {
                                --type;
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                                if (!label_.empty()) {
                                        label_.erase();
                                }
+*/
                                bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("No number"));
                        } else {
                                ++type;
                                bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Number"));
                        }
                        par->SetType(type);
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
 
        case LFUN_MATH_NONUMBER:
                if (is_multi_numbered(par->GetType())) {
-                               //         MathMatrixInset *mt = (MathMatrixInset*)par;
                                //BUG
-                               //         mt->SetNumbered(!mt->IsNumbered());
+                               //         par->SetNumbered(!par->IsNumbered());
 
 #ifdef WITH_WARNINGS
 #warning This is a terrible hack! We should find a better solution.
@@ -907,6 +925,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                                        return DISPATCHED;
                        }
                        mathcursor->setNumbered();
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
@@ -914,14 +933,16 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_MATH_LIMITS:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                if (mathcursor->Limits())
+                       // Andre:
                        UpdateLocal(bv);
                // fall through!
 
        case LFUN_MATH_SIZE:
                if (!arg.empty()) {
                        latexkeys const * l = in_word_set(arg);
-                       int const sz = (l) ? l->id: -1;
+                       int const sz = l ? l->id : -1;
                        mathcursor->SetSize(sz);
+                       // Andre:
                        UpdateLocal(bv);
                        break;
                }
@@ -952,11 +973,12 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        n = 1;
                }
 
-               MathMatrixInset * p = new MathMatrixInset(m, n);
-               if (mathcursor && p) {
+               if (mathcursor) {
+                       MathMatrixInset * p = new MathMatrixInset(m, n);
                        if (k > 2 && int(strlen(s)) > m)
                                p->SetAlign(s[0], &s[1]);
                        mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+                       // Andre:
                        UpdateLocal(bv);
                }
                break;
@@ -1008,6 +1030,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
 
                MathDelimInset * p = new MathDelimInset(ilf, irg);
                mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
+               // Andre:
                UpdateLocal(bv);
                break;
        }
@@ -1017,6 +1040,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                sp = new MathSpaceInset(1);
                mathcursor->insertInset(sp, LM_TC_INSET);
                space_on = true;
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -1027,7 +1051,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        break;
 
                string old_label = is_multiline(par->GetType())
-                       ? mathcursor->getLabel() : label_;
+                       ? mathcursor->getLabel() : par->label();
 
 #ifdef WITH_WARNINGS
 #warning This is a terrible hack! We should find a better solution.
@@ -1065,13 +1089,18 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
                        bv->redraw();
 
+#ifdef WITH_WARNINGS
+#warning Labels
+#endif
+/*
                if (is_multi_numbered(par->GetType())) {
                        mathcursor->setLabel(new_label);
-                       // MathMatrixInset *mt = (MathMatrixInset*)par;
-                       // mt->SetLabel(new_label);
+                       // par->SetLabel(new_label);
                } else
                        label_ = new_label;
+*/
 
+               // Andre:
                UpdateLocal(bv);
                break;
        }
@@ -1079,6 +1108,7 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
        case LFUN_MATH_DISPLAY:
                bv->lockedInsetStoreUndo(Undo::EDIT);
                display(!disp_flag_);
+               // Andre:
                UpdateLocal(bv);
                break;
 
@@ -1215,11 +1245,11 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                        } else if (c == '\\') {
                                if (was_macro)
                                        mathcursor->MacroModeClose();
-                               bv->owner()->getLyXFunc()
-                                       ->Dispatch(LFUN_MESSAGE,
-                                                  _("TeX mode"));
+                               // This line nukes the mathcursor. Why?
+                               //bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("TeX mode"));
                                mathcursor->setLastCode(LM_TC_TEX);
                        }
+                       // Andre:
                        UpdateLocal(bv);
                } else if (action == LFUN_MATH_PANEL) {
                        result = UNDISPATCHED;
@@ -1229,24 +1259,27 @@ InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
                }
        }
        
-       if (was_macro != mathcursor->InMacroMode()
+       //UpdateLocal(bv);
+       
+       // Andre:
+       if ((mathcursor && was_macro != mathcursor->InMacroMode())
            && action >= 0
            && action != LFUN_BACKSPACE) 
-               UpdateLocal(bv);
+               UpdateLocal(bv);
 
        if (sp && !space_on)
                sp = 0;
 
-       if (mathcursor->Selection() || was_selection)
+       if (mathcursor && (mathcursor->Selection() || was_selection))
                ToggleInsetSelection(bv);
 
-       if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
-           (result == UNDISPATCHED))
+       if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
+           result == UNDISPATCHED)
                ShowInsetCursor(bv);
        else
                bv->unlockInset(this);
 
-       return result;
+       return result;  // original version
 }
 
 
index b547e421e644dec6aa8e58a28a2d82013a3aeaaa..e8ce2f22828bc3d200fb575e02314461144a4e73 100644 (file)
@@ -24,9 +24,9 @@
 #include <iosfwd>
 
 #include "insets/lyxinset.h"
+#include <boost/smart_ptr.hpp>
 
 class MathParInset;
-class MathedCursor;
 class Buffer;
 
 ///
@@ -112,15 +112,12 @@ protected:
        ///
        void UpdateLocal(BufferView * bv);
        ///
-       MathParInset * par;
-       ///
-       static MathedCursor * mathcursor;
+       //boost::shared_ptr<MathParInset> par;
+       MathParInset * par;
     
 private:
        ///
        bool disp_flag_;
-       ///
-       string label_;
 };
 
 
@@ -144,6 +141,6 @@ LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
 inline
 bool InsetFormula::display() const
 {
-       return (disp_flag_) ? true : false;
+       return disp_flag_;
 }
 #endif
index 5965b2b2f14d6197e5245358c85a9684ec477443..6469b547bdfb6e398adebb4c5002428015676818 100644 (file)
@@ -24,6 +24,7 @@
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "math_macro.h"
+#include "math_macroarg.h"
 #include "math_macrotable.h"
 #include "math_macrotemplate.h"
 #include "lyx_main.h"
 using std::ostream;
 using std::istream;
 
+extern MathedCursor * mathcursor;
+
 InsetFormulaMacro::InsetFormulaMacro()
        : InsetFormula(true)
 {
-       opened_ = false;
+       par = &MathMacroTable::provideTemplate("unknown", 0);
 }
 
 
 InsetFormulaMacro::InsetFormulaMacro(string nm, int na)
-        : InsetFormula(true), name_(nm)
-{
-       tmacro_ = MathMacroTable::mathMTable.getTemplate(name_);
-       if (!tmacro_.get()) {
-               tmacro_.reset(new MathMacroTemplate(name_, na));
-               MathMacroTable::mathMTable.addTemplate(tmacro_);
-       }
-       opened_ = false;
-}
-
-
-InsetFormulaMacro::~InsetFormulaMacro()
+  : InsetFormula(true)
 {
-       // We do not want the InsetFormula destructor to
-       // delete this. That is taken care of elsewhere (Lgb)
-       par = 0;
+       par = &MathMacroTable::provideTemplate(nm, na);
 }
 
 
 Inset * InsetFormulaMacro::Clone(Buffer const &) const
 {
-       // This should really use a proper copy constructor
-       return new InsetFormulaMacro(name_, 0);
+       return new InsetFormulaMacro(*this);
 }
 
 
 void InsetFormulaMacro::Write(Buffer const *, ostream & os) const
 {
        os << "FormulaMacro ";
-       tmacro_->WriteDef(os, false);
+       tmacro()->WriteDef(os, false);
 }
 
 
 int InsetFormulaMacro::Latex(Buffer const *, ostream & os, bool /*fragile*/, 
                             bool /*free_spacing*/) const
 {
-       tmacro_->WriteDef(os, true); // or false?
+       tmacro()->WriteDef(os, true); // or false?
        return 2;
 }
 
@@ -104,20 +93,19 @@ void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
 {
        istream & is = lex.getStream();
        mathed_parser_file(is, lex.GetLineNo());
+
+       MathParInset * tmp = new MathParInset;
        MathedArray ar;
-       
-       mathed_parse(ar, 0, reinterpret_cast<MathParInset **>(&tmacro_));
-       // Since tmacro_ == 0 when mathed_parse is called we need to set
-       // its contents explicitly afterwards (Lgb)
-       tmacro_->setData(ar);
+       mathed_parse(ar, tmp, 0);
+       par = &MathMacroTable::provideTemplate(tmp->GetName(), tmp->xo());
+       par->setData(ar);
+       //cerr << "## InsetFormulaMacro::Read name: " << tmp->GetName() << endl;
+       //cerr << "## InsetFormulaMacro::Read nargs: " << tmp->xo() << endl;
+       //cerr << "## InsetFormulaMacro::Read 1: " << ar << endl;
        
        // Update line number
        lex.setLineNo(mathed_parser_lineno());
        
-       MathMacroTable::mathMTable.addTemplate(tmacro_);
-       name_ = tmacro_->GetName();
-       par = tmacro_.get();
-
        // reading of end_inset in the inset!!!
        while (lex.IsOK()) {
                lex.nextToken();
@@ -126,33 +114,26 @@ void InsetFormulaMacro::Read(Buffer const *, LyXLex & lex)
        }
 }
 
-
 int InsetFormulaMacro::ascent(BufferView * pain, LyXFont const & f) const
 {
-       if (opened_) {
-               return InsetFormula::ascent(pain, f);
-       }
-       return lyxfont::maxAscent(f) + 3;
+       return InsetFormula::ascent(pain, f);
 }
 
 
 int InsetFormulaMacro::descent(BufferView * pain, LyXFont const & f) const
 {
-       if (opened_) {
-               return InsetFormula::descent(pain, f);
-       }
-       return lyxfont::maxDescent(f) + 1;
+       return InsetFormula::descent(pain, f);
 }
 
 
+string InsetFormulaMacro::prefix() const
+{
+       return string(" ") + _("Macro: ") + par->GetName() + ": ";
+}
+
 int InsetFormulaMacro::width(BufferView * bv, LyXFont const & f) const
 {
-       if (opened_) {
-               return InsetFormula::width(bv, f);
-       }
-       string ilabel(_("Macro: "));
-       ilabel += name_;
-       return 6 + lyxfont::width(ilabel, f);
+       return 10 + lyxfont::width(prefix(), f) + InsetFormula::width(bv, f);
 }
 
 
@@ -161,48 +142,31 @@ void InsetFormulaMacro::draw(BufferView * bv, LyXFont const & f,
 {
        Painter & pain = bv->painter();
        LyXFont font(f);
-       if (opened_) {
-               tmacro_->setEditMode(true);
-               InsetFormula::draw(bv, font, baseline, x, cleared);
-               tmacro_->setEditMode(false);    
-       } else {
-               font.setColor(LColor::math);
-               
-               int const y = baseline - ascent(bv, font) + 1;
-               int const w = width(bv, font) - 2;
-               int const h = (ascent(bv, font) + descent(bv, font) - 2);
+
+       // label
+       font.setColor(LColor::math);
        
-               pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
-               pain.rectangle(int(x), y, w, h, LColor::mathframe);
-               
-               string s(_("Macro: "));
-               s += name_;
-               pain.text(int(x + 2), baseline, s, font);
-               x +=  width(bv, font) - 1;
-       }
-}
+       int const y = baseline - ascent(bv, font) + 1;
+       int const w = width(bv, font) - 2;
+       int const h = (ascent(bv, font) + descent(bv, font) - 2);
 
+       pain.fillRectangle(int(x), y, w, h, LColor::mathbg);
+       pain.rectangle(int(x), y, w, h, LColor::mathframe);
+       
+       pain.text(int(x + 2), baseline, prefix(), font);
+       x += width(bv, font);
 
-string const InsetFormulaMacro::EditMessage() const 
-{
-       return _("Math macro editor mode");
+       // formula
+       float t = InsetFormula::width(bv, f) + 5;
+       x -= t;
+       InsetFormula::draw(bv, font, baseline, x, cleared);
+       x += t;
 }
 
 
-void InsetFormulaMacro::Edit(BufferView * bv, int x, int y,unsigned int button)
-{
-       opened_ = true;
-       par = static_cast<MathParInset*>(tmacro_->Clone());
-       InsetFormula::Edit(bv, x, y, button);
-}
-
-              
-void InsetFormulaMacro::InsetUnlock(BufferView * bv)
+string const InsetFormulaMacro::EditMessage() const 
 {
-       opened_ = false;
-       tmacro_->setData(par->GetData());
-       tmacro_->setEditMode(false);
-       InsetFormula::InsetUnlock(bv);
+       return _("Math macro editor mode");
 }
 
 
@@ -211,19 +175,19 @@ InsetFormulaMacro::LocalDispatch(BufferView * bv,
                                 kb_action action, string const & arg)
 {
        if (action == LFUN_MATH_MACROARG) {
-               int const i = lyx::atoi(arg) - 1;
-               if (i >= 0 && i < tmacro_->getNoArgs()) {
-                       mathcursor->insertInset(tmacro_->getMacroPar(i),
-                                                LM_TC_INSET);
+               int const i = lyx::atoi(arg);
+               if (i > 0 && i <= tmacro()->nargs()) {
+                       mathcursor->insertInset(new MathMacroArgument(i), LM_TC_INSET);
                        InsetFormula::UpdateLocal(bv);
                }
        
                return DISPATCHED;
        }
-       tmacro_->setEditMode(true);
-       tmacro_->Metrics();
-       RESULT result = InsetFormula::LocalDispatch(bv, action, arg);
-       tmacro_->setEditMode(false);
-    
-       return result;
+       par->Metrics();
+       return InsetFormula::LocalDispatch(bv, action, arg);
+}
+
+MathMacroTemplate * InsetFormulaMacro::tmacro() const
+{
+       return static_cast<MathMacroTemplate *>(par);
 }
index 3ab9c44acb40b2324f37fecce601924c341270c1..ece763538a0550c80cc0a312da6eb87ef978e57b 100644 (file)
@@ -17,8 +17,6 @@
 #ifndef INSET_FORMULA_MACRO_H 
 #define INSET_FORMULA_MACRO_H
 
-#include <boost/smart_ptr.hpp>
-
 #include "formula.h"
 
 #ifdef __GNUG__
@@ -27,6 +25,9 @@
 
 class MathMacroTemplate;
 
+// InsetFormulaMacro's ParInset is the ParInset of the macro definition
+// which in turn is stored in the global MathMacroTable.
+// No copying/updating needed anymore...
 
 ///
 class InsetFormulaMacro: public InsetFormula {
@@ -37,8 +38,6 @@ public:
        explicit
        InsetFormulaMacro(string name, int na);
        ///
-       ~InsetFormulaMacro();
-       ///
        int ascent(BufferView *, LyXFont const &) const;
        ///
        int descent(BufferView *, LyXFont const &) const;
@@ -48,7 +47,7 @@ public:
        void draw(BufferView *,LyXFont const &, int, float &, bool) const;
        ///
        void Read(Buffer const *, LyXLex & lex);
-        ///
+       ///
        void Write(Buffer const *, std::ostream & os) const;
        ///
        int Latex(Buffer const *, std::ostream & os, bool fragile,
@@ -64,24 +63,15 @@ public:
        /// what appears in the minibuffer when opening
        string const EditMessage() const;
        ///
-       void Edit(BufferView *, int x, int y, unsigned int button);
-       ///
-       void InsetUnlock(BufferView *);
-       ///
        RESULT LocalDispatch(BufferView *, kb_action, string const &);
-
 private:
+       /// prefix in inset
+       string prefix() const;
        ///
-        bool opened_;
-       ///
-        string name_;
-       ///
-        boost::shared_ptr<MathMacroTemplate> tmacro_;
+       MathMacroTemplate * tmacro() const;
 };
 
-
-inline
-Inset::Code InsetFormulaMacro::LyxCode() const
+inline Inset::Code InsetFormulaMacro::LyxCode() const
 {
        return Inset::MATHMACRO_CODE;
 }
index b9c40e861c6830a9a921f83e3d4220620c792592..6f2386f595c56c180eb7641d1535bd79558d1571 100644 (file)
@@ -5,11 +5,10 @@
 #include <iosfwd>
 
 enum MathedMacroFlag {
-       MMF_Env = 1,
-       MMF_Exp = 2,
+       MMF_Env  = 1,
+       MMF_Exp  = 2,
        MMF_Edit = 4
 };
 
-extern
 std::ostream & operator<<(std::ostream & o, MathedMacroFlag mmf);
 #endif
index be1b146fc9bbd8408ffc49eddb003dc5737fdb7a..382072e65afff0c999cba38459cc7a35025a59d2 100644 (file)
@@ -1,19 +1,19 @@
 /*
-*  File:        math_cursor.C
-*  Purpose:     Interaction for mathed
-*  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
-*  Created:     January 1996
-*  Description: Math interaction for a WYSIWYG math editor.
-*
-*  Dependencies: Xlib, XForms
-*
-*  Copyright: 1996, Alejandro Aguilar Sierra
-*
-*   Version: 0.8beta, Mathed & Lyx project.
-*
-*   You are free to use and modify this code under the terms of
-*   the GNU General Public Licence version 2 or later.
-*/
+ *  File:        math_cursor.C
+ *  Purpose:     Interaction for mathed
+ *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
+ *  Created:     January 1996
+ *  Description: Math interaction for a WYSIWYG math editor.
+ *
+ *  Dependencies: Xlib, XForms
+ *
+ *  Copyright: 1996, Alejandro Aguilar Sierra
+ *
+ *   Version: 0.8beta, Mathed & Lyx project.
+ *
+ *   You are free to use and modify this code under the terms of
+ *   the GNU General Public Licence version 2 or later.
+ */
 
 #ifdef __GNUG__
 #pragma implementation
@@ -25,6 +25,7 @@
 #include "math_parser.h"
 #include "math_cursor.h"
 #include "math_macro.h"
+#include "math_macroarg.h"
 #include "math_macrotable.h"
 #include "math_root.h"
 #include "support/lstrings.h"
@@ -43,6 +44,9 @@
 #include "math_macrotemplate.h"
 #include "mathed/support.h"
 
+
+
+
 using std::endl;
 
 namespace {
@@ -68,75 +72,146 @@ bool IsMacro(short tok, int id)
 int const MAX_STACK_ITEMS = 32;
 
 struct MathStackXIter {
+public:
+       enum type {MATHSTK, SELSTK};
+
+private:
        std::vector<MathedXIter> item;
-       int i;
+       int pos_;
+       type id_;
+
 
-       MathStackXIter(int n = MAX_STACK_ITEMS)
-               : item(n), i(0) {
+public:
+
+       MathStackXIter(type id)
+               : item(MAX_STACK_ITEMS), pos_(-1), id_(id) {
        }
 
        MathedXIter * push() {
-               return &item[i++];
+               //dump();
+               ++pos_;
+               return &item[pos_];
        }
 
        MathedXIter * pop() {
-    --i;
-               return &item[i - 1];
+               //dump();
+               item[pos_] = MathedXIter();
+    --pos_;
+               return &item[pos_];
        }
 
        MathedXIter * Item(int idx) {
-               if (idx + 1 > i)
-                       lyxerr << "Wrong index: " << idx << " i: " << i << endl;
-               return &item[i - idx - 1];
+               if (idx > pos_)
+                       lyxerr << "Wrong index: " << idx << " pos_: " << pos_ << endl;
+               return &item[pos_ - idx];
        }
 
        void Reset() {
-               i = 0;
+               pos_ = -1;
        }
 
        bool Full() {
-               return i >= MAX_STACK_ITEMS;
+               return pos_ >= MAX_STACK_ITEMS - 2;
+       }
+
+       bool empty() {
+               return pos_ <= 0;
+       }
+
+       MathParInset * outer() {
+               return empty() ? 0 : item[0].getPar();
        }
 
-       bool Empty() {
-               return i <= 1;
+       int Level() {
+               return pos_;
        }
 
-       int Level() { return i; }
+       MathParInset * parInset(int i) {
+               return pos_ < 0 ? 0 : item[i].getPar();
+       }
+
+
+       void dump() {
+               lyxerr << "\n------------- MathStack ------------\n";
+               for (int i = 0; i < pos_ + 3; ++i) {
+                       lyxerr << "pos: " << i << " par: "
+                               << item[i].getPar() << " data: '"
+                               << *item[i].GetData() << "'" << endl;
+               }
+               lyxerr << "------------- MathStack ------------\n";
+       }
 
-} mathstk, selstk;
+};
+
+MathStackXIter mathstk = MathStackXIter(MathStackXIter::MATHSTK);
+MathStackXIter selstk  = MathStackXIter(MathStackXIter::SELSTK);
 
 } // namespace anon
 
 
+extern MathedCursor * mathcursor;
+
+bool is_mathcursor_inside(MathParInset * p) 
+{
+       //lyxerr << "called is_mathcursor_inside: " << p << endl;
+
+       if (!mathcursor) {
+               //lyxerr << "  mathcursor not set" << endl;
+               return false;
+       }
+
+       for (int i = 0; i < mathstk.Level(); ++i) {
+               //lyxerr << "   level: " << i << "  " << mathstk.parInset(i) << endl;
+               if (mathstk.parInset(i) == p) {
+                       //lyxerr << "  found!" << endl;
+                       return true;
+               }
+       }
+
+       //lyxerr << "   cursor: " << mathcursor->cursor->getPar() << endl;
+       if (mathcursor->cursor->getPar() == p) {
+               //lyxerr << "  found!" << endl;
+               return true;
+       }
+       //lyxerr << "   not found" << endl;
+       return false; 
+}
+
+
+
 /***----------------  Mathed Cursor  ---------------------------***/
 
+
 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
 {
-       accent   = 0;
-       anchor   = 0;
-       lastcode = LM_TC_MIN;
+       accent     = 0;
+       anchor     = 0;
+       lastcode   = LM_TC_MIN;
        SetPar(p);
-       if (!MathMacroTable::built)
-               MathMacroTable::mathMTable.builtinMacros();
 }
 
-
 void MathedCursor::SetPar(MathParInset * p)
 {
-       macro_mode = false;
-       selection  = false; // not SelClear() ?
-       mathstk.Reset();
-       cursor = mathstk.push();
-       par = p;
-       cursor->SetData(par);
+  macro_mode = false;
+  selection  = false; // not SelClear() ?
+  mathstk.Reset();
+  cursor = mathstk.push();
+  par = p;
+  SetCursorData(par);
+}
+
+
+void MathedCursor::SetCursorData(MathParInset * p)
+{
+       //lyxerr << "SetCursorData: " << p << endl;
+       cursor->SetData(p);
 }
 
 
 void MathedCursor::draw(Painter & pain, int x, int y)
 {
        //    lyxerr << "Cursor[" << x << " " << y << "] ";
-       //win = pm;    // win = (mathedCanvas) ? mathedCanvas: pm;
+       //lyxerr << "MathedCursor::draw: par: " << par << endl;
 
        par->Metrics();
        int w = par->Width() + 2;
@@ -148,7 +223,7 @@ void MathedCursor::draw(Painter & pain, int x, int y)
                h += 8;
        }
 
-       pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
+       pain.rectangle(x - 1, y - a, w, h, LColor::green);
 
        par->draw(pain, x, y);
        cursor->Adjust();
@@ -166,14 +241,16 @@ void MathedCursor::Redraw(Painter & pain)
        par->GetXY(x, y);
        //mathed_set_font(LM_TC_VAR, 1);
        pain.fillRectangle(x, y - par->Ascent(),
-       x + w, y - par->Ascent() + h,
-       LColor::mathbg);
+               x + w, y - par->Ascent() + h, LColor::mathbg);
+
+       lyxerr << "MathedCursor::Redraw: par: " << par << endl;
        par->draw(pain, x, y);
 }
 
 
 bool MathedCursor::Left(bool sel)
 {
+       //par->GetData().dump(cerr);
        if (macro_mode) {
                // was MacroModeBack()
                if (!imacro->GetName().empty()) {
@@ -196,7 +273,7 @@ bool MathedCursor::Left(bool sel)
 
        bool result = cursor->Prev();
 
-       if (!result && !mathstk.Empty()) {
+       if (!result && !mathstk.empty()) {
                cursor = mathstk.pop();
                cursor->Adjust();
                result = true;
@@ -216,9 +293,10 @@ bool MathedCursor::Left(bool sel)
                        if (!p)
                                return result;
 
+                       //lyxerr << "\nInset,  max arg # " << p->getMaxArgumentIdx() << endl;
                        p->setArgumentIdx(p->getMaxArgumentIdx());
                        cursor = mathstk.push();
-                       cursor->SetData(p);
+                       SetCursorData(p);
                        cursor->GoLast();
                }
        }
@@ -229,7 +307,7 @@ bool MathedCursor::Left(bool sel)
 // Leave the inset
 bool MathedCursor::Pop()
 {
-       if (!mathstk.Empty()) {
+       if (!mathstk.empty()) {
                cursor = mathstk.pop();
                cursor->Next();
                return true;
@@ -246,7 +324,7 @@ bool MathedCursor::Push()
                if (!p)
                        return false;
                cursor = mathstk.push();
-               cursor->SetData(p);
+               SetCursorData(p);
                return true;
        }
        return false;
@@ -280,22 +358,22 @@ bool MathedCursor::Right(bool sel)
                }
 
                if (!selection) {
-                       MathParInset *p = cursor->GetActiveInset();
+                       MathParInset * p = cursor->GetActiveInset();
                        if (!p) {
-                       lyxerr << "Math error: Inset expected." << endl;
-                       return cursor->Next();
+                               lyxerr << "Math error: Inset expected." << endl;
+                               return cursor->Next();
                        }
                        p->setArgumentIdx(0);
                        cursor = mathstk.push();
-                       cursor->SetData(p);
+                       SetCursorData(p);
                        result = true;
                } else
                        result = cursor->Next();
 
        } else {
-               if (cursor->GetChar()!= LM_TC_CR)
-               result = cursor->Next();
-               if (!result && !mathstk.Empty()) {
+               if (cursor->GetChar() != LM_TC_CR)
+                       result = cursor->Next();
+               if (!result && !mathstk.empty()) {
                        cursor = mathstk.pop();
                        cursor->Next();
                        cursor->Adjust();
@@ -318,16 +396,16 @@ void MathedCursor::SetPos(int x, int y)
        lastcode = LM_TC_MIN;
        mathstk.Reset();
        cursor = mathstk.push();
-       cursor->SetData(par);
+       SetCursorData(par);
        cursor->fitCoord(x, y);
 
-       while (cursor->GetX()<x && cursor->OK()) {
+       while (cursor->GetX() < x && cursor->OK()) {
                if (cursor->IsActive()) {
                        MathParInset * p = cursor->GetActiveInset();
                        if (p->Inside(x, y)) {
                                p->SetFocus(x, y);
                                cursor = mathstk.push();
-                               cursor->SetData(p);
+                               SetCursorData(p);
                                cursor->fitCoord(x, y);
                                continue;
                        }
@@ -365,7 +443,7 @@ void MathedCursor::End()
 }
 
 
-MathMatrixInset create_multiline(short int type, int cols)
+MathMatrixInset create_multiline(short int type, int cols)
 {
        int columns;
        string align;
@@ -401,8 +479,8 @@ MathMatrixInset * create_multiline(short int type, int cols)
                        break;
        }
 
-       MathMatrixInset * mt = new MathMatrixInset(columns, -1);
-       mt->SetAlign(' ', align);
+       MathMatrixInset mt(columns, -1);
+       mt.SetAlign(' ', align);
        return mt;
 }
 
@@ -420,7 +498,7 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
 
        if (t == LM_TC_CR) {
                MathParInset * p = cursor->getPar();
-               if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
+               if (p == par && p->GetType() < LM_OT_MPAR && p->GetType() > LM_OT_MIN) {
                        short int type = LM_OT_MPAR;
                        int cols = 1;
                        if (c >= '1' && c <= '9') {
@@ -436,7 +514,8 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
 
                        if (p->GetType() == LM_OT_PARN)
                                ++type;
-                       MathMatrixInset * mt = create_multiline(type, cols);
+                       MathMatrixInset * mt =
+                               new MathMatrixInset(create_multiline(type, cols));
                        mt->SetStyle(LM_ST_DISPLAY);
                        mt->SetType(type);
                        mt->setData(p->GetData());
@@ -445,26 +524,28 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
                        p = mt;
                        p->Metrics();
                        int pos = cursor->getPos();
-                       cursor->SetData(par);
+                       SetCursorData(par);
                        cursor->goPosAbs(pos);
                }
-               if (p &&  p->Permit(LMPF_ALLOW_CR)) {
+               if (p && p->Permit(LMPF_ALLOW_CR)) {
                        cursor->addRow();
                }
        } else if (t == LM_TC_TAB) {
                MathParInset * p = cursor->getPar();
-               if (p &&  p->Permit(LMPF_ALLOW_TAB)) {
+               if (p && p->Permit(LMPF_ALLOW_TAB)) {
                        if (c) {
                                cursor->insert(c, t);
                                cursor->checkTabs();
                        } else
                                cursor->goNextColumn();
-               } else // Navigate between arguments
-               if (p && p->GetType() == LM_OT_MACRO) {
-                       if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
-                               p->setArgumentIdx(p->getArgumentIdx() + 1);
-                               cursor->SetData(p);
-                               return;
+               } else {
+                       // Navigate between arguments
+                       if (p && p->GetType() == LM_OT_MACRO) {
+                               if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
+                                       p->setArgumentIdx(p->getArgumentIdx() + 1);
+                                       SetCursorData(p);
+                                       return;
+                               }
                        }
                }
        } else {
@@ -476,9 +557,9 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
                        }
                }
 
-               if (accent) {
+               if (accent)
                        doAccent(c, t);
-               else
+               else
                        cursor->insert(c, t);
 
                lastcode = t;
@@ -501,7 +582,7 @@ void MathedCursor::insertInset(MathedInset * p, int t)
                        SelDel();
        }
 
-       if (mathstk.i < MAX_STACK_ITEMS - 1) {
+       if (!mathstk.Full()) {
                if (accent && !MathIsActive(t)) {       
                        doAccent(p);
                } else {
@@ -526,10 +607,10 @@ void MathedCursor::Delete()
                return;
        }
 
-       if (cursor->Empty() && !mathstk.Empty()) 
+       if (cursor->Empty() && !mathstk.empty()) 
                cursor = mathstk.pop();
 
-       //   if (cursor->GetChar()!= LM_TC_TAB)
+       //   if (cursor->GetChar() != LM_TC_TAB)
        cursor->Delete();
        cursor->checkTabs();
 }
@@ -547,9 +628,8 @@ void MathedCursor::DelLine()
 
        MathParInset * p = cursor->getPar();
 
-       if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR) {
+       if (p && p->GetType() <= LM_OT_MATRIX && p->GetType() >= LM_OT_MPAR)
                cursor->delRow();
-       }
 }
 
 
@@ -571,16 +651,18 @@ bool MathedCursor::Up(bool sel)
                if (MathIsUp(cd)) {
                        Push();
                        return true;
-               } else {
-                       // A subscript may be followed by a superscript
-                       cursor->ipush();
-                       cursor->Next();
-                       if (MathIsUp(cursor->GetChar())) {
-                               Push();
-                               return true;
-                       } else  // return to the previous state
-                               cursor->ipop();
                }
+
+               // A subscript may be followed by a superscript
+               cursor->ipush();
+               cursor->Next();
+               if (MathIsUp(cursor->GetChar())) {
+                       Push();
+                       return true;
+               }
+               
+               // return to the previous state
+               cursor->ipop();
        }
 
        result = cursor->Up();
@@ -592,16 +674,16 @@ bool MathedCursor::Up(bool sel)
                        bool is_down = (cx->GetChar() == LM_TC_DOWN);
                        cursor = mathstk.pop();
                        cursor->Next();
-                       result =  (is_down) ? true: Up();
+                       result = is_down ? true : Up();
                } else {
-                       result = (p->getArgumentIdx() > 0);
+                       result = p->getArgumentIdx() > 0;
                        if (result) {
                                p->setArgumentIdx(p->getArgumentIdx() - 1);
-                               cursor->SetData(p);
+                               SetCursorData(p);
                        }
                }
 
-               if (!result && !mathstk.Empty()) {
+               if (!result && !mathstk.empty()) {
                        cursor = mathstk.pop();
                        return Up();
                }
@@ -628,16 +710,17 @@ bool MathedCursor::Down(bool sel)
                if (MathIsDown(cd)) {
                        Push();
                        return true;
-               } else {
+               }
                // A superscript may be followed by a subscript
                cursor->ipush();
                cursor->Next();
                if (MathIsDown(cursor->GetChar())) {
                        Push();
                        return true;
-               } else
-                       cursor->ipop();
                }
+
+               // return to the previous state
+               cursor->ipop();
        }
 
        result = cursor->Down();
@@ -648,15 +731,15 @@ bool MathedCursor::Down(bool sel)
                        bool is_up = (cx->GetChar() == LM_TC_UP);
                        cursor = mathstk.pop();
                        cursor->Next();
-                       result = (is_up) ? true : Down();
+                       result = is_up ? true : Down();
                } else {
-                       result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
+                       result = p->getArgumentIdx() < p->getMaxArgumentIdx();
                        if (result) {
                                p->setArgumentIdx(p->getArgumentIdx() + 1);
-                               cursor->SetData(p);
+                               SetCursorData(p);
                        }
                }
-               if (!result && !mathstk.Empty()) {
+               if (!result && !mathstk.empty()) {
                        cursor = mathstk.pop();
                        return Down(sel);
                }
@@ -681,7 +764,7 @@ void MathedCursor::SetSize(short size)
 {
        MathParInset * p = cursor->getPar();
        p->UserSetSize(size);
-       cursor->SetData(p);
+       SetCursorData(p);
 }
 
 
@@ -739,14 +822,14 @@ void MathedCursor::Interpret(string const & s)
                l = in_word_set(s);
 
        if (!l) {
-               p = MathMacroTable::mathMTable.createMacro(s);
+               p = new MathMacro(MathMacroTable::provideTemplate(s));
                if (!p) {
-               lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
-               if (s == "root") {
-                       p = new MathRootInset;
-                       tcode = LM_TC_ACTIVE_INSET;
-               } else
-                       p = new MathFuncInset(s, LM_OT_UNDEF);
+                       lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
+                       if (s == "root") {
+                               p = new MathRootInset;
+                               tcode = LM_TC_ACTIVE_INSET;
+                       } else
+                               p = new MathFuncInset(s, LM_OT_UNDEF);
                } else {
                        tcode = static_cast<MathMacro*>(p)->getTCode();
                        lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
@@ -803,7 +886,7 @@ void MathedCursor::Interpret(string const & s)
                                break;
 
                        case LM_TK_MACRO:
-                               p = MathMacroTable::mathMTable.createMacro(s);
+                               p = new MathMacro(MathMacroTable::provideTemplate(s));
                                tcode = static_cast<MathMacro*>(p)->getTCode();
                                lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
                                break;
@@ -859,8 +942,9 @@ void MathedCursor::MacroModeClose()
                macro_mode = false;
                latexkeys const * l = in_word_set(imacro->GetName());
                if (!imacro->GetName().empty()
-               && (!l || (l && IsMacro(l->token, l->id))) &&
-               !MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
+                               && (!l || (l && IsMacro(l->token, l->id))) &&
+                               !MathMacroTable::hasTemplate(imacro->GetName()))
+               {
                        if (!l) {
                                //imacro->SetName(macrobf);
                                // This guarantees that the string will be removed by destructor
@@ -874,7 +958,7 @@ void MathedCursor::MacroModeClose()
                                        static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
                        }
                        cursor->Delete();
-                       if (l || MathMacroTable::mathMTable.createMacro(imacro->GetName())) {
+                       if (l || MathMacroTable::hasTemplate(imacro->GetName())) {
                                Interpret(imacro->GetName());
                        }
                        imacro->SetName("");
@@ -1072,16 +1156,16 @@ void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
 
 void MathedCursor::setAccent(int ac)
 {
-       if (ac > 0 && accent < 8) {
+       if (ac > 0 && accent < 8)
                nestaccent[accent++] = ac;
-       else
+       else
                accent = 0;  // consumed!
 }
 
 
 int MathedCursor::getAccent() const
 {
-       return (accent > 0) ? nestaccent[accent - 1]: 0;
+       return (accent > 0) ? nestaccent[accent - 1] : 0;
 }
 
 
@@ -1194,3 +1278,11 @@ MathedTextCodes MathedCursor::getLastCode() const
 {
        return lastcode;
 }
+
+
+bool MathedCursor::hasData(MathedArray const & ar)
+{
+       lyxerr << "hasData: ar: " << &ar << " cursor: " << cursor->GetData() <<
+endl;
+       return &ar == cursor->GetData();
+}
index 51a9942ef5e43cee4ca1562619863b5f95c99ef4..6362d2583665b55af19ff21ad62341e495adf354 100644 (file)
@@ -77,6 +77,8 @@ public:
        ///
        MathParInset * getCurrentPar() const;
        ///
+       void SetCursorData(MathParInset *);
+       ///
        void SetPar(MathParInset *);
        ///
        void Interpret(string const &);
@@ -131,8 +133,11 @@ public:
        void toggleLastCode(MathedTextCodes t);
        ///
        MathedTextCodes getLastCode() const;
+
+       /// true iff cursor points to data
+       bool hasData(MathedArray const &);
        
-protected:
+//protected:
        ///
        bool macro_mode;
        
index 48d2a6e523ca3f877e8f32ed54fa8df069f6e9fb..d4dc10b1a519cd50d8e1547f1800a4746e95a787 100644 (file)
@@ -21,6 +21,7 @@
 #pragma implementation
 #endif
 
+#include "debug.h"
 #include "math_iter.h"
 #include "math_inset.h"
 #include "symbol_def.h"
@@ -37,7 +38,8 @@ int MathedInset::workWidth;
 
 
 MathedInset::MathedInset(string const & nm, short ot, short st)
-       : name(nm), objtype(ot), width(0), ascent(0), descent(0), size_(st) 
+       : name(nm), objtype(ot), width(0), ascent(0), descent(0),
+         size_(st)
 {}
 
 
@@ -60,6 +62,10 @@ void MathedInset::drawStr(Painter & pain, short type, int siz,
        pain.text(x, y, st, mf);
 }
 
+void MathedInset::substitute(MathMacro *)
+{
+}
+
 
 int MathedInset::Ascent() const
 {
@@ -160,7 +166,6 @@ void MathedInset::size(short s)
        size_ = s;
 }
 
-
 void MathedInset::incSize()
 {
        ++size_;
index d17d0a90c90eec92beff4a45bb73fdf68439a396..484806415f25de42dee559e125ea898d84221ef9 100644 (file)
@@ -30,6 +30,7 @@
 #include "symbol_def.h"
 
 class Painter;
+class MathMacro;
 
 /** Abstract base class for all math objects.
     A math insets is for use of the math editor only, it isn't a
@@ -50,6 +51,8 @@ public:
        virtual void Write(std::ostream &, bool fragile) = 0;
        /// Reproduces itself
        virtual MathedInset * Clone() = 0;
+       /// Reproduces itself with macro arguments substituted
+       virtual void substitute(MathMacro * macro);
        /// Compute the size of the object
        virtual void Metrics() = 0; 
        /// 
index 513e9a8713af3a7cf28316419768acd767341f9b..8e3e7ef7a4640dccf87fe791e50518a81bd430c9 100644 (file)
@@ -60,6 +60,11 @@ void MathedIter::fcode(short c) const
        fcode_ = c; 
 }
 
+byte MathedIter::at() const
+{
+       return (*array)[pos]; 
+}
+
 byte MathedIter::at(int p) const
 {
        return (*array)[p]; 
index 4d753644fce743816150d0acbd5a2fec6034deb6..813b9fe86e218eb59aab569a0b2a415cd459d664 100644 (file)
@@ -111,6 +111,8 @@ public:
        ///
        MathedArray * GetData() const;
        ///
+       byte at() const;
+       ///
        byte & at(int pos);
        ///
        byte at(int pos) const;
index eb83d1587af13808aa786611ccfc7ed4a084b478..22ba0d5365c3b1783b1c2bad8344665e85871d71 100644 (file)
@@ -34,9 +34,9 @@
 #include "mathed/support.h"
 #include "math_macrotemplate.h"
 #include "macro_support.h"
+#include "Painter.h"
 
-using std::ostream;
-using std::endl;
+using namespace std;
 
 ostream & operator<<(ostream & o, MathedTextCodes mtc)
 {
@@ -44,25 +44,15 @@ ostream & operator<<(ostream & o, MathedTextCodes mtc)
 }
 
 
-MathMacro::MathMacro(boost::shared_ptr<MathMacroTemplate> const & t)
-       : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO),
-         tmplate_(t)
+MathMacro::MathMacro(MathMacroTemplate const & t)
+       : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
+         tmplate_(const_cast<MathMacroTemplate *>(&t)),
+               args_(t.nargs()),
+               idx_(-1)
 {
-       //nargs_ = tmplate_->getNoArgs();
-       int const n = tmplate_->getNoArgs();
-       
-       tcode_ = tmplate_->getTCode();
-
-       for (int i = 0; i < n; ++i) {
-               args_.push_back(MathMacroArgument(t->args_[i]));
-       }
-       //for (int i = 0; i < nargs_; ++i) {
-       //      MathMacroArgument * ma = new MathMacroArgument(*t->args_[i]);
-       //      args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
-       //}
-       
-       idx_ = 0;
-       SetName(tmplate_->GetName());
+       array = tmplate_->GetData();
+       for (int i = 0; i < nargs(); ++i) 
+               args_[i].reset(new MathParInset);
 }
 
 
@@ -72,129 +62,229 @@ MathedInset * MathMacro::Clone()
 }
 
 
+void MathMacro::expand()
+{
+       expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
+       expanded_->substitute(this);
+}
+
+
+
+MathParInset const * MathMacro::arg(int i) const
+{
+       if (i < 0 || i >= nargs()) {
+               lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
+               lyx::Assert(0);
+               return 0;
+       }
+
+       return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
+}
+
+MathParInset * MathMacro::arg(int i) 
+{
+       if (i < 0 || i >= nargs()) {
+               lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
+               lyx::Assert(0);
+               return 0;
+       }
+
+       return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
+}
+
+
+MathMacroTemplate * MathMacro::tmplate() const
+{
+       return const_cast<MathMacroTemplate *>(tmplate_);
+}
+
+
+extern bool is_mathcursor_inside(MathParInset *);
+
+
 void MathMacro::Metrics()
 {
-       for (unsigned int i = 0; i < args_.size(); ++i) 
-               tmplate_->args_[i] = getArg(i);
-       tmplate_->SetStyle(size());
-       tmplate_->Metrics();
-       width = tmplate_->Width();
-       ascent = tmplate_->Ascent();
-       descent = tmplate_->Descent();
+
+       if (is_mathcursor_inside(this)) {
+
+               tmplate_->Metrics();
+               width   = tmplate_->Width()   + 4;
+               ascent  = tmplate_->Ascent()  + 2;
+               descent = tmplate_->Descent() + 2;
+
+               width +=  mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
+
+               for (int i = 0; i < nargs(); ++i) {
+                       MathParInset * p = arg(i);
+                       p->Metrics();
+                       if (p->Width() + 30 > width) 
+                               width = p->Width() + 30;
+                       descent += p->Height() + 10;
+               }
+       } else {
+               expand();
+               expanded_->Metrics();
+               width   = expanded_->Width()   + 4;
+               ascent  = expanded_->Ascent()  + 2;
+               descent = expanded_->Descent() + 2;
+
+       }
 }
 
 
 void MathMacro::draw(Painter & pain, int x, int y)
 {
-       xo(x);
-       yo(y);
-       Metrics();
-       tmplate_->SetStyle(size());
-       tmplate_->draw(pain, x, y);
+       LColor::color col;
+
+       if (is_mathcursor_inside(this)) {
+               int h = y + Descent() - 2;
+               for (int i = nargs() - 1; i >= 0; --i) {
+                       MathParInset * p = arg(i);
+                       h -= p->Descent() + 5;
+                       p->draw(pain, x + 30, h);
+                       char str[] = "#1:";
+                       str[1] += i;
+                       drawStr(pain, LM_TC_TEX, size(), x + 1, h, str);
+                       h -= p->Ascent() + 5;
+               }
+
+               h -= tmplate_->Descent();
+               int w =  mathed_string_width(LM_TC_TEXTRM, size(), GetName());
+               drawStr(pain, LM_TC_TEXTRM, size(), x + 2, h, GetName());
+               tmplate_->draw(pain, x + w + 12, h);
+
+               col = LColor::red;
+       } else {
+               expanded_->draw(pain, x + 2, y - 1);
+               col = LColor::black;
+       }
+
+       int w = Width();
+       int a = Ascent();
+       int h = Height();
+       pain.rectangle(x, y - a, w, h, col);
 }
 
 
 bool MathMacro::setArgumentIdx(int i)
 {
-       if (i >= 0 && 0 < (args_.size() - i)) {
+       if (i >= 0 && 0 < (nargs() - i)) {
                idx_ = i;
                return true;
        } else
                return false;
+       idx_ = i;
+       return true;
 }
 
 
 int MathMacro::getArgumentIdx() const 
 { 
+       //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
        return idx_;
 }
 
 
 int MathMacro::getMaxArgumentIdx() const 
 { 
-       return args_.size() - 1;
+       return nargs() - 1;
+}
+
+
+
+int MathMacro::nargs() const 
+{ 
+       return args_.size();
 } 
 
 
 MathedArray & MathMacro::GetData() 
 { 
-       return args_[idx_].GetData(); 
+       //lyxerr << "MathMacro::GetData: " << *this << endl;
+       return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
 } 
 
 
 MathedArray const & MathMacro::GetData() const
 { 
-       return args_[idx_].GetData(); 
+       //lyxerr << "MathMacro::GetData: " << *this << endl;
+       return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
 } 
 
 
 int MathMacro::GetColumns() const
 {
-       return tmplate_->getMacroPar(idx_)->GetColumns();
+       return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
 }
 
 
 void MathMacro::GetXY(int & x, int & y) const
 {
-       const_cast<MathMacro*>(this)->Metrics();
-       tmplate_->GetMacroXY(idx_, x, y);
+       if (idx_ >= 0)
+               arg(idx_)->GetXY(x, y);
+       else
+               MathParInset::GetXY(x, y);
 }
 
 
 bool MathMacro::Permit(short f) const
 {
-       return (args_.size() > 0) ?
-               tmplate_->getMacroPar(idx_)->Permit(f) :
-               MathParInset::Permit(f);
+       return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
 }
 
 
 void MathMacro::SetFocus(int x, int y)
 {
-       Metrics();
-       tmplate_->SetMacroFocus(idx_, x, y);
+       idx_ = -1;
+       for (int i = 0; i < nargs(); ++i) {
+               if (arg(i)->Inside(x, y)) {
+                       idx_ = i;
+                       break;
+               }
+       }
+}
+
+void MathMacro::setData(MathedArray const & a, int i)
+{
+       arg(i)->setData(a);
 }
 
 
 void MathMacro::setData(MathedArray const & a)
 {
-       args_[idx_].setData(a);
+       if (idx_ >= 0)
+               arg(idx_)->setData(a);
+       else
+               array = a;
 }
 
 
 MathedTextCodes MathMacro::getTCode() const
 {
-       return tcode_;
+       return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
+       //return LM_TC_INSET;
 }
        
+void MathMacro::dump(ostream & os) const
+{
+       os << "\n macro: '" << this << "'\n";
+       os << " name: '" << name << "'\n";
+       os << " idx: '" << idx_ << "'\n";
+       os << " data: '" << array << "'\n";
+       os << " nargs: '" << nargs() << "'\n";
+       for (int i = 0; i < nargs(); ++i)
+               os << "  " << arg(i) << ": " << arg(i)->GetData() << endl;
+       os << endl;
+}
 
 void MathMacro::Write(ostream & os, bool fragile)
 {
        os << '\\' << name;
-
-       int const n = args_.size();
-       
-       if (n > 0) {
+       for (int i = 0; i < nargs(); ++i) {
                os << '{';
-               
-               for (int i = 0; i < n; ++i) {
-                       array = args_[i].GetData();
-                       MathParInset::Write(os, fragile);
-                       if (i < n - 1)  
-                               os << "}{";
-               }
+               arg(i)->Write(os, fragile);
                os << '}';
-       } else
+       }
+       if (nargs() == 0) 
                os << ' ';
 }
-
-
-MathMacroArgument const & MathMacro::getArg(int i) const
-{
-       return args_[i];
-}
-//boost::shared_ptr<MathMacroArgument> MathMacro::getArg(int i)
-//{
-//     return args_[i];
-//}
-
index 59c62467600c815621e724f7e3c9d70dde543dde..45fc2c91e9ffd5d16dfb128bde8eb206232690ca 100644 (file)
@@ -22,6 +22,7 @@
 #endif
 
 #include <vector>
+#include <iosfwd>
 #include <boost/smart_ptr.hpp>
 
 #include "math_parinset.h"
@@ -36,9 +37,8 @@ class MathMacroTemplate;
  */
 class MathMacro : public MathParInset {
 public:
-       /// A macro can only be built from an existing template
-       explicit
-       MathMacro(boost::shared_ptr<MathMacroTemplate> const &);
+       /// A macro can be built from an existing template
+       explicit MathMacro(MathMacroTemplate const &);
        ///
        void draw(Painter &, int, int);
        ///
@@ -47,13 +47,15 @@ public:
        MathedInset * Clone();
        ///
        void Write(std::ostream &, bool fragile);
-       ///
+       /// Index 0 is the template, index 1..nargs() are the parameters
        bool setArgumentIdx(int);
        ///
        int getArgumentIdx() const;
        ///
        int getMaxArgumentIdx() const;
        ///
+       int nargs() const;
+       ///
        int GetColumns() const;
        ///
        void GetXY(int &, int &) const;
@@ -66,23 +68,38 @@ public:
        ///
        void setData(MathedArray const &);
        ///
+       void setData(MathedArray const &, int);
+       ///
        MathedTextCodes getTCode() const;
        ///
        bool Permit(short) const;
        ///
-       MathMacroArgument const & getArg(int i) const;
-       //boost::shared_ptr<MathMacroArgument> getArg(int i);
-private:
+       void expand();
        ///
-       boost::shared_ptr<MathMacroTemplate> tmplate_;
+       void dump(ostream & os) const;
        ///
-       //std::vector<boost::shared_ptr<MathMacroArgument> > args_;
-       std::vector<MathMacroArgument> args_;
+       MathParInset const * arg(int) const;
        ///
-       int idx_;
+       MathParInset * arg(int);
        ///
-       //int nargs_;
+       MathMacroTemplate * tmplate() const;
+private:
+       ///
+       MathMacroTemplate * tmplate_;
+       /// our arguments
+       std::vector< boost::shared_ptr<MathParInset> > args_;
+       /// the expanded version fror drawing
+       boost::shared_ptr<MathParInset> expanded_;
        ///
-       MathedTextCodes tcode_;
+       int idx_;
+
+       /// unimplemented
+       void operator=(MathMacro const &);
 };
+
+inline ostream & operator<<(ostream & os, MathMacro const & m)
+{
+       m.dump(os);
+       return os;
+}
 #endif
index 58d43134c9c7eb511c4f1e3f0212ce0bcb5fb822..cbcad44b00a34fdb964dd25acabf40e887d308c1 100644 (file)
@@ -4,66 +4,63 @@
 #pragma implementation
 #endif
 
+#include "math_macro.h"
 #include "math_macroarg.h"
 #include "mathed/support.h"
 #include "Lsstream.h"
+#include "debug.h"
 
 
-MathMacroArgument::MathMacroArgument(int n)
-       : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO_ARG),
-         expnd_mode_(false), number_(n)
-{}
+using namespace std;
 
+MathMacroArgument::MathMacroArgument(int n)
+       : MathedInset(string(), LM_OT_MACRO_ARG, LM_ST_TEXT),
+               number_(n)
+{
+       if (n < 1 || n > 9) {
+               lyxerr << "MathMacroArgument::MathMacroArgument: wrong Argument id: "
+                       << n << endl;
+               lyx::Assert(0);
+       }
+}
 
 MathedInset * MathMacroArgument::Clone()
 {
+       //return new MathMacroArgument(*this);
        return this;
 }
 
-
-void MathMacroArgument::setExpand(bool e)
+int MathMacroArgument::number() const
 {
-       expnd_mode_ = e;
+       return number_;
 }
 
-
-bool MathMacroArgument::getExpand() const
+void MathMacroArgument::substitute(MathMacro * /*m*/)
 {
-       return expnd_mode_;
+       lyxerr << "Calling MathMacroArgument::substitute!\n";
+       //return m->arg(number_)->Clone();
 }
 
 
-void MathMacroArgument::draw(Painter & pain, int x, int baseline)
+void MathMacroArgument::draw(Painter & pain, int x, int y)
 {
-       if (expnd_mode_) {
-               MathParInset::draw(pain, x, baseline);
-       } else {
-               std::ostringstream ost;
-               ost << '#' << number_;
-               drawStr(pain, LM_TC_TEX, size(), x, baseline, ost.str().c_str());
-       }
+       char str[] = "#0";
+       str[1] += number_; 
+       drawStr(pain, LM_TC_TEX, size(), x, y, str);
 }
 
+
 void MathMacroArgument::Metrics()
 {
-       if (expnd_mode_) {
-               MathParInset::Metrics();
-       } else {
-               std::ostringstream ost;
-               ost << '#' << number_;
-               width = mathed_string_width(LM_TC_TEX, size(),
-                                           ost.str().c_str());
-               mathed_string_height(LM_TC_TEX, size(), ost.str().c_str(),
-                                    ascent, descent);
-       }
+       char str[] = "#0";
+       str[1] += number_; 
+       width = mathed_string_width(LM_TC_TEX, size(), str);
+       mathed_string_height(LM_TC_TEX, size(), str, ascent, descent);
 }
 
 
-void MathMacroArgument::Write(std::ostream & os, bool fragile)
+void MathMacroArgument::Write(std::ostream & os, bool /*fragile*/)
 {
-       if (expnd_mode_) {
-               MathParInset::Write(os, fragile);
-       } else {
-               os << '#' << number_ << ' ';
-       }
+       os << '#' << number_ << ' ';
 }
+
index a8eb021aa400df25098295e3442a4ee7be9819de..ba2290df30718b4e22528b7d109f44a995206f28 100644 (file)
@@ -2,7 +2,7 @@
 #ifndef MATHMACROARGUMENT_H
 #define MATHMACROARGUMENT_H
 
-#include "math_parinset.h"
+#include "math_inset.h"
 
 #ifdef __GNUG__
 #pragma interface
 /** A macro argument
     \author Alejandro Aguilar Sierra
 */
-class MathMacroArgument : public MathParInset {
+class MathMacroArgument : public MathedInset {
 public:
        ///
-       explicit
-       MathMacroArgument(int);
+       explicit MathMacroArgument(int);
        ///
        MathedInset * Clone();
        ///
+       void substitute(MathMacro *);
+       ///
        void Metrics();
        ///
        void draw(Painter &, int x, int baseline);
        ///
        void Write(std::ostream &, bool fragile);
-       /// Is expanded or not
-       void setExpand(bool e);
-       /// Is expanded or not
-       bool getExpand() const;
-private:
-       ///
-       bool expnd_mode_;
        ///
+       int number() const;
+
+
+private:
+       /// A number between 1 and 9
        int number_;
 };
+
 #endif
index baaff99c756aa665b281c6960842fe2112ba4f5e..c5ab680d3933c6f5b0cec651ee71e820f8a7ff88 100644 (file)
 
 using std::endl;
 
-MathMacroTable MathMacroTable::mathMTable;
+MathMacroTable::table_type MathMacroTable::macro_table;
 
 bool MathMacroTable::built = false;
 
 
-MathMacro * MathMacroTable::createMacro(string const & name) const
+void MathMacroTable::dump()
 {
-       boost::shared_ptr<MathMacroTemplate> mt = getTemplate(name);
-       return (mt.get()) ? new MathMacro(mt) : 0;
+       cerr << "\n------------------------------------------\n";
+       table_type::const_iterator it;
+       for (it = macro_table.begin(); it != macro_table.end(); ++it)
+               cerr << it->first << ": " << it->second->GetData() << endl;
+       cerr << "------------------------------------------\n";
 }
 
 
-boost::shared_ptr<MathMacroTemplate> const
-MathMacroTable::getTemplate(string const & name) const
+MathMacroTemplate &
+MathMacroTable::provideTemplate(string const & name, int na)
 {
-       table_type::const_iterator cit = macro_table.find(name);
-       if (cit != macro_table.end()) return (*cit).second;
-       return boost::shared_ptr<MathMacroTemplate>();
+       if (!built)
+               builtinMacros();
+       
+       if (macro_table.find(name) == macro_table.end())
+               macro_table.insert(make_pair(name, new MathMacroTemplate(name, na)));
+       
+       return *(macro_table.find(name)->second);
 }
 
 
-void
-MathMacroTable::addTemplate(boost::shared_ptr<MathMacroTemplate> const & m)
+MathMacroTemplate &
+MathMacroTable::provideTemplate(string const & name)
 {
-       lyx::Assert(m.get());
-       macro_table[m->GetName()] = m;
+       if (!built)
+               builtinMacros();
+       
+       return *macro_table[name];
+}
+
+
+bool MathMacroTable::hasTemplate(string const & name)
+{
+       if (!built)
+               builtinMacros();
+       
+       return macro_table.find(name) != macro_table.end();
+}
+
+
+MathMacro * MathMacroTable::cloneTemplate(string const & name)
+{
+       return new MathMacro(provideTemplate(name));
 }
 
 
@@ -55,26 +79,52 @@ void MathMacroTable::builtinMacros()
     
        // This macro doesn't have arguments
        {
-               boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("notin", 0));
-               addTemplate(m);
-               MathedArray array;
-               MathedIter iter(&array);
+               MathMacroTemplate & m = provideTemplate("notin", 0);
+               MathedIter iter(&m.GetData());
                iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not),
                                 LM_TC_INSET);
-               m->setData(array);
        }
-    
+
        // These two are only while we are still with LyX 2.x
        {
-               boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("emptyset", 0));
-               addTemplate(m); 
-               MathedArray array;
-               MathedIter iter(&array);
-               iter.insertInset(new MathAccentInset('O', LM_TC_RM, LM_not),
+               MathMacroTemplate & m = provideTemplate("emptyset", 0);
+               MathedIter iter(&m.GetData());
+               iter.insertInset(new MathAccentInset('0', LM_TC_RM, LM_not),
                                 LM_TC_INSET);
-               m->setData(array);
        }
-    
+
+       {
+               MathMacroTemplate & m = provideTemplate("perp", 0);
+               MathedIter iter(&m.GetData());
+               iter.insert(LM_bot, LM_TC_BOP);
+       }
+
+       // binom has two arguments
+       {
+               MathMacroTemplate & m = provideTemplate("binom", 2);
+               MathedIter iter(&m.GetData());
+
+               MathParInset * inset = new MathDelimInset('(', ')');
+               iter.insertInset(inset, LM_TC_ACTIVE_INSET);
+
+               MathedArray array2;
+               MathedIter iter2(&array2);
+               MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
+               iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
+               frac->setData(array2);
+
+               MathedArray array3;
+               MathedIter iter3(&array3);
+               iter3.insertInset(new MathMacroArgument(1), LM_TC_INSET);
+
+               MathedArray array4;
+               MathedIter iter4(&array4);
+               iter4.insertInset(new MathMacroArgument(2), LM_TC_INSET);
+
+               frac->SetData(array3, array4);
+       }
+
+/*    
        {
                boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("perp", 0));
                addTemplate(m);
@@ -106,4 +156,5 @@ void MathMacroTable::builtinMacros()
                iter4.insertInset(m->getMacroPar(1), LM_TC_INSET);
                frac->SetData(array, array2);
        }
+*/
 }
index 5da3a0940c2d10e3655004e2f3f846035c101e65..c432f5c37d35085a3f8cbc754da76f692ae71a2a 100644 (file)
@@ -5,36 +5,35 @@
 #include <map>
 #include "LString.h"
 
-#include <boost/utility.hpp>
-#include <boost/smart_ptr.hpp>
-
 #ifdef __GNUG__
 #pragma interface
 #endif
 
-class MathMacroTemplate;
+
 class MathMacro;
+class MathMacroTemplate;
 
 ///
-class MathMacroTable : boost::noncopyable {
+struct MathMacroTable {
 public:
        ///
-       void addTemplate(boost::shared_ptr<MathMacroTemplate> const &);
+       static MathMacroTemplate & provideTemplate(string const &, int);
        ///
-       MathMacro * createMacro(string const &) const;
+       static MathMacroTemplate & provideTemplate(string const &);
        ///
-       boost::shared_ptr<MathMacroTemplate> const
-       getTemplate(string const &) const;
+       static bool hasTemplate(string const &);
        ///
-       void builtinMacros();
+       static MathMacro * cloneTemplate(string const &);
+private:
        ///
-       static MathMacroTable mathMTable;
+       static void builtinMacros();
        ///
        static bool built;
-private:
        ///
-       typedef std::map<string, boost::shared_ptr<MathMacroTemplate> > table_type;
+       typedef std::map<string, MathMacroTemplate *> table_type;
+       //
+       static table_type macro_table;
        ///
-       table_type macro_table;
+       static void dump();
 };
 #endif
index 3a41679760eaa5e68fba08d7369575de1a262b34..cfb1b36b80b01465284ab6b9fa408766a2dace9a 100644 (file)
 #include "macro_support.h"
 #include "support/LOstream.h"
 #include "support/LAssert.h"
+#include "debug.h"
+#include "Painter.h"
 
-using std::ostream;
+using namespace std;
 
+MathMacroTemplate::MathMacroTemplate() :
+       MathParInset(LM_ST_TEXT, "undefined", LM_OT_MACRO),
+       na_(0), users_()
+{}
 
-MathMacroTemplate::MathMacroTemplate(string const & nm, int na):
-       MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
-       edit_(false),
-       nargs_(na)
-{
-       if (nargs_ > 0) {
-               tcode_ = LM_TC_ACTIVE_INSET;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_.push_back(MathMacroArgument(i + 1));
-               }
-               //for (int i = 0; i < nargs_; ++i) {
-               //      MathMacroArgument * ma = new MathMacroArgument(i + 1);
-               //      args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
-               //}
-       } else {
-               tcode_ = LM_TC_INSET;
-               // Here is  nargs != args_.size()
-               //args = 0;
-       }
-}
-
-
-void  MathMacroTemplate::setTCode(MathedTextCodes t)
-{
-       tcode_ = t;
-}
-
-
-MathedTextCodes MathMacroTemplate::getTCode() const
-{
-       return tcode_;
-}
+MathMacroTemplate::MathMacroTemplate(string const & nm, int na) :
+       MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
+       na_(na), users_()
+{}
 
 
-int MathMacroTemplate::getNoArgs() const
+int MathMacroTemplate::nargs() const
 {
-       return nargs_;
+       return na_;
 }
 
 
-void MathMacroTemplate::setEditMode(bool ed)
-{
-       if (ed) {
-               edit_ = true;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(false);
-               }
-       } else {
-               edit_ = false;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(true);
-               }
-       }
-}
-
-
-void MathMacroTemplate::draw(Painter & pain, int x, int y)
-{
-       int x2;
-       int y2;
-       bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
-       if (edit_) {
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(false);
-               }
-               x2 = x;
-               y2 = y;
-       } else {
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(true);
-               }
-               x2 = xo();
-               y2 = yo();
-       }
-       MathParInset::draw(pain, x, y);
-       xo(x2);
-       yo(y2);
-       for (int i = 0; i < nargs_; ++i) {
-               args_[i].setExpand(expnd);
-       }
-}
-
-
-void MathMacroTemplate::Metrics()
-{
-       bool const expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
-    
-       if (edit_) {
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(false);
-               }
-       } else {
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(true);
-               }
-       }
-       MathParInset::Metrics();
-       for (int i = 0; i < nargs_; ++i) {
-               args_[i].setExpand(expnd);
-       }
-}
-
-
-void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
+void MathMacroTemplate::WriteDef(ostream & os, bool fragile) const
 {
        os << "\n\\newcommand{\\" << name << "}";
 
-       if (nargs_ > 0 ) 
-               os << "[" << nargs_ << "]";
+       if (na_ > 0 )
+               os << "[" << na_ << "]";
 
        os << "{";
-
-       for (int i = 0; i < nargs_; ++i) {
-               args_[i].setExpand(false);
-       }
-
-       Write(os, fragile);
+#ifdef WITH_WARNINGS
+#warning stupid cast
+#endif
+       const_cast<MathMacroTemplate *>(this)->Write(os, fragile);
        os << "}\n";
 }
 
 
-void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
-{
-       args_[i].GetXY(x, y);
-}
-
-
-MathParInset * MathMacroTemplate::getMacroPar(int i) const
-{
-       if (i >= 0 && i < nargs_) {
-               MathParInset * p = const_cast<MathParInset *>
-                       (static_cast<MathParInset const *>(&args_[i]));
-               lyx::Assert(p);
-               return p;
-       } else 
-               return 0;
-}
-
-void MathMacroTemplate::setMacroPar(int i, MathedArray const & ar)
+void MathMacroTemplate::Metrics()
 {
-       if (i >= 0 && i < nargs_)
-               args_[i].setData(ar);
+       MathParInset::Metrics();
+       width   += 4;
+       ascent  += 2;
+       descent += 2;
 }
 
 
-
-void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
+void MathMacroTemplate::draw(Painter & pain, int x, int y)
 {
-       for (int i = 0; i < nargs_; ++i) {
-               if (args_[i].Inside(x, y)) {
-                       idx = i;
-                       break;
-               }
-       }
+       MathParInset::draw(pain, x + 2, y + 1);
+       int w = Width();
+       int a = Ascent();
+       int h = Height();
+       pain.rectangle(x, y - a, w, h, LColor::blue);
 }
index e30ef406e893a72f5c4ea2b21243392184aff401..12bdbc0cb3df0d5c9b0e951f84a007611c3b29a1 100644 (file)
@@ -2,13 +2,9 @@
 #ifndef MATHMACROTEMPLATE
 #define MATHMACROTEMPLATE
 
-#include <vector>
-
-#include <boost/utility.hpp>
-//#include <boost/smart_ptr.hpp>
+#include <set>
 
 #include "math_parinset.h"
-#include "math_macroarg.h"
 
 #ifdef __GNUG__
 #pragma interface
@@ -19,44 +15,32 @@ class MathMacro;
 /** This class contains the macro definition
     \author Alejandro Aguilar Sierra
  */
-class MathMacroTemplate : public MathParInset, boost::noncopyable {
+//class MathMacroTemplate : public MathParInset, boost::noncopyable 
+
+class MathMacroTemplate : public MathParInset {
 public:
-       friend class MathMacro;
-       
-       /// A template constructor needs all the data
-       explicit
-       MathMacroTemplate(string const &, int na);
        ///
-       void draw(Painter &, int, int);
+       MathMacroTemplate();
        ///
-       void Metrics();
+       MathMacroTemplate(std::string const & name, int nargs);
        ///
-       void WriteDef(std::ostream &, bool fragile);
-       /// useful for special insets
-       void setTCode(MathedTextCodes t);
-       ///
-       MathedTextCodes getTCode() const;
+       void WriteDef(std::ostream &, bool fragile) const;
        /// Number of arguments
-       int getNoArgs() const;
-       ///
-       void GetMacroXY(int, int &, int &) const;
-       ///
-       MathParInset * getMacroPar(int) const;
+       int nargs() const;
        ///
-       void setMacroPar(int, MathedArray const &);
-       ///
-       void SetMacroFocus(int &, int, int);
+       void draw(Painter &, int, int);
        ///
-       void setEditMode(bool);
+       void Metrics();
 private:
-       /// Are we in edit mode or not?
-       bool edit_;
        ///
-       MathedTextCodes tcode_;
+       int na_;
        ///
-       //std::vector<boost::shared_ptr<MathMacroArgument> > args_;
-       std::vector<MathMacroArgument> args_;
-       ///
-       int nargs_;
+       std::set<MathMacro *> users_;
+
+       /// unimplemented
+       void operator=(MathMacroTemplate const &);
+       /// unimplemented
+       MathMacroTemplate(MathMacroTemplate const &);
 };
+
 #endif
index 6d7021a7295d7f574bd5d19278e47a5f6b50946d..136c288b9651e8b9d5f670c935ceaa2620183642 100644 (file)
@@ -8,10 +8,10 @@
 #include "math_iter.h"
 #include "array.h"
 #include "math_xiter.h"
+#include "math_parser.h"
 #include "LColor.h"
 #include "mathed/support.h"
 #include "Painter.h"
-#include "math_parser.h"
 #include "math_rowst.h"
 #include "math_parinset.h"
 #include "debug.h"
@@ -26,14 +26,23 @@ MathedRowContainer & MathParInset::getRowSt()
        return row_;
 }
 
+string MathParInset::label() const
+{
+       if (row_.size() == 0) {
+               lyxerr << "Warning: Empty rowst when accessing label!\n";
+               return string();
+       }
+       return row_.back().getLabel();
+}
+
 
 MathParInset::MathParInset(short st, string const & nm, short ot)
        : MathedInset(nm, ot, st)
 {
-       ascent = 8;
-       width = 4;
+       ascent  = 8;
+       width   = 4;
        descent = 0;
-       flag = 1;
+       flag    = 1;
        if (objtype == LM_OT_SCRIPT)
                flag |= LMPF_SCRIPT;
 }
@@ -44,6 +53,13 @@ MathedInset * MathParInset::Clone()
        return new MathParInset(*this);
 }
 
+void MathParInset::substitute(MathMacro * m)
+{
+       //lyxerr << "called: MathParInset::substitute, m: " << m << endl;
+       array.substitute(m);
+}
+
+
 
 void MathParInset::setData(MathedArray const & a)
 {
@@ -61,26 +77,24 @@ void MathParInset::setData(MathedArray const & a)
 }
 
 
-void 
-MathParInset::draw(Painter & pain, int x, int y)
+void MathParInset::draw(Painter & pain, int x, int y)
 {
        byte cxp = 0;
-       int xp = 0;
-       int asc = df_asc;
-       int des = 0;
+       int xp   = 0;
+       int asc  = df_asc;
+       int des  = 0;
        bool limits = false;
        
        xo_ = x;
        yo_ = y;
        MathedXIter data(this);
        if (array.empty()) {
-               //MathedXIter data(this);
                data.GetPos(x, y);
                pain.rectangle(x, y - df_asc, df_width, df_asc,
                               LColor::mathline);
                return;
        }
-       //MathedXIter data(this);
+
        data.GoBegin();
        while (data.OK()) {
                data.GetPos(x, y);
@@ -154,23 +168,23 @@ MathParInset::draw(Painter & pain, int x, int y)
 }
 
 
-void 
-MathParInset::Metrics()
+void MathParInset::Metrics()
 {
        byte cx;
        byte cxp = 0;
        int ls;
        int asc = df_asc;
        int des = 0;
-       int tb = 0;
+       int tb  = 0;
        int tab = 0;
        
        bool limits = false;
        
        ascent = df_asc;//mathed_char_height(LM_TC_VAR, size, 'I', asc, des); 
-       width = df_width;
+       width  = df_width;
        descent = 0;
-       if (array.empty()) return;
+       if (array.empty())
+               return;
        
        ascent = 0;
        MathedXIter data(this);
@@ -181,8 +195,10 @@ MathParInset::Metrics()
                        string const s = data.GetString();
                        mathed_string_height(data.fcode(),
                                             size(), s, asc, des);
-                       if (asc > ascent) ascent = asc;
-                       if (des > descent) descent = des;
+                       if (asc > ascent)
+                               ascent = asc;
+                       if (des > descent)
+                               descent = des;
                        limits = false;
                        mathed_char_height(LM_TC_CONST, size(), 'y', asc, des);
                } else if (MathIsInset(cx)) {
@@ -198,8 +214,10 @@ MathParInset::Metrics()
                                asc = p->Ascent();
                                des = p->Descent();
                        }
-                       if (asc > ascent) ascent = asc;
-                       if (des > descent) descent = des;
+                       if (asc > ascent)
+                               ascent = asc;
+                       if (des > descent)
+                               descent = des;
                        if (cx!= LM_TC_UP && cx!= LM_TC_DOWN)
                                limits = p->GetLimits();
                        data.Next();
@@ -208,7 +226,8 @@ MathParInset::Metrics()
                        int y;
                        data.GetIncPos(x, y);
                        if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
-                               if (ascent < df_asc) ascent = df_asc;
+                               if (ascent < df_asc)
+                                       ascent = df_asc;
                                tb = x;
                        }
                        data.setTab(x - tb, tab);
@@ -222,7 +241,8 @@ MathParInset::Metrics()
                                int y;
                                data.GetIncPos(x, y);
                                if (data.IsFirst() || cxp == LM_TC_TAB || cxp == LM_TC_CR) {
-                                       if (ascent < df_asc) ascent = df_asc;
+                                       if (ascent < df_asc)
+                                               ascent = df_asc;
                                        tb = x;
                                } 
                                data.setTab(x - tb, tab);
@@ -232,7 +252,8 @@ MathParInset::Metrics()
                                        int y;
                                        data.GetIncPos(x, y);
                                        data.setTab(x, tab);
-                                       if (ascent < df_asc) ascent = df_asc;
+                                       if (ascent < df_asc)
+                                               ascent = df_asc;
                                } 
                        tb = tab = 0;
                        data.subMetrics(ascent, descent);
@@ -251,7 +272,8 @@ MathParInset::Metrics()
        // No matter how simple is a matrix, it is NOT a subparagraph
        if (isMatrix()) {
                if (cxp == LM_TC_TAB) {
-                       if (ascent<df_asc) ascent = df_asc;
+                       if (ascent < df_asc)
+                               ascent = df_asc;
                        data.setTab(0, tab);
                } else {
                        data.setTab(width - tb, tab);
@@ -265,7 +287,9 @@ MathParInset::Metrics()
 
 void MathParInset::Write(ostream & os, bool fragile)
 {
-       if (array.empty()) return;
+       if (array.empty())
+               return;
+
        int brace = 0;
        latexkeys const * l;
        MathedIter data(&array);
@@ -275,9 +299,8 @@ void MathParInset::Write(ostream & os, bool fragile)
        
        if (!Permit(LMPF_FIXED_SIZE)) { 
                l = lm_get_key_by_id(size(), LM_TK_STY);
-               if (l) {
+               if (l)
                        os << '\\' << l->name << ' ';
-               }
        }
        while (data.OK()) {
                byte cx = data.GetChar();
@@ -308,8 +331,10 @@ void MathParInset::Write(ostream & os, bool fragile)
                                            (data.fcode() == LM_TC_SPECIAL))
                                                os << '\\';
                                        else {
-                                               if (c == '{') ++brace;
-                                               if (c == '}') --brace;
+                                               if (c == '{')
+                                                       ++brace;
+                                               if (c == '}')
+                                                       --brace;
                                        }
                                        if (c == '}' && data.fcode() == LM_TC_TEX && brace < 0) 
                                                lyxerr <<"Math warning: Unexpected closing brace."
@@ -320,7 +345,7 @@ void MathParInset::Write(ostream & os, bool fragile)
                        }
                        if (data.fcode()>= LM_TC_RM && data.fcode()<= LM_TC_TEXTRM)
                                os << '}';
-               } else     
+               } else {
                        if (MathIsInset(cx)) {
                                MathedInset * p = data.GetInset();
                                if (cx == LM_TC_UP)
@@ -331,7 +356,7 @@ void MathParInset::Write(ostream & os, bool fragile)
                                if (cx == LM_TC_UP || cx == LM_TC_DOWN)
                                        os << '}';
                                data.Next();
-                       } else
+                       } else {
                                switch (cx) {
                                case LM_TC_TAB:
                                {
@@ -363,6 +388,8 @@ void MathParInset::Write(ostream & os, bool fragile)
                                        lyxerr << "WMath Error: unrecognized code[" << cx << "]";
                                        return;
                                }     
+                       }
+               }
        }
        
        if (crow) {
@@ -402,23 +429,22 @@ void MathParInset::GetXY(int & x, int & y) const
 
 void MathParInset::UserSetSize(short sz)
 {
-   if (sz >= 0) {
-       size(sz);      
-       flag = flag & ~LMPF_FIXED_SIZE;
-   }
+       if (sz >= 0) {
+               size(sz);      
+               flag = flag & ~LMPF_FIXED_SIZE;
+       }
 }
 
 
 void MathParInset::SetStyle(short sz) 
 {
-    if (Permit(LMPF_FIXED_SIZE)) {
-       if (Permit(LMPF_SCRIPT)) 
-         sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
-       if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) {
-           ++sz;
-       } 
-       MathedInset::SetStyle(sz);
-    }
+       if (Permit(LMPF_FIXED_SIZE)) {
+               if (Permit(LMPF_SCRIPT)) 
+                       sz = (sz < LM_ST_SCRIPT) ? LM_ST_SCRIPT: LM_ST_SCRIPTSCRIPT;
+               if (Permit(LMPF_SMALLER) && sz < LM_ST_SCRIPTSCRIPT) 
+                       ++sz;
+               MathedInset::SetStyle(sz);
+       }
 }
 
 
index e414bf41aec387713ad85ef91b3ce08745ee8eae..731c7f781ce60f158659b890d53b41dddbc24df3 100644 (file)
@@ -24,6 +24,8 @@ public:
                     short ot = LM_OT_MIN);
        ///
        virtual MathedInset * Clone();
+       ///
+       virtual void substitute(MathMacro *);
        /// Draw the object on a drawable
        virtual void draw(Painter &, int x, int baseline);
        /// Write LaTeX code
@@ -76,7 +78,9 @@ public:
        int yo() const;
        ///
        void clear();
-protected:
+       ///
+       string label() const;
+//protected:
        /// Paragraph data is stored here
        MathedArray array;
        /// 
index 7f488420812511db0527699b6ee0bb3ddd776fa1..8c9e9f9bb7cc8323bc53535a30c7c3bc51f97135 100644 (file)
@@ -50,20 +50,20 @@ using std::istream;
 using std::endl;
 
 
-extern MathMatrixInset create_multiline(short int type, int cols);
+extern MathMatrixInset create_multiline(short int type, int cols);
 
 namespace {
 
 enum {
-       FLAG_BRACE      = 1,    //  A { needed
-       FLAG_BRACE_ARG  = 2,    //  Next { is argument
-       FLAG_BRACE_OPT  = 4,    //  Optional {
-       FLAG_BRACE_LAST = 8,    //  Last } ends the parsing process
-       FLAG_BRACK_ARG  = 16,   //  Optional [
-       FLAG_RIGHT      = 32,      //  Next right ends the parsing process
-       FLAG_END        = 64,      //  Next end ends the parsing process
-       FLAG_BRACE_FONT = 128,  //  Next } closes a font
-       FLAG_BRACK_END  = 256   // Next ] ends the parsing process
+       FLAG_BRACE      = 1,    //  A { needed
+       FLAG_BRACE_ARG  = 2,    //  Next { is argument
+       FLAG_BRACE_OPT  = 4,    //  Optional {
+       FLAG_BRACE_LAST = 8,    //  Last } ends the parsing process
+       FLAG_BRACK_ARG  = 16,   //  Optional [
+       FLAG_RIGHT      = 32,   //  Next right ends the parsing process
+       FLAG_END        = 64,   //  Next end ends the parsing process
+       FLAG_BRACE_FONT = 128,  //  Next } closes a font
+       FLAG_BRACK_END  = 256   //  Next ] ends the parsing process
 };
 
 
@@ -89,9 +89,6 @@ MathedInsetTypes mathed_env = LM_OT_MIN;
 } // namespace anon
 
 
-string mathed_label;
-
-
 int const latex_mathenv_num = 12;
 char const * latex_mathenv[latex_mathenv_num] = { 
        "math", 
@@ -147,14 +144,13 @@ bool yy_mtextmode= false;
 inline
 void mathPrintError(string const & msg) 
 {
-       lyxerr << "Line ~" << yylineno << ": Math parse error: "
-              << msg << endl;
+       lyxerr << "Line ~" << yylineno << ": Math parse error: " << msg << endl;
 }
 
 
 void LexInitCodes()
 {
-       for (int i = 0;  i <= 255; ++i) {
+       for (int i = 0; i <= 255; ++i) {
                if (isalpha(i))
                        lexcode[i] = LexAlpha;
                else if (isdigit(i))
@@ -201,8 +197,7 @@ char LexGetArg(char lf, bool accept_spaces = false)
                        if (!lf) 
                                lf = c;
                        else if (c != lf) {
-                               lyxerr << "Math parse error: unexpected '"
-                                      << c << "'" << endl;
+                               lyxerr << "Math parse error: unexpected '" << c << "'" << endl;
                                return '\0';
                        }
                        break;
@@ -213,8 +208,7 @@ char LexGetArg(char lf, bool accept_spaces = false)
                ((lf == '[') ? ']'
                 : ((lf == '(') ? ')' : 0));
        if (!rg) {
-               lyxerr << "Math parse error: unknown bracket '"
-                      << lf << "'" << endl;
+               lyxerr << "Math parse error: unknown bracket '" << lf << "'" << endl;
                return '\0';
        }
        char * p = &yytext[0];
@@ -321,7 +315,9 @@ int yylex(void)
                                        ++p;
                                }
                                *p = '\0';
-                               if (yyis->good()) yyis->putback(c);
+                               if (yyis->good())
+                                       yyis->putback(c);
+                               //lyxerr << "reading: '" << yytext.data() << "'\n";
                                latexkeys const * l = in_word_set (yytext.data(), strlen(yytext.data()));
                                if (l) {
                                        if (l->token == LM_TK_BEGIN || l->token == LM_TK_END) { 
@@ -366,9 +362,9 @@ int nestaccent[8];
 inline
 void setAccent(int ac)
 {
-       if (ac > 0 && accent < 8) {
+       if (ac > 0 && accent < 8)
                nestaccent[accent++] = ac;
-       else
+       else
          accent = 0;  // consumed!
 }
 
@@ -404,13 +400,21 @@ MathedInset * doAccent(MathedInset * p)
        return ac;
 }
 
+
+void do_insert(MathedIter & it, MathedInset * m, MathedTextCodes t)
+{
+       if (accent) 
+               it.insertInset(doAccent(m), t);
+       else
+               it.insertInset(m, t);
+}
+
 } // namespace anon
 
 
 /**
  */
-void mathed_parse(MathedArray & array, unsigned flags = 0,
-                           MathParInset ** mtx = 0)
+void mathed_parse(MathedArray & array, MathParInset * & par, unsigned flags)
 {
        int t = yylex();
        int tprev = 0;
@@ -419,16 +423,22 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
        static int size = LM_ST_TEXT;
        MathedTextCodes varcode = LM_TC_VAR;
        MathedInset * binset = 0;
-       static MathMacroTemplate * macro = 0;
        
+       string last_label;              // last label seen
+       bool   last_numbered = true;    // have we seen '\nonumber' lately? 
+
        int brace = 0;
        int acc_brace = 0;
        int acc_braces[8];
-       MathParInset * mt = (mtx) ? *mtx : 0;
 
        ++plevel;
        MathedIter data(&array);
        while (t) {
+               //lyxerr << "t: " << t << " par: " << par << " flags: " << flags;
+               //lyxerr << "label: '" << last_label << "' ";
+               //array.dump(lyxerr);
+               //lyxerr << "\n";
+
                if ((flags & FLAG_BRACE) && t != LM_TK_OPEN) {
                        if ((flags & FLAG_BRACK_ARG) && t == '[') {
                        } else {
@@ -441,42 +451,38 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                switch (t) {
                        
                case LM_TK_ALPHA:
-                       if (accent) {
-                               data.insertInset(doAccent(yylval.i, varcode),
-                                                LM_TC_INSET);
-                       } else
+                       if (accent) 
+                               data.insertInset(doAccent(yylval.i, varcode), LM_TC_INSET);
+                       else
                                data.insert(yylval.i, varcode);  //LM_TC_VAR);
                        break;
 
                case LM_TK_ARGUMENT:
-                       if (macro) {
-                               data.insertInset(macro
-                                                ->getMacroPar(yylval.i - 1),
-                                                LM_TC_INSET);
-                       } else {
-                               lyxerr[Debug::MATHED] << "mathed_parse: macro arg outside macro def." << endl;
-                       }
-                       
+               {
+                       data.insertInset(new MathMacroArgument(yylval.i), LM_TC_INSET);
                        break;
+               }
 
                case LM_TK_NEWCOMMAND:
                {
                        int na = 0; 
                        
                        LexGetArg('{');
-                       string const name(&yytext[1]);
+                       string name = &yytext[1];
                        
-                       // ugly trick to be removed soon (lyx3)
                        char const c = yyis->peek();
                        if (c == '[') {
                                LexGetArg('[');
                                na = lyx::atoi(yytext.data());
                        }  
-                       macro = new MathMacroTemplate(name, na);
+                       //lyxerr << "LM_TK_NEWCOMMAND: name: " << name << " " << na << endl;
+#ifdef WITH_WARNINGS
+#warning dirty
+#endif
+                       par->SetName(name);
+                       par->xo(na); // abuse xo
                        flags = FLAG_BRACE|FLAG_BRACE_LAST;
 
-                       *mtx = macro;
-                       macro->setData(array);
                        break;
                }
                
@@ -528,10 +534,9 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        }
                        if (brace == 0 && (flags & FLAG_BRACE_LAST)) {
                                --plevel;
-                               return;
-                       } else {
-                               data.insert('}', LM_TC_TEX);
+                               goto clean_up;
                        }
+                       data.insert('}', LM_TC_TEX);
                        break;
                
                case '[':
@@ -551,17 +556,16 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                case ']':
                        if (flags & FLAG_BRACK_END) {
                                --plevel;
-                               return;
-                       } else
-                               data.insert(']', LM_TC_CONST);
+                               goto clean_up;
+                       }
+                       data.insert(']', LM_TC_CONST);
                        break;
                
                case '^':
                {  
-                       MathParInset * p = new MathParInset(size, "",
-                                                           LM_OT_SCRIPT);
+                       MathParInset * p = new MathParInset(size, "", LM_OT_SCRIPT);
                        MathedArray ar;
-                       mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
+                       mathed_parse(ar, par, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
                        p->setData(ar);
                        // lyxerr << "UP[" << p->GetStyle() << "]" << endl;
                        data.insertInset(p, LM_TC_UP);
@@ -573,7 +577,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        MathParInset * p = new MathParInset(size, "",
                                                            LM_OT_SCRIPT);
                        MathedArray ar;
-                       mathed_parse(ar, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
+                       mathed_parse(ar, par, FLAG_BRACE_OPT|FLAG_BRACE_LAST);
                        p->setData(ar);
                        data.insertInset(p, LM_TC_DOWN);
                        break;
@@ -587,22 +591,29 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        break;
                
                case '&':    // Tab
-                       if ((flags & FLAG_END) && mt
-                           && data.getCol()<mt->GetColumns() - 1) {
-                               data.setNumCols(mt->GetColumns());
-                               data.insert('T', LM_TC_TAB);
-                       } else 
-                               mathPrintError("Unexpected tab");
-                       // debug info. [made that conditional -JMarc]
-                       if (lyxerr.debugging(Debug::MATHED))
-                               lyxerr << data.getCol() << " "
-                                      << mt->GetColumns() << endl;
+                       data.insert('T', LM_TC_TAB);
+#ifdef WITH_WARNINGS
+#warning look here
+#endif
+                       data.setNumCols(par->GetColumns());
                        break;
                
                case LM_TK_NEWLINE:
-                       if (mt && (flags & FLAG_END)) {
-                               if (mt->Permit(LMPF_ALLOW_CR)) {
-                                       mt->getRowSt().push_back();
+                       //lyxerr << "reading line " << par->getRowSt().size() << "\n";
+                       if (flags & FLAG_END) {
+                               if (par->Permit(LMPF_ALLOW_CR)) {
+                                       par->getRowSt().push_back();
+                                       if (last_numbered) {
+                                               //lyxerr << "line " << par->getRowSt().size() << " not numbered\n";
+                                               par->getRowSt().back().setNumbered(false);
+                                               last_numbered = true;
+                                       }
+                                       if (last_label.size()) {
+                                               //lyxerr << "line " << par->getRowSt().size() << " labeled: "
+                                               //      << last_label << endl;
+                                               par->getRowSt().back().setLabel(last_label);
+                                               last_label.erase();
+                                       }
                                        data.insert('K', LM_TC_CR);
                                } else 
                                        mathPrintError("Unexpected newline");
@@ -639,16 +650,14 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        break;
 
                case LM_TK_BOP:
-                       if (accent) {
+                       if (accent)
                                data.insertInset(doAccent(yylval.i, LM_TC_BOP), LM_TC_INSET);
-                       else
+                       else
                                data.insert(yylval.i, LM_TC_BOP);
                        break;
 
                case LM_TK_STY:
-                       if (mt) {
-                               mt->UserSetSize(yylval.l->id);
-                       }
+                       par->UserSetSize(yylval.l->id);
                        break; 
 
                case LM_TK_SPACE:
@@ -670,38 +679,44 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        // fallthru
                case LM_TK_FRAC:
                {
-                       MathFracInset * fc = new MathFracInset(fractype);
+                       MathFracInset fc(fractype);
                        MathedArray num;
-                       mathed_parse(num, FLAG_BRACE|FLAG_BRACE_LAST);
+                       mathed_parse(num, par, FLAG_BRACE|FLAG_BRACE_LAST);
                        MathedArray den;
-                       mathed_parse(den, FLAG_BRACE|FLAG_BRACE_LAST);
-                       fc->SetData(num, den);
-                       data.insertInset(fc, LM_TC_ACTIVE_INSET);
+                       mathed_parse(den, par, FLAG_BRACE|FLAG_BRACE_LAST);
+                       fc.SetData(num, den);
+                       data.insertInset(fc.Clone(), LM_TC_ACTIVE_INSET);
                        break;
                }
                
                case LM_TK_SQRT:
                {           
-                       MathParInset * rt;
-                       
                        char c;
                        yyis->get(c);
                        
                        if (c == '[') {
-                               rt = new MathRootInset(size);
-                               rt->setArgumentIdx(0);
-                               MathedArray ar;
-                               mathed_parse(ar, FLAG_BRACK_END, &rt);
-                               rt->setData(ar); // I belive that line is not needed (Lgb)
-                               rt->setArgumentIdx(1);
+                               MathRootInset rt(size);
+
+                               MathedArray ar1;
+                               mathed_parse(ar1, par, FLAG_BRACK_END);
+                               rt.setArgumentIdx(0);
+                               rt.setData(ar1); // I belive that line is not needed (Lgb)
+
+                               MathedArray ar2;
+                               mathed_parse(ar2, par, FLAG_BRACE|FLAG_BRACE_LAST);
+
+                               rt.setArgumentIdx(1);
+                               rt.setData(ar2); // I belive that this line is not needed (Lgb)
+
+                               data.insertInset(rt.Clone(), LM_TC_ACTIVE_INSET);
                        } else {
                                yyis->putback(c);
-                               rt = new MathSqrtInset(size);
+                               MathSqrtInset rt(size);
+                               MathedArray ar;
+                               mathed_parse(ar, par, FLAG_BRACE|FLAG_BRACE_LAST);
+                               rt.setData(ar); // I belive that this line is not needed (Lgb)
+                               data.insertInset(rt.Clone(), LM_TC_ACTIVE_INSET);
                        }
-                       MathedArray ar;
-                       mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST, &rt);
-                       rt->setData(ar); // I belive that this line is not needed (Lgb)
-                       data.insertInset(rt, LM_TC_ACTIVE_INSET);
                        break;
                }
                
@@ -712,7 +727,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                                lfd = (lfd == LM_TK_SYM) ? yylval.l->id: yylval.i;
 //      lyxerr << "L[" << lfd << " " << lfd << "]";
                        MathedArray ar;
-                       mathed_parse(ar, FLAG_RIGHT);
+                       mathed_parse(ar, par, FLAG_RIGHT);
                        int rgd = yylex();
 //      lyxerr << "R[" << rgd << "]";
                        if (rgd == LM_TK_SYM || rgd == LM_TK_STR || rgd == LM_TK_BOP || rgd == LM_TK_SPECIAL)
@@ -727,11 +742,10 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                case LM_TK_RIGHT:
                        if (flags & FLAG_RIGHT) { 
                                --plevel;
-                               return;
-                       } else {
-                               mathPrintError("Unmatched right delimiter");
-//         panic = true;
+                               goto clean_up;
                        }
+                       mathPrintError("Unmatched right delimiter");
+//       panic = true;
                        break;
                
                case LM_TK_FONT:
@@ -745,7 +759,7 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        MathDecorationInset * sq = new MathDecorationInset(yylval.l->id,
                                                                           size);
                        MathedArray ar;
-                       mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
+                       mathed_parse(ar, par, FLAG_BRACE|FLAG_BRACE_LAST);
                        sq->setData(ar);
                        data.insertInset(sq, LM_TC_ACTIVE_INSET);
                        break;
@@ -756,11 +770,9 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        break;
                        
                case LM_TK_NONUM:
-                       if (mt) {
-                               if (!mt->getRowSt().size())
-                                       mt->getRowSt().push_back();
-                               mt->getRowSt().back().setNumbered(false);
-                       }
+                       //lyxerr << "prepare line " << par->getRowSt().size()
+                       //      << " not numbered\n";
+                       last_numbered = false;
                        break;
                
                case LM_TK_PMOD:
@@ -780,26 +792,21 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
 
                case LM_TK_UNDEF:
                {
-                       
-                       MathMacro * p = 
-                               MathMacroTable::mathMTable.createMacro(yylval.s);
-                       if (p) {
-                               if (accent) 
-                                       data.insertInset(doAccent(p), p->getTCode());
-                               else
-                                       data.insertInset(p, p->getTCode());
-                               for (int i = 0; p->setArgumentIdx(i); ++i) {
+                       // save this value, yylval.s might get overwritten soon
+                       const string name = yylval.s;
+                       //lyxerr << "LM_TK_UNDEF: str = " << name << endl;
+                       if (MathMacroTable::hasTemplate(name)) {
+                               MathMacro * m = MathMacroTable::cloneTemplate(name);
+                               //lyxerr << "Macro: " << m->GetData() << endl;
+                               for (int i = 0; i < m->nargs(); ++i) {
                                        MathedArray ar;
-                                       mathed_parse(ar, FLAG_BRACE|FLAG_BRACE_LAST);
-                                       p->setData(ar);
+                                       mathed_parse(ar, par, FLAG_BRACE|FLAG_BRACE_LAST);
+                                       m->setData(ar, i);
                                }
+                               do_insert(data, m, m->getTCode());
                        } else {
-                               MathedInset * q = new MathFuncInset(yylval.s, LM_OT_UNDEF);
-                               if (accent) {
-                                       data.insertInset(doAccent(q), LM_TC_INSET);
-                               } else {
-                                       data.insertInset(q, LM_TC_INSET);
-                               }
+                               MathedInset * q = new MathFuncInset(name, LM_OT_UNDEF);
+                               do_insert(data, q, LM_TC_INSET);
                        }
                        break;
                }
@@ -811,14 +818,19 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                        if (lyxerr.debugging(Debug::MATHED))
                                lyxerr << "[" << yylval.i << "]" << endl;
                        --plevel;
-                       if (mt) { // && (flags & FLAG_END)) {
-                               mt->setData(array);
-                               array.clear();
-                       }
-                       return;
+
+                       //if (mt) { // && (flags & FLAG_END)) {
+                       //      par.setData(array);
+                       //      array.clear();
+                       //}
+#ifdef WITH_WARNINGS
+#warning Look here
+#endif
+                       goto clean_up;
 
                case LM_TK_BEGIN:
                        if (yylval.i == LM_OT_MATRIX) {
+                               //lyxerr << "###### Reading LM_OT_MATRIX \n";
                                char ar[120];
                                char ar2[8];
                                ar[0] = ar2[0] = '\0'; 
@@ -829,30 +841,34 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                                }
                                strcpy(ar, yytext.data());
                                int const nc = parse_align(ar, ar2);
+
                                MathParInset * mm = new MathMatrixInset(nc, 0);
                                mm->SetAlign(ar2[0], ar);
-                               data.insertInset(mm, LM_TC_ACTIVE_INSET);
                                MathedArray dat;
-                               mathed_parse(dat, FLAG_END, &mm);
+                               mathed_parse(dat, mm, FLAG_END);
+                               data.insertInset(mm, LM_TC_ACTIVE_INSET);
+                               mm->setData(dat);
+
                        } else if (is_eqn_type(yylval.i)) {
+                               //lyxerr << "###### Reading is_eqn_type \n";
                                if (plevel!= 0) {
                                        mathPrintError("Misplaced environment");
                                        break;
                                }
-                               if (!mt) {
-                                       mathPrintError("0 paragraph.");
-                                       panic = true;
-                               }
                                
                                mathed_env = static_cast<MathedInsetTypes>(yylval.i);
                                if (mathed_env != LM_OT_MIN) {
+                                       //lyxerr << "###### Reading mathed_env != LM_OT_MIN \n";
                                        size = LM_ST_DISPLAY;
                                        if (is_multiline(mathed_env)) {
+                                               //lyxerr << "###### Reading is_multiline(mathed_env) \n";
                                                int cols = 1;
                                                if (is_multicolumn(mathed_env)) {
+              //lyxerr << "###### Reading is_multicolumn(mathed_env) \n";
                                                        if (mathed_env != LM_OT_ALIGNAT &&
                                                            mathed_env != LM_OT_ALIGNATN &&
                                                            yyis->good()) {
+                 //lyxerr << "###### Reading is !align\n";
                                                                char c;
                                                                yyis->get(c);
                                                                if (c != '%')
@@ -862,46 +878,35 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                                                        LexGetArg('{');
                                                        cols = strToInt(string(yytext.data()));
                                                }
-                                               mt = create_multiline(mathed_env, cols);
-                                               if (mtx) *mtx = mt;
+#ifdef WITH_WARNINGS
+#warning look here
+#endif
+                                               //mt = create_multiline(mathed_env, cols);
+                                               //if (mtx) *mtx = mt;
+
+                                               //MathMatrixInset mat = create_multiline(mathed_env, cols);
+                                               //data.insertInset(mat.Clone(), LM_TC_ACTIVE_INSET);
+
+                                               par = new MathMatrixInset(create_multiline(mathed_env, cols));
                                                flags |= FLAG_END;
-//                  data.Insert(' ', LM_TC_TAB);
-//                  data.Insert(' ', LM_TC_TAB);
-//                  data.Reset();
                                        }
-                                       mt->SetStyle(size);
-                                       mt->SetType(mathed_env);
+                                       par->SetStyle(size);
+                                       par->SetType(mathed_env);
                                }
                                
                                lyxerr[Debug::MATHED] << "MATH BEGIN[" << mathed_env << "]" << endl;
                        } else {
-//          lyxerr << "MATHCRO[" << yytext << "]";
-                               MathMacro * p = 
-                                       MathMacroTable::mathMTable.createMacro(yytext.data());
-                               if (p) {
-                                       data.insertInset(p, p->getTCode());
-                                       p->setArgumentIdx(0);
-                                       //mathed_parse(p->GetData(), FLAG_END, reinterpret_cast<MathParInset**>(&p));
-                                       MathedArray dat;
-                                       mathed_parse(dat, FLAG_END, reinterpret_cast<MathParInset**>(&p));
-//              for (int i = 0; p->setArgumentIdx(i); ++i)
-//                p->SetData(mathed_parse(FLAG_BRACE|FLAG_BRACE_LAST));
-                               } else 
-                                       mathPrintError("Unrecognized environment");
+                               MathMacro * m = MathMacroTable::cloneTemplate(yytext.data());
+                               data.insertInset(m, m->getTCode());
+                               MathedArray dat;
+                               mathed_parse(dat, par, FLAG_END);
                        }
                        break;
                
                case LM_TK_MACRO:
                { 
-                       MathedInset * p = 
-                               MathMacroTable::mathMTable.createMacro(yylval.l->name);
-                       
-                       if (p) {
-                               if (accent) {
-                                       data.insertInset(doAccent(p), LM_TC_INSET);
-                               } else
-                                       data.insertInset(p, static_cast<MathMacro*>(p)->getTCode());
-                       }
+                       MathMacro * m = MathMacroTable::cloneTemplate(yylval.l->name);
+                       do_insert(data, m, m->getTCode());
                        break;
                }
                
@@ -915,14 +920,9 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                                panic = true;
                                break;
                        } 
-                       if (mt) {
-                               if (!mt->getRowSt().size())
-                                       mt->getRowSt().push_back();
-                               mt->getRowSt().back().setLabel(yytext.data());
-                       } else {
-                               mathed_label = yytext.data();
-                       }
-                       lyxerr[Debug::MATHED] << "Label[" << mathed_label << "]" << endl;
+                       last_label = yytext.data();
+                       //lyxerr << "prepare line " << par->getRowSt().size()
+                       //      << " label: " << last_label << endl;
                        break;
                }
                
@@ -945,11 +945,26 @@ void mathed_parse(MathedArray & array, unsigned flags = 0,
                
                if ((flags & FLAG_BRACE_OPT)/* && t!= '^' && t!= '_'*/) {
                        flags &= ~FLAG_BRACE_OPT;
-                       //data.Insert (LM_TC_CLOSE);
                        break;
                }
        }
        --plevel;
+
+clean_up:
+
+       if (last_numbered == false) {
+               //lyxerr << "last line " << par->getRowSt().size() << " not numbered\n";
+               if (par->getRowSt().size() == 0)
+                       par->getRowSt().push_back();
+               par->getRowSt().back().setNumbered(false);
+       }
+       if (last_label.size()) {
+               //lyxerr << "last line " << par->getRowSt().size() << " labeled: "
+               //      << last_label << endl;
+               if (par->getRowSt().size() == 0)
+                       par->getRowSt().push_back();
+               par->getRowSt().back().setLabel(last_label);
+       }
 }
 
 
@@ -957,8 +972,6 @@ void mathed_parser_file(istream & is, int lineno)
 {
        yyis = &is;
        yylineno = lineno;
-       if (!MathMacroTable::built)
-               MathMacroTable::mathMTable.builtinMacros();
 }
 
 
index 2a8d0439a98209874b56b2c6e10f2fc33bedaa80..db8077f371dc0b9ba69fecb35e67fa3314398fa5 100644 (file)
@@ -12,61 +12,73 @@ MathedRowStruct::MathedRowStruct()
        : asc_(0), desc_(0), y_(0), numbered_(true)
 {}
 
+
 string const & MathedRowStruct::getLabel() const
 {
        return label_;
 }
 
+
 bool MathedRowStruct::isNumbered() const
 {
        return numbered_;
 }
 
+
 int MathedRowStruct::getBaseline() const
 {
        return y_;
 }
 
+
 void MathedRowStruct::setBaseline(int b)
 {
        y_ = b;
 }
 
+
 int MathedRowStruct::ascent() const
 {
        return asc_;
 }
 
+
 int MathedRowStruct::descent() const
 {
        return desc_;
 }
 
+
 void MathedRowStruct::ascent(int a)
 {
        asc_ = a;
 }
 
+
 void MathedRowStruct::descent(int d)
 {
        desc_ = d;
 }
 
+
 int MathedRowStruct::getTab(unsigned int i) const
 {
        return i < widths_.size() ? widths_[i] : 0;
 }
 
+
 void MathedRowStruct::setLabel(string const & l)
 {
        label_ = l;
 }
 
+
 void MathedRowStruct::setNumbered(bool nf)
 {
        numbered_ = nf;
 }
 
+
 void MathedRowStruct::setTab(unsigned int i, int t)
 {
        if (i >= widths_.size())
@@ -86,6 +98,7 @@ MathedRowContainer::iterator MathedRowContainer::begin()
        return iterator(this);
 }
 
+
 MathedRowContainer::iterator MathedRowContainer::end()
 {
        iterator it(this);
@@ -93,29 +106,41 @@ MathedRowContainer::iterator MathedRowContainer::end()
        return it;
 }
 
+
 bool MathedRowContainer::empty() const
 {
        return data_.size() == 0;
 }
 
+
 void MathedRowContainer::insert(iterator const & it)
 {
        lyx::Assert(it.st_ == this);
        data_.insert(data_.begin() + it.pos_, MathedRowStruct());
 }
-               
+
+
 void MathedRowContainer::erase(iterator & it)
 {
        lyx::Assert(it.st_ == this);
        data_.erase(data_.begin() + it.pos_);
 }
 
+
 MathedRowStruct & MathedRowContainer::back()
 {
        lyx::Assert(data_.size());
        return data_.back();
 }
 
+
+MathedRowStruct const & MathedRowContainer::back() const
+{
+       lyx::Assert(data_.size());
+       return data_.back();
+}
+
+
 void MathedRowContainer::push_back()
 {
        data_.push_back(MathedRowStruct());
@@ -137,39 +162,46 @@ MathedRowContainer::iterator::iterator()
        : st_(0), pos_(0)
 {}
 
+
 MathedRowContainer::iterator::iterator(MathedRowContainer * m)
        : st_(m), pos_(0)
 {}
 
+
 MathedRowContainer::iterator::operator void *() const
 {
        return (void *)(st_ && pos_ < st_->size());
 }
 
+
 MathedRowStruct * MathedRowContainer::iterator::operator->()
 {
        lyx::Assert(st_);
        return &st_->data_[pos_];
 }
 
+
 MathedRowStruct const * MathedRowContainer::iterator::operator->() const
 {
        lyx::Assert(st_);
        return &st_->data_[pos_];
 }
 
+
 void MathedRowContainer::iterator::operator++()
 {
        lyx::Assert(st_);
        ++pos_;
 }
 
+
 bool MathedRowContainer::iterator::is_last() const
 {
        lyx::Assert(st_);
        return pos_ == st_->size() - 1;
 }
 
+
 bool MathedRowContainer::iterator::operator==(const iterator & it) const
 {
        return st_ == it.st_ && pos_ == it.pos_;
index 38805faf89187dd899b69c0f29a2350ba97621e1..5134dfd480c2c259047c6af714255fd35687d393 100644 (file)
@@ -105,6 +105,8 @@ public:
        void erase(iterator & it);
        /// access to last row
        MathedRowStruct & back();
+       /// access to last row
+       MathedRowStruct const & back() const;
        /// append empty element
        void push_back();
        ///
index 31094541270a5c541d6fe68047f577b89deee62f..7bc70f46a819fdf86d17555e266809012f7ae265 100644 (file)
@@ -24,8 +24,7 @@ MathedInset * MathSqrtInset::Clone()
 }
 
 
-void
-MathSqrtInset::draw(Painter & pain, int x, int y)
+void MathSqrtInset::draw(Painter & pain, int x, int y)
 { 
        MathParInset::draw(pain, x + hmax_ + 2, y); 
        int const h = ascent;
@@ -50,16 +49,16 @@ void MathSqrtInset::Write(ostream & os, bool fragile)
 }
 
 
-void
-MathSqrtInset::Metrics()
+void MathSqrtInset::Metrics()
 {
        MathParInset::Metrics();
-       ascent += 4;
+       ascent  += 4;
        descent += 2;
        int a;
        int b;
        hmax_ = mathed_char_height(LM_TC_VAR, size(), 'I', a, b);
-       if (hmax_ < 10) hmax_ = 10;
+       if (hmax_ < 10)
+               hmax_ = 10;
        wbody_ = width + 4;
        width += hmax_ + 4;
 }
index b21cd969bb10818d44e2ae7e5a4a958daa03f0c6..e349c4c5e28de388ac55467c10b2842b63b78065 100644 (file)
@@ -174,9 +174,13 @@ void MathedXIter::Merge(MathedArray const & a)
 
 void MathedXIter::SetData(MathParInset * pp)
 {
+       //if (p_ && pp != p_) {
+       //      lyxerr << "MathedXIter::SetData: " << p_ << " " << pp << endl;
+       //}
        p_ = pp;
        x_ = y_ = 0;
        array = &p_->GetData();
+       //lyxerr << "MathedXIter::SetData: " << p_ << " " << *array << endl;
        ncols = p_->GetColumns();
        crow_ = container().begin();
        if (p_->Permit(LMPF_ALLOW_CR))
@@ -300,10 +304,10 @@ bool MathedXIter::Prev()
        do {
                ipush();
                Next();
-       } while (pos<pos2);
+       } while (pos < pos2);
        ipop();
        
-       return (!IsCR());
+       return !IsCR();
 }
 
 
@@ -339,8 +343,8 @@ bool MathedXIter::Up()
 
 bool MathedXIter::Down()
 {
-       int xp = x_;
-       int colp= col;
+       int xp   = x_;
+       int colp = col;
        // int rowp = row
        
        bool res = (IsCR()) ? true : goNextCode(LM_TC_CR);
@@ -397,11 +401,11 @@ void MathedXIter::delRow()
        ipush();
 //    while (Next()) {
        do {
-               if (IsCR()) {
+               if (IsCR()) 
                        break;
-               } else if (!IsTab()) {
+
+               if (!IsTab()) 
                        line_empty = false;
-               }
        } while (Next());
 
        int const p1 = getPos();
@@ -463,9 +467,9 @@ void MathedXIter::fitCoord(int /*xx*/, int yy)
 
 void MathedXIter::setTab(int tx, int tab)
 {
-       if (crow_ && tab <= ncols) {
+       if (crow_ && tab <= ncols)
                crow_->setTab(tab, tx);
-       else
+       else
                lyxerr << "MathErr: No tabs allowed here" << endl;
 }
 
@@ -492,8 +496,10 @@ void MathedXIter::IMetrics(int pos2, int & width, int & ascent, int & descent)
        bool limit = false;
        
        descent = ascent = width = 0;
-       if (!array) return;
-       if (array->empty()) return;
+       if (!array)
+               return;
+       if (array->empty())
+               return;
 //    if  (pos2 > array->last) return;
        x1 = x_; 
        while (pos < pos2) {
index 6012083339a88699e260b0f1da3c4eacbfc37a30..7454a33d1c3ea17c54257bb8ccd90042c478373f 100644 (file)
@@ -104,7 +104,7 @@ protected:
        int sx_;
        ///
        int sw_;
-       /// true= center, false= left align (default)
+       /// true == center, false == left align (default)
        bool limits_;
        /// 
        MathedRowContainer::iterator crow_;
index 79eda2f11929e17b32d4a6c48dff780050e635db..482fa2d0a6cb3f900db3c977a3a27b1c5aca4898 100644 (file)
@@ -36,7 +36,7 @@ extern math_deco_struct const * search_deco(int code);
 
 /// math_parser.C
 extern
-void mathed_parse(MathedArray & data, unsigned flags, MathParInset ** mt);
+void mathed_parse(MathedArray & data, MathParInset * & par, unsigned flags);
 
 /// math_parser.C
 extern