]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathParser.cpp
Fix bug #12772
[lyx.git] / src / mathed / MathParser.cpp
index ee6035304690cea0962d4147c8aaa62e75da4d56..1958900e3d2f0155e64609f5fa1ab3273b59bd3b 100644 (file)
@@ -77,7 +77,6 @@ following hack as starting point to write some macros:
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
-#include "support/unique_ptr.h"
 
 #include <sstream>
 
@@ -158,10 +157,10 @@ docstring escapeSpecialChars(docstring const & str, bool textmode)
 
 /*!
  * Add the row \p cellrow to \p grid.
- * \returns wether the row could be added. Adding a row can fail for
+ * \returns whether the row could be added. Adding a row can fail for
  * environments like "equation" that have a fixed number of rows.
  */
-bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
+bool addRow(InsetMathGrid & grid, row_type & cellrow,
            docstring const & vskip, bool allow_newpage = true)
 {
        ++cellrow;
@@ -193,10 +192,10 @@ bool addRow(InsetMathGrid & grid, InsetMathGrid::row_type & cellrow,
 
 /*!
  * Add the column \p cellcol to \p grid.
- * \returns wether the column could be added. Adding a column can fail for
+ * \returns whether the column could be added. Adding a column can fail for
  * environments like "eqnarray" that have a fixed number of columns.
  */
-bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
+bool addCol(InsetMathGrid & grid, col_type & cellcol)
 {
        ++cellcol;
        if (cellcol == grid.ncols()) {
@@ -237,9 +236,9 @@ bool addCol(InsetMathGrid & grid, InsetMathGrid::col_type & cellcol)
  */
 void delEmptyLastRow(InsetMathGrid & grid)
 {
-       InsetMathGrid::row_type const row = grid.nrows() - 1;
-       for (InsetMathGrid::col_type col = 0; col < grid.ncols(); ++col) {
-               InsetMathGrid::idx_type const idx = grid.index(row, col);
+       row_type const row = grid.nrows() - 1;
+       for (col_type col = 0; col < grid.ncols(); ++col) {
+               idx_type const idx = grid.index(row, col);
                if (!grid.cell(idx).empty() ||
                    grid.cellinfo(idx).multi != InsetMathGrid::CELL_NORMAL)
                        return;
@@ -360,7 +359,7 @@ ostream & operator<<(ostream & os, Token const & t)
 {
        if (!t.cs().empty()) {
                docstring const & cs = t.cs();
-               // FIXME: For some strange reason, the stream operator instanciate
+               // FIXME: For some strange reason, the stream operator instantiate
                // a new Token before outputting the contents of t.cs().
                // Because of this the line
                //     os << '\\' << cs;
@@ -371,9 +370,9 @@ ostream & operator<<(ostream & os, Token const & t)
                os << '\\' << to_utf8(cs);
        }
        else if (t.cat() == catLetter)
-               os << t.character();
+               os << static_cast<uint32_t>(t.character());
        else
-               os << '[' << t.character() << ',' << t.cat() << ']';
+               os << '[' << static_cast<uint32_t>(t.character()) << ',' << t.cat() << ']';
        return os;
 }
 
@@ -797,9 +796,8 @@ void Parser::parse2(MathAtom & at, const unsigned flags, const mode_type mode,
 bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
        const mode_type mode, const bool numbered)
 {
-       int limits = 0;
-       InsetMathGrid::row_type cellrow = 0;
-       InsetMathGrid::col_type cellcol = 0;
+       row_type cellrow = 0;
+       col_type cellcol = 0;
        MathData * cell = &grid.cell(grid.index(cellrow, cellcol));
        Buffer * buf = buffer_;
 
@@ -1006,10 +1004,6 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                p->nuc().erase(0);
 
                        parse(p->cell(p->idxOfScript(up)), FLAG_ITEM, mode);
-                       if (limits) {
-                               p->limits(limits);
-                               limits = 0;
-                       }
                }
 
                else if (t.character() == ']' && (flags & FLAG_BRACK_LAST)) {
@@ -1375,7 +1369,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        // otherwise parse it as an user macro
                        MathData count;
                        parse(count, FLAG_ITEM, mode);
-                       int cols;
+                       int cols = 0;
                        // limit arbitrarily to 100 columns
                        if (extractNumber(count, cols) && cols > 0 && cols < 100) {
                                // resize the table if necessary
@@ -1408,9 +1402,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                }
 
                else if (t.cs() == "limits" || t.cs() == "nolimits") {
-                       CatCode const cat = nextToken().cat();
-                       if (cat == catSuper || cat == catSub)
-                               limits = t.cs() == "limits" ? 1 : -1;
+                       if (!cell->empty())
+                               cell->back()->limits(t.cs() == "limits" ? LIMITS : NO_LIMITS);
                        else {
                                MathAtom at = createInsetMath(t.cs(), buf);
                                cell->push_back(at);
@@ -1503,7 +1496,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                if (ar[i].size() == 1)
                                        script[i] = ar[i][0].nucleus()->asScriptInset();
                        }
-                       bool const hasscript[2] = {script[0] ? true : false, script[1] ? true : false};
+                       bool const hasscript[2] = {script[0] != nullptr, script[1] != nullptr};
                        cell->push_back(MathAtom(new InsetMathSideset(buf, hasscript[0], hasscript[1])));
                        if (hasscript[0]) {
                                if (script[0]->hasDown())
@@ -1553,7 +1546,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
 
                else if (t.cs() == "ref" || t.cs() == "eqref" || t.cs() == "prettyref"
                          || t.cs() == "nameref" || t.cs() == "pageref"
-                         || t.cs() == "vpageref" || t.cs() == "vref") {
+                         || t.cs() == "vpageref" || t.cs() == "vref" 
+                         || t.cs() == "formatted" || t.cs() == "labelonly") {
                        cell->push_back(MathAtom(new InsetMathRef(buf, t.cs())));
                        docstring const opt = parse_verbatim_option();
                        docstring const ref = parse_verbatim_item();
@@ -1800,7 +1794,6 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        asArray(label, ar);
                        if (grid.asHullInset()) {
                                grid.asHullInset()->label(cellrow, label);
-                               grid.asHullInset()->numbered(cellrow, true);
                        } else {
                                cell->push_back(createInsetMath(t.cs(), buf));
                                cell->push_back(MathAtom(new InsetMathBrace(ar)));
@@ -1888,7 +1881,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                        bool const prot =  nextToken().character() == '*';
                        if (prot)
                                getToken();
-                       docstring const name = t.cs();
+                       docstring const name = t.cs();
                        docstring const arg = parse_verbatim_item();
                        Length length;
                        if (prot && arg == "\\fill")
@@ -1968,8 +1961,8 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                cmd = Encodings::fromLaTeXCommand(cmd,
                                        Encodings::MATH_CMD | Encodings::TEXT_CMD,
                                        termination, rem);
-                               for (size_t i = 0; i < cmd.size(); ++i)
-                                       cell->push_back(MathAtom(new InsetMathChar(cmd[i])));
+                               for (char_type c : cmd)
+                                       cell->push_back(MathAtom(new InsetMathChar(c)));
                                if (!rem.empty()) {
                                        char_type c = rem[0];
                                        cell->push_back(MathAtom(new InsetMathChar(c)));
@@ -2042,7 +2035,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
 
                                else {
                                        MathAtom at = createInsetMath(t.cs(), buf);
-                                       for (InsetMath::idx_type i = 0; i < at->nargs(); ++i)
+                                       for (idx_type i = 0; i < at->nargs(); ++i)
                                                parse(at.nucleus()->cell(i),
                                                        FLAG_ITEM, asMode(mode, l->extra));
                                        cell->push_back(at);
@@ -2083,19 +2076,17 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                                        Encodings::MATH_CMD | Encodings::TEXT_CMD,
                                                        is_combining, termination);
                                        }
-                                       if (c && buf->params().encoding().encodable(c)) {
+                                       if (c && buf && buf->params().encoding().encodable(c)) {
                                                if (termination) {
                                                        if (nextToken().cat() == catBegin) {
                                                                getToken();
                                                                if (nextToken().cat() == catEnd) {
                                                                        getToken();
-                                                                       num_tokens += 2;
                                                                } else
                                                                        putback();
                                                        } else {
                                                                while (nextToken().cat() == catSpace) {
                                                                        getToken();
-                                                                       ++num_tokens;
                                                                }
                                                        }
                                                }
@@ -2116,7 +2107,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                        if (at->currentMode() != InsetMath::UNDECIDED_MODE)
                                                m = at->currentMode();
                                        //lyxerr << "default creation: m2: " << m << endl;
-                                       InsetMath::idx_type start = 0;
+                                       idx_type start = 0;
                                        // this fails on \bigg[...\bigg]
                                        //MathData opt;
                                        //parse(opt, FLAG_OPTION, InsetMath::VERBATIM_MODE);
@@ -2124,7 +2115,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                        //      start = 1;
                                        //      at.nucleus()->cell(0) = opt;
                                        //}
-                                       for (InsetMath::idx_type i = start; i < at->nargs(); ++i) {
+                                       for (idx_type i = start; i < at->nargs(); ++i) {
                                                parse(at.nucleus()->cell(i), FLAG_ITEM, m);
                                                if (mode == InsetMath::MATH_MODE)
                                                        skipSpaces();
@@ -2136,7 +2127,6 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
 
 
                if (flags & FLAG_LEAVE) {
-                       flags &= ~FLAG_LEAVE;
                        break;
                }
        }
@@ -2166,7 +2156,7 @@ bool mathed_parse_cell(MathData & ar, docstring const & str, Parse::flags f)
 
 bool mathed_parse_cell(MathData & ar, istream & is, Parse::flags f)
 {
-       return Parser(is, f, ar.buffer()).parse(ar, 0, f & Parse::TEXTMODE ?
+       return Parser(is, f, ar.buffer()).parse(ar, 0, (f & Parse::TEXTMODE) ?
                                InsetMath::TEXT_MODE : InsetMath::MATH_MODE);
 }