]> git.lyx.org Git - lyx.git/commitdiff
some work on math-extern
authorAndré Pönitz <poenitz@gmx.net>
Fri, 9 Nov 2001 14:23:44 +0000 (14:23 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Fri, 9 Nov 2001 14:23:44 +0000 (14:23 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2997 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/Makefile.am
src/mathed/math_exintinset.C [deleted file]
src/mathed/math_exintinset.h [deleted file]
src/mathed/math_extern.C
src/mathed/math_inset.h
src/mathed/math_limitopinset.C [new file with mode: 0644]
src/mathed/math_limitopinset.h [new file with mode: 0644]
src/mathed/math_macroarg.h
src/mathed/math_symbolinset.C
src/mathed/math_symbolinset.h

index 0ec4638b102d188917d390d298e5995c9719d063..692c1a900d18fa19ed974539969eeaa88294a365 100644 (file)
@@ -68,6 +68,8 @@ libmathed_la_SOURCES = \
        math_kerninset.h \
        math_lefteqninset.C \
        math_lefteqninset.h \
+       math_limitopinset.C \
+       math_limitopinset.h \
        math_macro.C \
        math_macro.h \
        math_macroarg.C \
diff --git a/src/mathed/math_exintinset.C b/src/mathed/math_exintinset.C
deleted file mode 100644 (file)
index 2ad11b3..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-#include "math_exintinset.h"
-#include "math_support.h"
-#include "debug.h"
-#include "math_mathmlstream.h"
-#include "math_symbolinset.h"
-
-
-MathExIntInset::MathExIntInset(MathScriptInset const & scripts,
-               MathArray const & core, MathArray const & diff)
-       : int_(new MathSymbolInset("int")),
-               scripts_(scripts), core_(core), diff_(diff)
-{}
-
-
-MathInset * MathExIntInset::clone() const
-{
-       return new MathExIntInset(*this);
-}
-
-
-void MathExIntInset::write(WriteStream & os) const
-{
-       scripts_.write(int_.nucleus(), os);
-       os << core_ << "d" << diff_;
-}
-
-
-void MathExIntInset::normalize(NormalStream & os) const
-{
-       //os << "[int " << scripts_ << ' ' << core_ << ' ' << diff_ << ']'
-}
-
-
-void MathExIntInset::metrics(MathMetricsInfo const &) const
-{
-       lyxerr << "should not happen\n";
-}
-
-
-void MathExIntInset::draw(Painter &, int, int) const
-{  
-       lyxerr << "should not happen\n";
-}
-
-
-void MathExIntInset::maplize(MapleStream & os) const
-{
-       //os << name_.c_str() << '(' << cell(0) << ')';
-}
-
-void MathExIntInset::mathmlize(MathMLStream & os) const
-{
-       //os << name_.c_str() << '(' << cell(0) << ')';
-}
diff --git a/src/mathed/math_exintinset.h b/src/mathed/math_exintinset.h
deleted file mode 100644 (file)
index c92497a..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-// -*- C++ -*-
-#ifndef MATH_EXINTINSET_H
-#define MATH_EXINTINSET_H
-
-// /\int_l^u f(x) dxin one block (as opposed to 'f','(','x',')' or 'f','x')
-// for interfacing external programs
-
-#include "math_scriptinset.h"
-
-class MathExIntInset : public MathInset {
-public:
-       ///
-       MathExIntInset(MathScriptInset const &, MathArray const &, MathArray const &);
-       ///
-       MathInset * clone() const;
-       ///
-       void metrics(MathMetricsInfo const & st) const;
-       ///
-       void draw(Painter &, int x, int y) const;
-       ///
-       void write(WriteStream & os) const;
-       ///
-       void normalize(NormalStream &) const;
-       ///
-       void maplize(MapleStream &) const;
-       ///
-       void mathmlize(MathMLStream &) const;
-private:
-       ///
-       MathAtom int_;
-       ///
-       MathScriptInset scripts_;
-       ///
-       MathArray core_;
-       ///
-       MathArray diff_;
-};
-#endif
index 908c6f73eda54eb4a4bfbb21ad59f1044f7b85c3..e944e6ab4e2d1596ee0e3c485a39ae461dbff1db 100644 (file)
@@ -12,6 +12,7 @@
 #include "math_mathmlstream.h"
 #include "math_scriptinset.h"
 #include "math_stringinset.h"
+#include "math_symbolinset.h"
 #include "debug.h"
 
 
@@ -43,8 +44,6 @@ string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
        MathCharInset const * p = it->nucleus()->asCharInset();
        if (p) {
                for (MathTextCodes c = p->code(); it != end; ++it) {
-                       if (!it->nucleus())
-                               break;
                        p = it->nucleus()->asCharInset();
                        if (!p || p->code() != c)
                                break;
@@ -76,36 +75,8 @@ void extractStrings(MathArray & dat)
 }
 
 
-bool needAsterisk(MathAtom const &, MathAtom const &)
-{
-       return false;
-}
-
-
-void guessAsterisks(MathArray & dat)
-{
-       if (dat.size() <= 1)
-               return;
-       MathArray ar;
-       ar.push_back(*dat.begin());
-       MathArray::const_iterator it = dat.begin();
-       MathArray::const_iterator jt = it + 1;
-       for (; jt != dat.end(); ++it, ++jt) {
-               if (needAsterisk(*it, *jt))
-                       ar.push_back(MathAtom(new MathCharInset('*')));
-               ar.push_back(*it);
-       }
-       ar.push_back(*dat.end());
-       ar.swap(dat);
-}
-
-
 MathInset * singleItem(MathArray & ar)
 {
-       lyxerr << "ar.size: " << ar.size() << "\n";
-       //lyxerr << "ar.begin: " << ar.begin() << "\n";
-       //lyxerr << "ar.nuc: " << ar.begin()->nucleus() << "\n";
-       lyxerr << "ar.nuc: " << *ar.begin()->nucleus() << "\n";
        return ar.size() == 1 ? ar.begin()->nucleus() : 0;
 }
 
@@ -114,9 +85,7 @@ void extractMatrices(MathArray & ar)
 {
        lyxerr << "\nMatrices from: " << ar << "\n";
        for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
-               if (!it->nucleus())
-                       continue;       
-               MathDelimInset * del = it->nucleus()->asDelimInset();
+               MathDelimInset * del = (*it)->asDelimInset();
                if (!del)
                        continue;
                MathInset * arr = singleItem(del->cell(0));
@@ -138,43 +107,91 @@ string extractString(MathInset * p)
 }
 
 
-// replace '('...')' sequences by a real MathDelimInset
-void extractDelims(MathArray & ar) {
+// define a function for tests
+typedef bool TestItemFunc(MathInset *);
+
+// define a function for replacing pa
+typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
+
+// search end of nested sequence
+MathArray::iterator searchNestedEnd(
+       MathArray::iterator it,
+       MathArray::iterator last,
+       TestItemFunc testOpen,
+       TestItemFunc testClose
+)
+{
+       for (int level = 0; it != last; ++it) {
+               if (testOpen(it->nucleus()))
+                       ++level;
+               if (testClose(it->nucleus()))
+                       --level;
+               if (level == 0)
+                       break;
+       }
+       return it;
+}
+
+
+// replace nested sequences by a real Insets
+void replaceNested(
+       MathArray & ar,
+       TestItemFunc testOpen,
+       TestItemFunc testClose,
+       ReplaceArgumentFunc replaceArg
+)
+{
        // use indices rather than iterators for the loop  because we are going
        // to modify the array.
-       lyxerr << "\nDelims from: " << ar << "\n";
        for (MathArray::size_type i = 0; i < ar.size(); ++i) {
+               // check whether this is the begin of the sequence
                MathArray::iterator it = ar.begin() + i;
-               if (extractString(it->nucleus()) != "(")
+               if (!testOpen(it->nucleus()))
                        continue;
 
-               // search matching closing paranthesis
-               int level = 1;
-               MathArray::iterator jt = it + 1;
-               for (; jt != ar.end(); ++jt) {
-                       string s = extractString(jt->nucleus());
-                       if (s == "(")
-                               ++level;
-                       if (s == ")")
-                               --level;
-                       if (level == 0)
-                               break;
-               }
+               // search end of sequence
+               MathArray::iterator jt = searchNestedEnd(it, ar.end(), testOpen, testClose);
                if (jt == ar.end())
                        continue;
 
-               // create a proper deliminset
-               MathAtom at(new MathDelimInset("(", ")"));
-               at->cell(0) = MathArray(it + 1, jt);
+               // create a proper inset as replacement
+               MathInset * p = replaceArg(MathArray(it + 1, jt));
 
                // replace the original stuff by the new inset
                ar.erase(it + 1, jt + 1);
-               *it = at;
-               lyxerr << "\nDelims to: " << ar << "\n";
+               (*it).reset(p);
        }
 } 
 
 
+bool testParanOpen(MathInset * p)
+{
+       return extractString(p) == "(";
+}
+
+
+bool testParanClose(MathInset * p)
+{
+       return extractString(p) == ")";
+}
+
+
+MathInset * replaceByDelimInset(const MathArray & ar)
+{
+       MathDelimInset * del = new MathDelimInset("(", ")");
+       del->cell(0) = ar;
+       return del;
+}
+
+
+// replace '('...')' sequences by a real MathDelimInset
+void extractDelims(MathArray & ar) {
+       lyxerr << "\nDelims from: " << ar << "\n";
+       replaceNested(ar, testParanOpen, testParanClose, replaceByDelimInset);
+       lyxerr << "\nDelims to: " << ar << "\n";
+}
+
+
 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
 // assume 'extractDelims' ran before
 void extractFunctions(MathArray & ar)
@@ -188,8 +205,6 @@ void extractFunctions(MathArray & ar)
                MathArray::iterator it = ar.begin() + i;
 
                // is this a function name?
-               if (!it->nucleus())
-                       continue;       
                MathFuncInset * func = (*it)->asFuncInset();
                if (!func)
                        continue;
@@ -226,12 +241,56 @@ void extractFunctions(MathArray & ar)
 } 
 
 
+bool testIntSymbol(MathInset * p)
+{
+       return p->asSymbolInset() && p->asSymbolInset()->name() == "int";
+}
+
+
+bool testSmallD(MathInset * p)
+{
+       string s = extractString(p);
+       return s.size() && s[0] == 'd';
+}
+
+
+// replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
+// assume 'extractDelims' ran before
+void extractIntegrals(MathArray & ar)
+{
+       // we need at least three items...
+       if (ar.size() <= 2)
+               return;
+
+       lyxerr << "\nIntegrals from: " << ar << "\n";
+       for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) {
+               MathArray::iterator it = ar.begin() + i;
+
+               // is this a integral name?
+               if (!testIntSymbol(it->nucleus()))
+                       continue;
+
+               // search 'd'
+               MathArray::iterator jt =
+                       searchNestedEnd(it, ar.end(), testIntSymbol, testSmallD);
+
+               // create a proper inset as replacement
+               //MathInset * p = replaceArg(MathArray(it + 1, jt));
+
+               // replace the original stuff by the new inset
+               //ar.erase(it + 1, jt + 1);
+               //(*it).reset(p);
+       }
+}
+
+
 void extractStructure(MathArray & ar)
 {
        extractStrings(ar);
        extractMatrices(ar);
        extractDelims(ar);
        extractFunctions(ar);
+       extractIntegrals(ar);
 }
 
 
index 6c46d2a655b82a439553905957963213f0d27882..819d3cc21fe0c4172939cab051d189e02db875f1 100644 (file)
@@ -50,7 +50,7 @@ class MathNestInset;
 class MathScriptInset;
 class MathStringInset;
 class MathSpaceInset;
-class MathMacroTemplate;
+class MathSymbolInset;
 
 class NormalStream;
 class OctaveStream;
@@ -62,6 +62,7 @@ class MathArray;
 class LaTeXFeatures;
 class BufferView;
 class UpdatableInset;
+class MathMacroTemplate;
 
 
 class MathInset {
@@ -188,6 +189,7 @@ public:
        virtual MathScriptInset const * asScriptInset() const { return 0; }
        virtual MathSpaceInset        * asSpaceInset()        { return 0; }
        virtual MathStringInset       * asStringInset()       { return 0; }
+       virtual MathSymbolInset       * asSymbolInset()       { return 0; }
        virtual UpdatableInset   * asHyperActiveInset() const { return 0; }
 
        /// identifies things that can get scripts
diff --git a/src/mathed/math_limitopinset.C b/src/mathed/math_limitopinset.C
new file mode 100644 (file)
index 0000000..5d4d694
--- /dev/null
@@ -0,0 +1,54 @@
+#include "math_limitopinset.h"
+#include "math_support.h"
+#include "math_mathmlstream.h"
+#include "math_symbolinset.h"
+#include "debug.h"
+
+
+MathLimitOpInset::MathLimitOpInset(MathScriptInset const & scripts,
+               MathArray const & core)
+       : int_(new MathSymbolInset("int")),
+               scripts_(scripts), core_(core)
+{}
+
+
+MathInset * MathLimitOpInset::clone() const
+{
+       return new MathLimitOpInset(*this);
+}
+
+
+void MathLimitOpInset::write(WriteStream & os) const
+{
+       scripts_.write(int_.nucleus(), os);
+       os << core_ << "d" << diff_;
+}
+
+
+void MathLimitOpInset::normalize(NormalStream & os) const
+{
+       //os << "[int " << scripts_ << ' ' << core_ << ' ' << diff_ << ']'
+}
+
+
+void MathLimitOpInset::metrics(MathMetricsInfo const &) const
+{
+       lyxerr << "should not happen\n";
+}
+
+
+void MathLimitOpInset::draw(Painter &, int, int) const
+{  
+       lyxerr << "should not happen\n";
+}
+
+
+void MathLimitOpInset::maplize(MapleStream & os) const
+{
+       //os << name_.c_str() << '(' << cell(0) << ')';
+}
+
+void MathLimitOpInset::mathmlize(MathMLStream & os) const
+{
+       //os << name_.c_str() << '(' << cell(0) << ')';
+}
diff --git a/src/mathed/math_limitopinset.h b/src/mathed/math_limitopinset.h
new file mode 100644 (file)
index 0000000..f5343c2
--- /dev/null
@@ -0,0 +1,38 @@
+// -*- C++ -*-
+#ifndef MATH_LIMITOPINSET_H
+#define MATH_LIMITOPINSET_H
+
+// /\sum_l^u f(x) in one block
+// for interfacing external programs
+
+#include "math_scriptinset.h"
+
+class MathLimitOpInset : public MathInset {
+public:
+       ///
+       MathLimitOpInset(MathScriptInset const &, MathArray const &);
+       ///
+       MathInset * clone() const;
+       ///
+       void metrics(MathMetricsInfo const & st) const;
+       ///
+       void draw(Painter &, int x, int y) const;
+       ///
+       void write(WriteStream & os) const;
+       ///
+       void normalize(NormalStream &) const;
+       ///
+       void maplize(MapleStream &) const;
+       ///
+       void mathmlize(MathMLStream &) const;
+private:
+       ///
+       MathAtom int_;
+       ///
+       MathScriptInset scripts_;
+       ///
+       MathArray core_;
+       ///
+       MathArray diff_;
+};
+#endif
index d0b4591966a454be65e4591dbbc33a70766b0687..b200bcc5045fbf18d510c13b49fc3f7b9d46799e 100644 (file)
@@ -18,17 +18,18 @@ public:
        ///
        MathInset * clone() const;
        ///
+       bool isActive() const { return false; }
+       ///
        void metrics(MathMetricsInfo const & st) const;
        ///
        void draw(Painter &, int x, int y) const;
        ///
-       void write(WriteStream & os) const;
+       void substitute(MathMacro const & macro);
+
        ///
        void normalize(NormalStream &) const;
        ///
-       void substitute(MathMacro const & macro);
-       ///
-       bool isActive() const { return false; }
+       void write(WriteStream & os) const;
 
 private:
        /// A number between 1 and 9
index e7f000f64c960b30f938ebefa958935f48739d7f..38ad5673ede1b2c91abc834e5bfeb860644898a2 100644 (file)
@@ -1,8 +1,8 @@
 #include "math_symbolinset.h"
 #include "math_parser.h"
 #include "math_mathmlstream.h"
-#include "debug.h"
 #include "math_support.h"
+#include "debug.h"
 
 
 MathSymbolInset::MathSymbolInset(const latexkeys * l)
@@ -16,18 +16,6 @@ MathInset * MathSymbolInset::clone() const
 }
 
 
-void MathSymbolInset::write(WriteStream & os) const
-{
-       os << '\\' << sym_->name.c_str() << ' ';
-}
-
-
-void MathSymbolInset::normalize(NormalStream & os) const
-{
-       os << "[symbol " << sym_->name.c_str() << "]";
-}
-
-
 MathTextCodes MathSymbolInset::code() const
 {
        switch(sym_->token) {
@@ -58,6 +46,12 @@ MathTextCodes MathSymbolInset::code2() const
 }
 
 
+string MathSymbolInset::name() const
+{
+       return sym_->name;
+}
+
+
 void MathSymbolInset::metrics(MathMetricsInfo const & mi) const
 {
        mi_ = mi;
@@ -76,7 +70,7 @@ void MathSymbolInset::metrics(MathMetricsInfo const & mi) const
                        mathed_string_dim(LM_TC_TEX, mi_, sym_->name, ascent_, descent_, width_);
        }
        if (isRelOp())
-               width_ +=  mathed_char_width(LM_TC_TEX, mi_, 'I');
+               width_ += mathed_char_width(LM_TC_TEX, mi_, 'I');
 }
 
 
@@ -112,27 +106,39 @@ bool MathSymbolInset::takesLimits() const
 }
 
 
+void MathSymbolInset::normalize(NormalStream & os) const
+{
+       os << "[symbol " << name().c_str() << "]";
+}
+
+
 void MathSymbolInset::maplize(MapleStream & os) const
 {
-       if (sym_->name == "cdot")
+       if (name() == "cdot")
                os << '*';
        else
-               os << sym_->name.c_str();
+               os << name().c_str();
 }
 
 
 void MathSymbolInset::mathmlize(MathMLStream & os) const
 {
-       os << sym_->name.c_str();
+       os << name().c_str();
 }
 
 
 void MathSymbolInset::octavize(OctaveStream & os) const
 {
-       if (sym_->name == "cdot")
+       if (name() == "cdot")
                os << '*';
        else
-               os << sym_->name.c_str();
+               os << name().c_str();
+}
+
+
+void MathSymbolInset::write(WriteStream & os) const
+{
+       os << '\\' << name().c_str() << ' ';
 }
 
 
index 5c1a6a348db8343a914a3856cae1ee0f83dc6d66..4f7db6d9557e1efca55ba1cdf8fc53d6f0c721af 100644 (file)
@@ -18,10 +18,6 @@ public:
        ///
        MathInset * clone() const;
        ///
-       void write(WriteStream & os) const;
-       ///
-       void normalize(NormalStream &) const;
-       ///
        void metrics(MathMetricsInfo const & st) const;
        ///
        void draw(Painter &, int x, int y) const;
@@ -31,13 +27,21 @@ public:
        bool isScriptable() const;
        /// identifies things that can get \limits or \nolimits
        bool takesLimits() const;
+       ///
+       MathSymbolInset * asSymbolInset() { return this; }
 
+       ///
+       void normalize(NormalStream &) const;
        ///
        void maplize(MapleStream &) const;
        ///
        void mathmlize(MathMLStream &) const;
        ///
        void octavize(OctaveStream &) const;
+       ///
+       void write(WriteStream & os) const;
+       ///
+       string name() const;
 private:
        ///
        MathTextCodes code() const;