]> git.lyx.org Git - lyx.git/commitdiff
basic support for \xymatrix
authorAndré Pönitz <poenitz@gmx.net>
Mon, 4 Feb 2002 18:44:51 +0000 (18:44 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Mon, 4 Feb 2002 18:44:51 +0000 (18:44 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3482 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/ChangeLog
src/mathed/Makefile.am
src/mathed/math_factory.C
src/mathed/math_parser.C
src/mathed/math_xymatrix.C [new file with mode: 0644]
src/mathed/math_xymatrix.h [new file with mode: 0644]

index 487cecde8927e16762160015866ca7a2c4567743..2e8b7ad51d06100b28228ffd434751ba33511d86 100644 (file)
@@ -1,3 +1,8 @@
+
+2002-02-01  André Pönitz <poenitz@gmx.net>
+
+       * math_xymatrixinset.[Ch]: some support for \xymatrix
+
 2002-02-01  André Pönitz <poenitz@gmx.net>
 
        * math_undersetinset.[Ch]: implement direct support for \underset
index b932fc218250af0c9f6761c13fe6691fbc57932f..5a5a05fe9715fee9d129118ff3e2370c694e839f 100644 (file)
@@ -128,4 +128,6 @@ libmathed_la_SOURCES = \
        math_undersetinset.C \
        math_undersetinset.h \
        math_xdata.C \
-       math_xdata.h
+       math_xdata.h \
+       math_xymatrix.C \
+       math_xymatrix.h
index be16ec7499894e54a1c26daaf32d9a06ff43e9e4..c7a72f873d80eda87847ff0094a4062889eb7361 100644 (file)
@@ -23,6 +23,7 @@
 #include "math_symbolinset.h"
 #include "math_undersetinset.h"
 #include "math_unknowninset.h"
+#include "math_xymatrix.h"
 
 
 MathAtom createMathInset(latexkeys const * l)
@@ -90,6 +91,9 @@ MathAtom createMathInset(string const & s)
                        && s[2] >= '1' && s[2] <= '9')
                return MathAtom(new MathMacroArgument(s[2] - '0'));
 
+       if (s == "xymatrix")
+               return MathAtom(new MathXYMatrixInset);
+
        latexkeys const * l = in_word_set(s);
        if (l)
                return createMathInset(l);
index 54711e868194cbc1a772188d9e0e4baf76e8b5f3..4a94e204fad25cd3bf5fe22c5975e1534f766560 100644 (file)
@@ -139,8 +139,9 @@ enum {
        FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
        FLAG_BRACK_END  = 1 << 4,  //  next closing bracket ends the parsing process
        FLAG_BOX        = 1 << 5,  //  we are in a box
-       FLAG_ITEM       = 1 << 7,  //  read a (possibly braced token)
-       FLAG_BLOCK      = 1 << 8,  //  next block ends the parsing process
+       FLAG_ITEM       = 1 << 6,  //  read a (possibly braced token)
+       FLAG_BLOCK      = 1 << 7,  //  next block ends the parsing process
+       FLAG_BLOCK2     = 1 << 8,  //  next block2 ends the parsing process
        FLAG_LEAVE      = 1 << 9   //  leave the loop at the end
 };
 
@@ -263,6 +264,8 @@ private:
        void error(string const & msg);
        ///
        bool parse_lines(MathAtom & t, bool numbered, bool outmost);
+       /// parses {... & ... \\ ... & ... }
+       bool parse_lines2(MathAtom & t);
 
 private:
        ///
@@ -281,6 +284,10 @@ private:
        Token const & getToken();
        /// skips spaces if any
        void skipSpaces();
+       /// skips opening brace
+       void skipBegin();
+       /// skips closing brace
+       void skipEnd();
        /// counts a sequence of hlines
        int readHLines();
        ///
@@ -359,6 +366,24 @@ void Parser::skipSpaces()
 }
 
 
+void Parser::skipBegin()
+{
+       if (nextToken().cat() == catBegin)
+               getToken();
+       else
+               lyxerr << "'{' expected\n";
+}
+
+
+void Parser::skipEnd()
+{
+       if (nextToken().cat() == catEnd)
+               getToken();
+       else
+               lyxerr << "'}' expected\n";
+}
+
+
 int Parser::readHLines()
 {
        int num = 0;
@@ -504,8 +529,6 @@ bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost)
                return false;
        }
 
-       MathInset::col_type const cols = p->ncols();
-
        // save global variables
        bool   const saved_num   = curr_num_;
        string const saved_label = curr_label_;
@@ -519,9 +542,10 @@ bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost)
                curr_label_.erase();
 
                // reading a row
-               for (MathInset::col_type col = 0; col < cols; ++col) {
+               for (MathInset::col_type col = 0; col < p->ncols(); ++col) {
                        //lyxerr << "reading cell " << row << " " << col << "\n";
-                       parse_into(p->cell(col + row * cols), FLAG_BLOCK);
+               
+                       parse_into(p->cell(col + row * p->ncols()), FLAG_BLOCK);
 
                        // break if cell is not followed by an ampersand
                        if (nextToken().cat() != catAlign) {
@@ -582,6 +606,62 @@ bool Parser::parse_lines(MathAtom & t, bool numbered, bool outmost)
 }
 
 
+bool Parser::parse_lines2(MathAtom & t)
+{      
+       MathGridInset * p = t->asGridInset();
+       if (!p) {
+               lyxerr << "error in Parser::parse_lines() 1\n";
+               return false;
+       }
+
+       skipBegin();
+
+       for (int row = 0; true; ++row) {
+               // reading a row
+               for (MathInset::col_type col = 0; true; ++col) {
+                       //lyxerr << "reading cell " << row << " " << col << " " << p->ncols() << "\n";
+               
+                       if (col >= p->ncols()) {
+                               //lyxerr << "adding col " << col << "\n";
+                               p->addCol(p->ncols());
+                       }
+
+                       parse_into(p->cell(col + row * p->ncols()), FLAG_BLOCK2);
+                       //lyxerr << "read cell: " << p->cell(col + row * p->ncols()) << "\n";
+
+                       // break if cell is not followed by an ampersand
+                       if (nextToken().cat() != catAlign) {
+                               //lyxerr << "less cells read than normal in row/col: " << row << " " << col << "\n";
+                               break;
+                       }
+                       
+                       // skip the ampersand
+                       getToken();
+               }
+
+               // is a \\ coming?
+               if (nextToken().isCR()) {
+                       // skip the cr-token
+                       getToken();
+               }
+
+               // we are finished if the next token is an '}'
+               if (nextToken().cat() == catEnd) {
+                       // skip the end-token
+                       getToken();
+                       // leave the 'read a line'-loop
+                       break;
+               }
+
+               // otherwise, we have to start a new row
+               p->appendRow();
+       }
+
+       return true;
+}
+
+
+
 bool Parser::parse_macro(string & name)
 {
        name = "{error}";
@@ -751,7 +831,7 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
        while (good()) {
                Token const & t = getToken();
        
-               //lyxerr << "t: " << t << " flags: " << flags << "'\n";
+               //lyxerr << "t: " << t << " flags: " << flags << "\n";
                //array.dump(lyxerr);
                //lyxerr << "\n";
 
@@ -775,6 +855,14 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
                        }
                }
 
+               if (flags & FLAG_BLOCK2) {
+                       if (t.cat() == catAlign || t.isCR() || t.cs() == "end"
+                                       || t.cat() == catEnd) {
+                               putback();
+                               return;
+                       }
+               }
+
                //
                // cat codes
                //
@@ -967,6 +1055,12 @@ void Parser::parse_into(MathArray & array, unsigned flags, MathTextCodes code)
                        return;
                }
 
+               else if (t.cs() == "xymatrix") {
+                       array.push_back(createMathInset(t.cs()));
+                       parse_lines2(array.back());
+                       // skip closing brace
+               }
+
                // Disabled
 #if 0
                else if (t.cs() == "mbox") {
diff --git a/src/mathed/math_xymatrix.C b/src/mathed/math_xymatrix.C
new file mode 100644 (file)
index 0000000..f71a46c
--- /dev/null
@@ -0,0 +1,53 @@
+#include <config.h>
+
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
+#include "math_xymatrix.h"
+#include "math_mathmlstream.h"
+#include "math_streamstr.h"
+
+
+MathXYMatrixInset::MathXYMatrixInset()
+       : MathGridInset(1, 1)
+{}
+
+
+MathInset * MathXYMatrixInset::clone() const
+{
+       return new MathXYMatrixInset(*this);
+}
+
+
+void MathXYMatrixInset::metrics(MathMetricsInfo const & st) const
+{
+       MathMetricsInfo mi = st;
+       if (mi.style == LM_ST_DISPLAY)
+               mi.style = LM_ST_TEXT;
+       MathGridInset::metrics(mi);
+}
+
+
+void MathXYMatrixInset::write(WriteStream & os) const
+{
+       os << "\\xymatrix{";
+       MathGridInset::write(os);
+       os << "}\n";
+}
+
+
+void MathXYMatrixInset::normalize(NormalStream & os) const
+{
+       os << "[xymatrix ";
+       MathGridInset::normalize(os);
+       os << "]";
+}
+
+
+void MathXYMatrixInset::maplize(MapleStream & os) const
+{
+       os << "xymatrix(";
+       MathGridInset::maplize(os);
+       os << ")";
+}
diff --git a/src/mathed/math_xymatrix.h b/src/mathed/math_xymatrix.h
new file mode 100644 (file)
index 0000000..bfebb04
--- /dev/null
@@ -0,0 +1,31 @@
+// -*- C++ -*-
+#ifndef MATH_XYMATRIX_H
+#define MATH_XYMATRIX_H
+
+#include "math_gridinset.h"
+
+#ifdef __GNUG__
+#pragma interface
+#endif
+
+
+class MathXYMatrixInset : public MathGridInset {
+public: 
+       ///
+       MathXYMatrixInset();
+       ///
+       MathInset * clone() const;
+       ///
+       void metrics(MathMetricsInfo const & st) const;
+       ///
+       MathXYMatrixInset * asXYMatrixInset() { return this; }
+
+       ///
+       void write(WriteStream & os) const;
+       ///
+       void normalize(NormalStream &) const;
+       ///
+       void maplize(MapleStream &) const;
+};
+
+#endif