]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_parser.C
Fix to bug 2362: Deleting superscript also deletes subscript.
[lyx.git] / src / mathed / math_parser.C
index b5746affca97f1285dce6769683d0d6a1c257f86..be12a451e55004f885095806144fecdb5eb445cc 100644 (file)
@@ -42,6 +42,7 @@ following hack as starting point to write some macros:
 #include "math_arrayinset.h"
 #include "math_braceinset.h"
 #include "math_charinset.h"
+#include "math_colorinset.h"
 #include "math_commentinset.h"
 #include "math_deliminset.h"
 #include "math_envinset.h"
@@ -50,7 +51,6 @@ following hack as starting point to write some macros:
 #include "math_macro.h"
 #include "math_macroarg.h"
 #include "math_macrotemplate.h"
-#include "math_parboxinset.h"
 #include "math_parinset.h"
 #include "math_rootinset.h"
 #include "math_scriptinset.h"
@@ -64,11 +64,10 @@ following hack as starting point to write some macros:
 #include "lyxlex.h"
 #include "debug.h"
 
+#include "support/convert.h"
+
 #include <sstream>
 
-#ifndef CXX_GLOBAL_CSTD
-using std::atoi;
-#endif
 using std::endl;
 using std::fill;
 
@@ -103,6 +102,93 @@ bool stared(string const & s)
 }
 
 
+/*!
+ * Add the row \p cellrow to \p grid.
+ * \returns wether the row could be added. Adding a row can fail for
+ * environments like "equation" that have a fixed number of rows.
+ */
+bool addRow(MathGridInset & grid, MathGridInset::row_type & cellrow,
+            string const & vskip)
+{
+       ++cellrow;
+       if (cellrow == grid.nrows()) {
+               //lyxerr << "adding row " << cellrow << endl;
+               grid.addRow(cellrow - 1);
+               if (cellrow == grid.nrows()) {
+                       // We can't add a row to this grid, so let's
+                       // append the content of this cell to the previous
+                       // one.
+                       // This does not happen in well formed .lyx files,
+                       // but LyX versions 1.3.x and older could create
+                       // such files and tex2lyx can still do that.
+                       --cellrow;
+                       lyxerr << "ignoring extra row";
+                       if (!vskip.empty())
+                               lyxerr << " with extra space " << vskip;
+                       lyxerr << '.' << endl;
+                       return false;
+               }
+       }
+       grid.vcrskip(LyXLength(vskip), cellrow - 1);
+       return true;
+}
+
+
+/*!
+ * Add the column \p cellcol to \p grid.
+ * \returns wether the column could be added. Adding a column can fail for
+ * environments like "eqnarray" that have a fixed number of columns.
+ */
+bool addCol(MathGridInset & grid, MathGridInset::col_type & cellcol)
+{
+       ++cellcol;
+       if (cellcol == grid.ncols()) {
+               //lyxerr << "adding column " << cellcol << endl;
+               grid.addCol(cellcol - 1);
+               if (cellcol == grid.ncols()) {
+                       // We can't add a column to this grid, so let's
+                       // append the content of this cell to the previous
+                       // one.
+                       // This does not happen in well formed .lyx files,
+                       // but LyX versions 1.3.x and older could create
+                       // such files and tex2lyx can still do that.
+                       --cellcol;
+                       lyxerr << "ignoring extra column." << endl;
+                       return false;
+               }
+       }
+       return true;
+}
+
+
+/*!
+ * Check wether the last row is empty and remove it if yes.
+ * Otherwise the following code
+ * \verbatim
+\begin{array}{|c|c|}
+\hline
+1 & 2 \\ \hline
+3 & 4 \\ \hline
+\end{array}
+ * \endverbatim
+ * will result in a grid with 3 rows (+ the dummy row that is always present),
+ * because the last '\\' opens a new row.
+ */
+void delEmptyLastRow(MathGridInset & grid)
+{
+       MathGridInset::row_type const row = grid.nrows() - 1;
+       for (MathGridInset::col_type col = 0; col < grid.ncols(); ++col) {
+               if (!grid.cell(grid.index(row, col)).empty())
+                       return;
+       }
+       // Copy the row information of the empty row (which would contain the
+       // last hline in the example above) to the dummy row and delete the
+       // empty row.
+       grid.rowinfo(row + 1) = grid.rowinfo(row);
+       grid.delRow(row);
+}
+
+
 // These are TeX's catcodes
 enum CatCode {
        catEscape,     // 0    backslash
@@ -133,7 +219,8 @@ inline CatCode catcode(unsigned char c)
 
 
 enum {
-       FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing
+       FLAG_ALIGN      = 1 << 0,  //  next & or \\ ends the parsing process
+       FLAG_BRACE_LAST = 1 << 1,  //  next closing brace ends the parsing
        FLAG_RIGHT      = 1 << 2,  //  next \\right ends the parsing process
        FLAG_END        = 1 << 3,  //  next \\end ends the parsing process
        FLAG_BRACK_LAST = 1 << 4,  //  next closing bracket ends the parsing
@@ -144,8 +231,7 @@ enum {
        FLAG_EQUATION   = 1 << 9,  //  next \] leaves the loop
        FLAG_SIMPLE2    = 1 << 10, //  next \) leaves the loop
        FLAG_OPTION     = 1 << 11, //  read [...] style option
-       FLAG_BRACED     = 1 << 12, //  read {...} style argument
-       FLAG_SKIPSPACE  = 1 << 13  //  skip spaces
+       FLAG_BRACED     = 1 << 12  //  read {...} style argument
 };
 
 
@@ -260,6 +346,8 @@ private:
        vector<Token> tokens_;
        ///
        unsigned pos_;
+       /// Stack of active environments
+       vector<string> environments_;
 };
 
 
@@ -391,7 +479,7 @@ void Parser::tokenize(istream & is)
 
 void Parser::tokenize(string const & buffer)
 {
-       istringstream is(buffer.c_str(), ios::in | ios::binary);
+       istringstream is(buffer, ios::in | ios::binary);
 
        char c;
        while (is.get(c)) {
@@ -585,14 +673,6 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                lyxerr << endl;
 #endif
 
-               if (flags & FLAG_SKIPSPACE) {
-                       if (t.cat() == catSpace || t.cat() == catNewline) 
-                               continue;
-                       pop_back();
-                       return;
-               }
-
-
                if (flags & FLAG_ITEM) {
 
                        if (t.cat() == catBegin) {
@@ -706,13 +786,12 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                }
 
                else if (t.cat() == catAlign) {
-                       ++cellcol;
-                       //lyxerr << " column now " << cellcol << " max: " << grid.ncols() << endl;
-                       if (cellcol == grid.ncols()) {
-                               //lyxerr << "adding column " << cellcol << endl;
-                               grid.addCol(cellcol - 1);
-                       }
-                       cell = &grid.cell(grid.index(cellrow, cellcol));
+                       //lyxerr << " column now " << (cellcol + 1)
+                       //       << " max: " << grid.ncols() << endl;
+                       if (flags & FLAG_ALIGN)
+                               return;
+                       if (addCol(grid, cellcol))
+                               cell = &grid.cell(grid.index(cellrow, cellcol));
                }
 
                else if (t.cat() == catSuper || t.cat() == catSub) {
@@ -805,9 +884,9 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                                        return;
                                }
 
-                               string arg  = getArg('[', ']');
+                               string const arg  = getArg('[', ']');
                                if (!arg.empty())
-                                       nargs = atoi(arg.c_str());
+                                       nargs = convert<int>(arg);
 
                        }
 
@@ -849,12 +928,30 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                else if (t.cs() == "end") {
                        if (flags & FLAG_END) {
                                // eat environment name
-                               //string const name =
-                               getArg('{', '}');
-                               // FIXME: check that we ended the correct environment
-                               return;
-                       }
-                       error("found 'end' unexpectedly");
+                               string const name = getArg('{', '}');
+                               if (environments_.empty())
+                                       error("'found \\end{" + name +
+                                             "}' without matching '\\begin{" +
+                                             name + "}'");
+                               else if (name != environments_.back())
+                                       error("'\\end{" + name +
+                                             "}' does not match '\\begin{" +
+                                             environments_.back() + "}'");
+                               else {
+                                       environments_.pop_back();
+                                       // Delete empty last row in matrix
+                                       // like insets.
+                                       // If you abuse MathGridInset for
+                                       // non-matrix like structures you
+                                       // probably need to refine this test.
+                                       // Right now we only have to test for
+                                       // single line hull insets.
+                                       if (grid.nrows() > 1)
+                                               delEmptyLastRow(grid);
+                                       return;
+                               }
+                       } else
+                               error("found 'end' unexpectedly");
                }
 
                else if (t.cs() == ")") {
@@ -870,14 +967,16 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                }
 
                else if (t.cs() == "\\") {
-                       grid.vcrskip(LyXLength(getArg('[', ']')), cellrow);
-                       ++cellrow;
-                       cellcol = 0;
-                       if (cellrow == grid.nrows())
-                               grid.addRow(cellrow - 1);
-                       if (grid.asHullInset())
-                               grid.asHullInset()->numbered(cellrow, numbered);
-                       cell = &grid.cell(grid.index(cellrow, cellcol));
+                       if (flags & FLAG_ALIGN)
+                               return;
+                       if (addRow(grid, cellrow, getArg('[', ']'))) {
+                               cellcol = 0;
+                               if (grid.asHullInset())
+                                       grid.asHullInset()->numbered(
+                                                       cellrow, numbered);
+                               cell = &grid.cell(grid.index(cellrow,
+                                                            cellcol));
+                       }
                }
 
 #if 0
@@ -891,16 +990,15 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                        }
                        // resize the table if necessary
                        for (int i = 0; i < cols; ++i) {
-                               ++cellcol;
-                               if (cellcol == grid.ncols()) {
-                                       //lyxerr << "adding column " << cellcol << endl;
-                                       grid.addCol(cellcol - 1);
+                               if (addCol(grid, cellcol)) {
+                                       cell = &grid.cell(grid.index(
+                                                       cellrow, cellcol));
+                                       // mark this as dummy
+                                       grid.cellinfo(grid.index(
+                                               cellrow, cellcol)).dummy_ = true;
                                }
-                               cell = &grid.cell(grid.index(cellrow, cellcol));
-                               // mark this as dummy
-                               grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = true;
                        }
-                       // the last cell is the real thng, not a dummy
+                       // the last cell is the real thing, not a dummy
                        grid.cellinfo(grid.index(cellrow, cellcol)).dummy_ = false;
 
                        // read special alignment
@@ -961,11 +1059,16 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
 
                else if (t.cs() == "left") {
                        skipSpaces();
-                       string l = getToken().asString();
+                       Token const & tl = getToken();
+                       // \| and \Vert are equivalent, and MathDelimInset
+                       // can't handle \|
+                       // FIXME: fix this in MathDelimInset itself!
+                       string const l = tl.cs() == "|" ? "Vert" : tl.asString();
                        MathArray ar;
                        parse(ar, FLAG_RIGHT, mode);
                        skipSpaces();
-                       string r = getToken().asString();
+                       Token const & tr = getToken();
+                       string const r = tr.cs() == "|" ? "Vert" : tr.asString();
                        cell->push_back(MathAtom(new MathDelimInset(l, r, ar)));
                }
 
@@ -979,6 +1082,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
 
                else if (t.cs() == "begin") {
                        string const name = getArg('{', '}');
+                       environments_.push_back(name);
 
                        if (name == "array" || name == "subarray") {
                                string const valign = parse_verbatim_option() + 'c';
@@ -995,7 +1099,14 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                        }
 
                        else if (name == "split" || name == "cases" ||
-                                        name == "gathered" || name == "aligned") {
+                                name == "gathered" || name == "aligned") {
+                               cell->push_back(createMathInset(name));
+                               parse2(cell->back(), FLAG_END, mode, false);
+                       }
+
+                       else if (name == "alignedat") {
+                               // ignore this for a while
+                               getArg('{', '}');
                                cell->push_back(createMathInset(name));
                                parse2(cell->back(), FLAG_END, mode, false);
                        }
@@ -1092,6 +1203,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                }
 
                else if (t.cs() == "label") {
+                       // FIXME: This is swallowed in inline formulas
                        string label = parse_verbatim_item();
                        MathArray ar;
                        asArray(label, ar);
@@ -1113,10 +1225,21 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                }
 
                else if (t.cs() == "color") {
-                       MathAtom at = createMathInset(t.cs());
-                       parse(at.nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
-                       parse(at.nucleus()->cell(1), flags, mode);
-                       cell->push_back(at);
+                       string const color = parse_verbatim_item();
+                       cell->push_back(MathAtom(new MathColorInset(true, color)));
+                       parse(cell->back().nucleus()->cell(0), flags, mode);
+                       return;
+               }
+
+               else if (t.cs() == "textcolor") {
+                       string const color = parse_verbatim_item();
+                       cell->push_back(MathAtom(new MathColorInset(false, color)));
+                       parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
+               }
+
+               else if (t.cs() == "normalcolor") {
+                       cell->push_back(createMathInset(t.cs()));
+                       parse(cell->back().nucleus()->cell(0), flags, mode);
                        return;
                }
 
@@ -1125,6 +1248,11 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                        parse2(cell->back(), FLAG_ITEM, mode, false);
                }
 
+               else if (t.cs() == "xymatrix") {
+                       cell->push_back(createMathInset(t.cs()));
+                       parse2(cell->back(), FLAG_ITEM, mode, false);
+               }
+
                else if (t.cs() == "framebox" || t.cs() == "makebox") {
                        cell->push_back(createMathInset(t.cs()));
                        parse(cell->back().nucleus()->cell(0), FLAG_OPTION, MathInset::TEXT_MODE);
@@ -1163,35 +1291,35 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                        if (l) {
                                if (l->inset == "font") {
                                        cell->push_back(createMathInset(t.cs()));
-                                       parse(cell->back().nucleus()->cell(0), FLAG_ITEM, asMode(mode, l->extra));
+                                       parse(cell->back().nucleus()->cell(0),
+                                               FLAG_ITEM, asMode(mode, l->extra));
                                }
 
                                else if (l->inset == "oldfont") {
                                        cell->push_back(createMathInset(t.cs()));
-                                       parse(cell->back().nucleus()->cell(0), flags, asMode(mode, l->extra));
-                                       return;
+                                       parse(cell->back().nucleus()->cell(0),
+                                               flags | FLAG_ALIGN, asMode(mode, l->extra));
+                                       if (prevToken().cat() != catAlign &&
+                                           prevToken().cs() != "\\")
+                                               return;
+                                       putback();
                                }
 
                                else if (l->inset == "style") {
                                        cell->push_back(createMathInset(t.cs()));
-                                       parse(cell->back().nucleus()->cell(0), flags, mode);
-                                       return;
-                               }
-
-                               else if (l->inset == "parbox") {
-                                       // read optional positioning and width
-                                       string pos   = parse_verbatim_option();
-                                       string width = parse_verbatim_item();
-                                       cell->push_back(createMathInset(t.cs()));
-                                       parse(cell->back().nucleus()->cell(0), FLAG_ITEM, MathInset::TEXT_MODE);
-                                       cell->back().nucleus()->asParboxInset()->setPosition(pos);
-                                       cell->back().nucleus()->asParboxInset()->setWidth(width);
+                                       parse(cell->back().nucleus()->cell(0),
+                                               flags | FLAG_ALIGN, mode);
+                                       if (prevToken().cat() != catAlign &&
+                                           prevToken().cs() != "\\")
+                                               return;
+                                       putback();
                                }
 
                                else {
                                        MathAtom at = createMathInset(t.cs());
                                        for (MathInset::idx_type i = 0; i < at->nargs(); ++i)
-                                               parse(at.nucleus()->cell(i), FLAG_ITEM, asMode(mode, l->extra));
+                                               parse(at.nucleus()->cell(i),
+                                                       FLAG_ITEM, asMode(mode, l->extra));
                                        cell->push_back(at);
                                }
                        }
@@ -1214,7 +1342,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
                                //}
                                for (MathInset::idx_type i = start; i < at->nargs(); ++i) {
                                        parse(at.nucleus()->cell(i), FLAG_ITEM, m);
-                                       parse1(grid, FLAG_SKIPSPACE, mode, numbered);
+                                       skipSpaces();
                                }
                                cell->push_back(at);
                        }
@@ -1235,7 +1363,7 @@ void Parser::parse1(MathGridInset & grid, unsigned flags,
 
 void mathed_parse_cell(MathArray & ar, string const & str)
 {
-       istringstream is(str.c_str());
+       istringstream is(str);
        mathed_parse_cell(ar, is);
 }
 
@@ -1248,7 +1376,7 @@ void mathed_parse_cell(MathArray & ar, istream & is)
 
 bool mathed_parse_normal(MathAtom & t, string const & str)
 {
-       istringstream is(str.c_str());
+       istringstream is(str);
        return Parser(is).parse(t);
 }
 
@@ -1267,7 +1395,7 @@ bool mathed_parse_normal(MathAtom & t, LyXLex & lex)
 
 void mathed_parse_normal(MathGridInset & grid, string const & str)
 {
-       istringstream is(str.c_str());
+       istringstream is(str);
        Parser(is).parse1(grid, 0, MathInset::MATH_MODE, false);
 }