]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formula.C
mathed34.diff
[lyx.git] / src / mathed / formula.C
index 6ea9cb6e28a5220563ad1a4cba5713fb8f29124d..a4185a9cdcc6275b026264a8c3993c6139a967c2 100644 (file)
@@ -59,6 +59,7 @@ extern string mathed_label;
 extern char const * latex_special_chars;
 
 int greek_kb_flag = 0;
+extern char const * latex_mathenv[];
 
 // this is only used by Whichfont and mathed_init_fonts (Lgb)
 LyXFont * Math_Fonts = 0;
@@ -70,15 +71,22 @@ static int sel_x;
 static int sel_y;
 static bool sel_flag;
 
-MathedCursor * InsetFormula::mathcursor = 0;
+// quite a hack i know. Should be done with return values...
+int number_of_newlines = 0;
 
-void mathed_init_fonts();
+static
+int mathed_write(MathParInset *, std::ostream &, bool fragile,
+                string const & label = string());
 
+void mathed_init_fonts();
 
 static
 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
 
 
+MathedCursor * InsetFormula::mathcursor = 0;
+
+
 LyXFont WhichFont(short type, int size)
 {
        LyXFont f;
@@ -219,7 +227,7 @@ InsetFormula::InsetFormula(MathParInset * p)
 
        par = is_multiline(p->GetType()) ?
                new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
-               new MathParInset(p);
+               new MathParInset(*p);
        //   mathcursor = 0;
 
        disp_flag = (par->GetType()>0);
@@ -250,11 +258,9 @@ void InsetFormula::Write(Buffer const * buf, ostream & os) const
 
 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
 {
-       int ret = 0;
        //#warning Alejandro, the number of lines is not returned in this case
        // This problem will disapear at 0.13.
-       mathed_write(par, os, &ret, fragile, label);
-       return ret;
+       return mathed_write(par, os, fragile, label);
 }
 
 
@@ -299,7 +305,8 @@ void InsetFormula::Read(Buffer const *, LyXLex & lex)
        // Silly hack to read labels.
        mathed_label.erase();
 
-       mathed_parse(0, 0, &par);
+       MathedArray ar;
+       mathed_parse(ar, 0, &par);
        par->Metrics();
        disp_flag = (par->GetType() > 0);
 
@@ -531,9 +538,6 @@ void InsetFormula::display(bool dspf)
                        par->SetStyle(LM_ST_DISPLAY);
                } else {
                        if (is_multiline(par->GetType())) {
-                               MathParInset * p = new MathParInset(par);
-                               delete par;
-                               par = p;
                                if (mathcursor)
                                        mathcursor->SetPar(par);
                        }
@@ -905,7 +909,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
 
        case LFUN_MATH_SIZE:
                if (!arg.empty()) {
-                       latexkeys * l = in_word_set(arg);
+                       latexkeys const * l = in_word_set(arg);
                        int sz = (l) ? l->id: -1;
                        mathcursor->SetSize(sz);
                        UpdateLocal(bv);
@@ -939,7 +943,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
                if (mathcursor && p) {
                        if (k > 2 && int(strlen(s)) > m)
                                p->SetAlign(s[0], &s[1]);
-                       mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
+                       mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
                        UpdateLocal(bv);
                }
                break;
@@ -950,7 +954,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
                bv->lockedInsetStoreUndo(Undo::INSERT);
                char lf[40], rg[40], arg2[40];
                int ilf = '(', irg = '.';
-               latexkeys * l;
+               latexkeys const * l;
                string vdelim("(){}[]./|");
 
                if (arg.empty())
@@ -987,7 +991,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
                }
 
                MathDelimInset * p = new MathDelimInset(ilf, irg);
-               mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
+               mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
                UpdateLocal(bv);
                break;
        }
@@ -995,7 +999,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
        case LFUN_PROTECTEDSPACE:
                bv->lockedInsetStoreUndo(Undo::INSERT);
                sp = new MathSpaceInset(1);
-               mathcursor->Insert(sp);
+               mathcursor->insertInset(sp, LM_TC_INSET);
                space_on = true;
                UpdateLocal(bv);
                break;
@@ -1221,7 +1225,7 @@ InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
 static
 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
 {
-       MathedIter it(par->GetData());
+       MathedIter it(&par->GetData());
 
        while (it.OK() && !(features.binom && features.boldsymbol)) {
                if (it.IsInset()) {
@@ -1246,3 +1250,57 @@ void mathedValidate(LaTeXFeatures & features, MathParInset * par)
                it.Next();
        }
 }
+
+
+static
+int mathed_write(MathParInset * p, ostream & os,
+                bool fragile, string const & label)
+{
+       number_of_newlines = 0;
+       short mathed_env = p->GetType();
+       
+       if (mathed_env == LM_OT_MIN) {
+               if (fragile) os << "\\protect";
+               os << "\\( "; // changed from " \\( " (Albrecht Dress)
+       } else {
+               if (mathed_env == LM_OT_PAR){
+                       os << "\\[\n";
+               } else {
+                       os << "\\begin{"
+                          << latex_mathenv[mathed_env]
+                          << "}";
+                       if (is_multicolumn(mathed_env)) {
+                               if (mathed_env != LM_OT_ALIGNAT
+                                   && mathed_env != LM_OT_ALIGNATN)
+                                       os << "%";
+                               os << "{" << p->GetColumns()/2 << "}";
+                       }
+                       os << "\n";
+               }
+               ++number_of_newlines;
+       }
+       
+       if (!label.empty() && label[0] > ' '
+           && is_singlely_numbered(mathed_env)) {
+               os << "\\label{"
+                  << label
+                  << "}\n";
+               ++number_of_newlines;
+       }
+       
+       p->Write(os, fragile);
+       
+       if (mathed_env == LM_OT_MIN){
+               if (fragile) os << "\\protect";
+               os << " \\)";
+       } else if (mathed_env == LM_OT_PAR) {
+               os << "\\]\n";
+               ++number_of_newlines;
+       } else {
+               os << "\n\\end{"
+                  << latex_mathenv[mathed_env]
+                  << "}\n";
+               number_of_newlines += 2;
+       }
+       return number_of_newlines;
+}